{"错误":[{"代码":32,"消息":"无法验证您。"}]} asp。净

时间:2017-06-16 09:58:30

标签: c# asp.net twitter twitter-oauth

我在尝试从twitter获取令牌时收到以下错误。下面是我试图执行的代码。

请在我做错的地方帮助我..

这是主要功能。

string url = "https://api.twitter.com/oauth/request_token";
    string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture)));
    string oauth_timestamp = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString(CultureInfo.InvariantCulture);
    //string oauth_callback = "http://localhost:11761/iRouter.aspx";
    string oauth_consumer_key = "[REDACTED]";
    string oauth_token = "[REDACTED]";
    string oauth_signature_method = "HMAC-SHA1";
    string oauth_version = "1.0";
    string oauth_signature = CreateSignature(url, oauth_nonce, oauth_timestamp, oauth_token, oauth_consumer_key, oauth_signature_method, oauth_version);
    string auth_header = CreateAuthorizationHeaderParameter(oauth_signature, oauth_timestamp, oauth_nonce, oauth_signature_method, oauth_consumer_key, oauth_token, oauth_version);
    try
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; ;
        WebClient wc = new WebClient();
        wc.Headers.Add("Content-Type: " + "application/x-www-form-urlencoded;charset=UTF-8");
        wc.Headers.Add("Authorization: " + auth_header);
        Stream data = wc.OpenRead(url);
        StreamReader reader = new StreamReader(data);
        string retirnedJson = reader.ReadToEnd();
        data.Close();
        reader.Close();

        return retirnedJson;
    }
    catch (WebException wex)
    {
        if (wex.Response != null)
        {
            using (var errorResponse = (HttpWebResponse)wex.Response)
            {
                using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                {
                    string error = reader.ReadToEnd();
                    //TODO: use JSON.net to parse this string and look at the error message
                }
            }
        }return null;
    }

创建签名的方法。

public string CreateSignature(string url, string _oauthNonce, string _oathTimestamp, string OauthToken, string OauthConsumerKey, string OauthSignatureMethod, string OathVersion)
{
    //string builder will be used to append all the key value pairs
    var stringBuilder = new StringBuilder();
    stringBuilder.Append("POST&");
    stringBuilder.Append(Uri.EscapeDataString(url));
    stringBuilder.Append("&");

    //the key value pairs have to be sorted by encoded key
    var dictionary = new SortedDictionary<string, string>
                             {
                                 {"oauth_nonce", _oauthNonce},
                                 {"oauth_signature_method", OauthSignatureMethod},
                                 {"oauth_timestamp", _oathTimestamp},
                                 {"oauth_consumer_key", OauthConsumerKey},
                                 {"oauth_token", OauthToken},
                                 {"oauth_version", OathVersion},
                             };

    foreach (var keyValuePair in dictionary)
    {
        //append a = between the key and the value and a & after the value
        stringBuilder.Append(Uri.EscapeDataString(string.Format("{0}={1}&", keyValuePair.Key, keyValuePair.Value)));
    }
    string signatureBaseString = stringBuilder.ToString().Substring(0, stringBuilder.Length - 3);

    //generation the signature key the hash will use
    string signatureKey =
        Uri.EscapeDataString(OauthConsumerKey) + "&" +
        Uri.EscapeDataString(OauthToken);

    var hmacsha1 = new HMACSHA1(
        new ASCIIEncoding().GetBytes(signatureKey));

    //hash the values
    string signatureString = Convert.ToBase64String(
        hmacsha1.ComputeHash(
            new ASCIIEncoding().GetBytes(signatureBaseString)));

    return signatureString;
}

创建标题的方法

public string CreateAuthorizationHeaderParameter(string oauth_signature, string oauth_timestamp, string oauth_nonce, string oauth_signature_method, string oauth_consumer_key, string oauth_token, string oauth_version)
{
    string authorizationHeaderParams = String.Empty;
    authorizationHeaderParams += "OAuth ";
    authorizationHeaderParams += "oauth_nonce=" + "\"" +
                                 Uri.EscapeDataString(oauth_nonce) + "\",";

    authorizationHeaderParams +=
        "oauth_signature_method=" + "\"" +
        Uri.EscapeDataString(oauth_signature_method) +
        "\",";

    authorizationHeaderParams += "oauth_timestamp=" + "\"" +
                                 Uri.EscapeDataString(oauth_timestamp) + "\",";

    authorizationHeaderParams += "oauth_consumer_key="
                                 + "\"" + Uri.EscapeDataString(oauth_consumer_key) + "\",";

    authorizationHeaderParams += "oauth_token=" + "\"" +
                                 Uri.EscapeDataString(oauth_token) + "\",";

    authorizationHeaderParams += "oauth_signature=" + "\""
                                 + Uri.EscapeDataString(oauth_signature) + "\",";

    authorizationHeaderParams += "oauth_version=" + "\"" +
                                 Uri.EscapeDataString(oauth_version) + "\"";
    return authorizationHeaderParams;
}

2 个答案:

答案 0 :(得分:0)

  1. 如果您最近更改了应用的权限,请尝试重新生成OAUTH密钥。
  2. 尝试将POST更改为GET,因为您正在发送读取而不是写入。

答案 1 :(得分:0)

转到twitter开发人员帐户,然后进行设置,请检查是否已启用“阅读+发布推文和个人资料”信息,这不是默认选项,没有此选项,您可以阅读推文,但不能发布。 / p>

就我而言,这解决了问题,我使用的是Tweetinvi,非常易于使用。

enter image description here