在调用twitter API时,错误消息“无法验证您”,代码为32?

时间:2016-07-18 21:30:07

标签: android twitter oauth

调用此API时: https://api.twitter.com/1.1/followers/list.json?amp%3Binclude_user_entities=false&amp%3Bscreen_name=twitterdev&amp%3Bskip_status=true&cursor=-1

具有以下授权:

OAuth oauth_consumer_key="XXXXXXXXXXXXXXXXXXXXXXXXX", oauth_nonce="0ddddff20481ae3567739f4eefa49b22", oauth_signature="7DQKPmbqJAzRh%2FM0UTB2w82lGvY%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1468795885", oauth_token="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", oauth_version="1.0" 

从twitter获取我的关注者列表,我成功获得了关注者的json对象,但是当从 twitterdev <替换 screen_name (通过顶部api传递的参数)的值时/ strong>到我的实际screen_name的值,我收到此错误:

{
"errors": [
    {
        "code": 32,
        "message": "Could not authenticate you."
    }
]

}

拜托,任何人都可以帮我解决这个问题吗?

以下代码为我的请求生成OAuth签名:

    String oauth_signature_method = "HMAC-SHA1";
    String oauth_consumer_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    String oauth_token= "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";

    String uuid_string = UUID.randomUUID().toString();
    uuid_string = uuid_string.replaceAll("-", "");
    String oauth_nonce = uuid_string;

    Date now = Calendar.getInstance().getTime();
    String oauth_timestamp = (new Long(now.getTime()/1000)).toString();

    String parameter_string = "oauth_consumer_key=" + oauth_consumer_key + "&oauth_nonce=" +
            oauth_nonce + "&oauth_signature_method=" + oauth_signature_method +
            "&oauth_timestamp=" + oauth_timestamp + "&oauth_token=" + authToken +
            "&oauth_version=1.0";

    String signature_base_string = " GET&https%3A%2F%2Fapi.twitter.com%2F1.1%2Ffollowers%2Flist.json&"+
            URLEncoder.encode("user_id=XXXXXXXXXX&", "UTF-8") +
            URLEncoder.encode(parameter_string, "UTF-8");

    String oauth_signature = computeSignature(signature_base_string, "XXXXXXXXXXXXXXXXXXXXXXXXXXX" + "&");

    String auth_header_string = "OAuth oauth_consumer_key=\"" + oauth_consumer_key +
            "\", oauth_nonce=\"" + oauth_nonce +
            "\", oauth_signature=\""+URLEncoder.encode(oauth_signature, "UTF-8") +
            "\", oauth_signature_method=\"HMAC-SHA1" +
            "\", oauth_timestamp=\""+ oauth_timestamp +
            "\", oauth_token=\""+oauth_token +
            "\", oauth_version=\"1.0\"";


private String computeSignature(String baseString, String keyString){

    SecretKey secretKey = null;

    byte[] keyBytes = keyString.getBytes();
    secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");

    Mac mac = null;
    try {
        mac = Mac.getInstance("HmacSHA1");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    try {
        mac.init(secretKey);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }

    byte[] text = baseString.getBytes();

    return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
}

1 个答案:

答案 0 :(得分:1)

您无法在不重新生成签名的情况下更改请求查询参数。具体请参见https://dev.twitter.com/oauth/overview/creating-signatures&#34;收集参数&#34;。

stackoverflow中有很多用于签名请求的示例,您应该从那里开始,专门搜索您在Android中使用的框架。