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();

ここで何が問題なのかわかりません。ヘルプが必要です。

4

2 に答える 2

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 に答える
0

これにより、あなたの人生はとても楽になります。私は以前にそれを使用しましたが、すべてを処理します。 http://www.twitterizer.net

于 2012-11-25T09:04:28.287 に答える