0

我正在使用以下代码来尝试获取 oauth_token 和 oauth_secret。

        string url = "https://api.twitter.com/oauth/request_token";
        string base_format = "oauth_callback={0}&oauth_consumer_key={1}&oauth_nonce={2}&oauth_signature_method={3}&oauth_timestamp={4}&oauth_version={5}";
        string base_string = String.Format(base_format, oauth_callback, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_version);
        base_string = String.Concat("POST&", Uri.EscapeDataString(url), Uri.EscapeDataString(base_string));

        var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                    "&");

        string oauth_signature;
        using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
        {
            oauth_signature = Convert.ToBase64String(
                hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(base_string)));
        }
        string header_format = "OAuth oauth_nonce=\"{0}\", oauth_callback=\"{1}\", oauth_signature_method=\"{2}\", oauth_timestamp=\"{3}\", oauth_consumer_key=\"{4}\", oauth_signature=\"{5}\", oauth_version=\"{6}\"";
        string header = string.Format(header_format, Uri.EscapeDataString(oauth_nonce), Uri.EscapeDataString(oauth_callback), Uri.EscapeDataString(oauth_signature_method), Uri.EscapeDataString(oauth_timestamp), Uri.EscapeDataString(oauth_consumer_key), Uri.EscapeDataString(oauth_signature), Uri.EscapeDataString(oauth_version));

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Headers.Add("Authorization", header);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";

        WebResponse response = req.GetResponse();

它还给我一个 401。我看不出这里有什么问题。需要帮助。

4

2 回答 2

0

这将使您的生活变得更加轻松。我以前用过它,它会照顾一切。 http://www.twitterizer.net

于 2012-11-25T09:04:28.287 回答
0

您需要将 OAuthTokenSecret 添加到您的复合密钥中。

工作示例

public string UpdateStatus(string status)
{
    var oauth_version = "1.0";
    var oauth_signature_method = "HMAC-SHA1";

    // unique request details
    var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
    var oauth_timestamp = Convert.ToInt64(
        (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc))
            .TotalSeconds).ToString();

    var resource_url = "https://api.twitter.com/1.1/statuses/update.json";

    // create oauth signature
    var baseString = string.Format(
        "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}&" +
        "oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}",
        config.OAuthConsumerKey,
        oauth_nonce,
        oauth_signature_method,
        oauth_timestamp,
        config.OAuthToken,
        oauth_version,
        Uri.EscapeDataString(status));


    baseString = string.Concat("POST&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

    var compositeKey = string.Concat(Uri.EscapeDataString(config.OAuthConsumerSecret),
    "&", Uri.EscapeDataString(config.OAuthTokenSecret));

    string oauth_signature;
    using (var hasher = new HMACSHA1(Encoding.ASCII.GetBytes(compositeKey)))
    {
        oauth_signature = Convert.ToBase64String(hasher.ComputeHash(Encoding.ASCII.GetBytes(baseString)));
    }

    // create the request header
    var authHeader = string.Format(
        "OAuth oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\"," +
        " oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " +
        "oauth_timestamp=\"{4}\",  oauth_token=\"{5}\", " +
        "oauth_version=\"{6}\"",
        Uri.EscapeDataString(config.OAuthConsumerKey),
        Uri.EscapeDataString(oauth_nonce),
        Uri.EscapeDataString(oauth_signature),
        Uri.EscapeDataString(oauth_signature_method),
        Uri.EscapeDataString(oauth_timestamp),
        Uri.EscapeDataString(config.OAuthToken),
        Uri.EscapeDataString(oauth_version)
    );


    var postBody = "status=" + Uri.EscapeDataString(status);
    ServicePointManager.Expect100Continue = false;

    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        using (Stream stream = request.GetRequestStream())
        {
            byte[] content = Encoding.ASCII.GetBytes(postBody);
            stream.Write(content, 0, content.Length);
        }

        WebResponse response = request.GetResponse();
        Stream reqStream = response.GetResponseStream();
        StreamReader reqStreamReader = new StreamReader(reqStream);
        return reqStreamReader.ReadToEnd();
    }
    catch (Exception ex)
    {
        return ex + ":\n" + ex.Message;
    }
}
于 2016-10-09T22:26:13.913 回答