如何使用C#和twitter DM API创建授权的创建欢迎消息请求

时间:2018-02-21 09:26:13

标签: c# asp.net-mvc api asp.net-web-api twitter

我正在使用Twitter AccountActivity API和DM API,但是当涉及POST / DELETE请求时,我收到此错误:

  • 401 Unauthorized

但是GET请求工作正常。我想我的请求标题有问题。有人可以帮忙吗? 这是我正在使用的代码示例:

public WebResponse PostWelcomeMessageRule(HttpWebRequest request)
    {
        var oauth_token = ConfigurationManager.AppSettings["TK"];
        var oauth_token_secret = ConfigurationManager.AppSettings["TS"];
        var oauth_consumer_key = ConfigurationManager.AppSettings["CK"].ToString();
        var oauth_consumer_secret = ConfigurationManager.AppSettings["CS"].ToString();
        // oauth implementation details
        var oauth_version = "1.0";
        var oauth_signature_method = "HMAC-SHA1";
        var welcomeMsgId = 961185762921328645;
        // unique request details
        var oauth_nonce = Convert.ToBase64String(
            new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
        var timeSpan = DateTime.UtcNow
            - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

        // message api details
        var resource_url = "https://api.twitter.com/1.1/direct_messages/welcome_messages/rules/new.json";
        // create oauth signature
        var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                    "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&welcome_message_id={6}";


        var baseString = string.Format(baseFormat,
                                oauth_consumer_key,
                                oauth_nonce,
                                oauth_signature_method,
                                oauth_timestamp,
                                oauth_token,
                                oauth_version,
                                welcomeMsgId
                                );

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

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

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

        // create the request header
        var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                           "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                           "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                           "oauth_version=\"{6}\", welcome_message_id=\"{7}\"";

        var authHeader = string.Format(headerFormat,
                    Uri.EscapeDataString(oauth_nonce),
                    Uri.EscapeDataString(oauth_signature_method),
                    Uri.EscapeDataString(oauth_timestamp),
                    Uri.EscapeDataString(oauth_consumer_key),
                    Uri.EscapeDataString(oauth_token),
                    Uri.EscapeDataString(oauth_signature),
                    Uri.EscapeDataString(oauth_version),
                    Uri.EscapeDataString(welcomeMsgId.ToString())
            );

        request = (HttpWebRequest)WebRequest.Create(resource_url);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "POST";
        request.ContentType = "application/json; charset=utf-8";

        WebResponse response = request.GetResponse();
        string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
        return response;

    }

0 个答案:

没有答案