【问题标题】:Twitter API Failed to validate oauth signature and token javaTwitter API 无法验证 oauth 签名和令牌 java
【发布时间】:2011-08-10 08:35:59
【问题描述】:

我正在尝试在 Android 应用程序中使用 http 协议访问 Twitter 帐户的资源。 Twitter 使用开放式身份验证标准 OAuth 进行身份验证,因此我遵循 https://dev.twitter.com/docs/auth/oauth#Overview 中的示例。 我的问题是: 我想获取请求令牌,但是当我向端点 https://api.twitter.com/oauth/request_token 发出请求时,响应是“无法验证 oauth 签名和令牌”。 我的代码:

签名步骤

时间戳和随机数

oauthTimestamp = String.valueOf(new Date().getTime());
oauthNonce = Base64.encodeToString(oauthTimestamp.getBytes(),Base64.DEFAULT);
    //remove /n at the end of the string
oauthNonce = oauthNonce.substring(0, oauthNonce.length() - 1);

签名基本字符串

String signatureBaseString = 
     "POST"                                                                                             
     + "&"
     + URLEncoder.encode("https://api.twitter.com/oauth/request_token")
     + "&"
     + URLEncoder.encode("oauth_callback=" + redirectUrl)
     + URLEncoder.encode("&" + "oauth_consumer_key=" + consumerKey)
     + URLEncoder.encode("&" + "oauth_nonce=" + oauthNonce)
     + URLEncoder.encode("&" + "oauth_signature_method=" +  "HMAC-SHA1")
     + URLEncoder.encode("&" + "oauth_timestamp=" + oauthTimestamp)
     + URLEncoder.encode("&" + "oauth_version=" + "1.0");

签名调用

signature = getSignatureToken(applicationSecret, signatureBaseString, "HmacSHA1");

签名方法

private String getSignatureToken(String consumerSecret, String baseString, String algotithm) {
    byte[] keyBytes = (consumerSecret+"&").getBytes();
    SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, algotithm);
    Mac mac;
    String res = null;
    try {
        mac = Mac.getInstance(algotithm);
        mac.init(secretKeySpec);
        byte[] rawHmac = mac.doFinal((baseString).getBytes());
        res = android.util.Base64.encodeToString(rawHmac, android.util.Base64.DEFAULT);
        res = res.substring(0, res.length() - 1);
        System.out.println("MAC : " + res);

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return res;
}

请求令牌步骤

HttpResponse response;
HttpPost authorization = new HttpPost("https://api.twitter.com/oauth/request_token");
final String headerValue = 
    "OAuth " + 
    "oauth_nonce=\""+oauthNonce+"\", " +
    "oauth_callback=\""+redirectUrl+"\", " +
    "oauth_signature_method=\"HMAC-SHA1\", " +
    "oauth_timestamp=\""+oauthTimestamp+"\", " +
    "oauth_consumer_key=\""+consumerKey+"\", " +
    "oauth_signature=\""+URLEncoder.encode(signature)+"\", " +
    "oauth_version=\"1.0\"";    

authorization.addHeader("Authorization", headerValue);
response = this.httpClient.execute(authorization);
System.out.println(EntityUtils.toString(response.getEntity()));

response = this.httpClient.execute(authorization) 给我 HTTP/1.1 401 Unauthorized

有什么想法吗?

谢谢

【问题讨论】:

  • 我面临同样的问题,但无法解决。你能告诉我你是怎么解决的吗?

标签: java android


【解决方案1】:

好吧,你必须分阶段调试它,但我觉得奇怪的是:你为什么要删除 getSignatureToken() 中 HMAC(签名)的最后一个字符?这可能会破坏 Base64 编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2014-04-07
    • 2011-04-08
    相关资源
    最近更新 更多