具有以下授权:
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();
}
答案 0 :(得分:1)
您无法在不重新生成新签名的情况下更改请求查询参数。具体请参见https://dev.twitter.com/oauth/overview/creating-signatures&#34;收集参数&#34;。
stackoverflow中有很多用于签名请求的示例,您应该从那里开始,专门搜索您在Android中使用的框架。