static public final String CUSUMER_KEY = "mcIgMJKcyhqFlAHK9batPvRvC";
static public final String CUSUMER_SECRET = "5PRVECiRPoddsBd70qGBfZZdJVPwWcxiicFf7usfS5nyhg4GGp";
static public final String ACCESS_TOKEN = "2320954854-eZYObTlxJ3Pqqd0YuGNlWcRQJQ4rNAyvsfsssV7";
static public final String ACCESS_TOKEN_SECRET = "iOEYAG5yEgRuWRh8MY5AIfwK2f2oF6HF9gAkzArTLQUKS";
static public final String REQUEST_URL = "https://api.twitter.com/oauth/request_token";
static public final String ACCESS_URL = "http://api.twitter.com/oauth/access_token";
static public final String AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize";
static public final String AUTH_URL = "https://api.twitter.com/oauth/authenticate?oauth_token=";
static public final String CALL_BACK = "%22http%3A%2F%2Fmyapp.com%3A3005%2Ftwitter%2Fprocess_callback%22";
public String get_access_token(){
String ret="";
try{
String sg_base_str = "POST&"+ URLEncoder.encode("https://api.twitter.com/oauth/request_token", "UTF-8")+"&";
String[][] data =
{
{"oauth_consumer_key" , CUSUMER_KEY },
{"oauth_nonce" , oauth_nounce() },
{"oauth_signature" , "" },
{"oauth_signature_method", "HMAC-SHA1" },
{"oauth_timestamp" , oauth_timestamp() },
{"oauth_token" , ACCESS_TOKEN },
{"oauth_version" , "1.0" }
};
for(int i = 0; i < data.length; i++)
{
String _26;
if(i + 1 < data.length) {
_26 = "%26";
}
else _26 = "";
if(data[i][0] != "oauth_signature")
{
sg_base_str += URLEncoder.encode(data[i][0],"UTF-8") + "%3D"
+ URLEncoder.encode(data[i][1],"UTF-8") + _26 ;
}
}
ret = hmacDigest(sg_base_str,CUSUMER_SECRET+"&"+ACCESS_TOKEN_SECRET, "HmacSHA1")+"\n\n"+sg_base_str;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ret=ret.replaceAll("/","%2F");
ret=ret.replaceAll("=","%3D");
ret=ret.replaceAll("\\+","%2B");
return ret;
}
public String oauth_nounce() {
String uuid_string = UUID.randomUUID().toString();
uuid_string = uuid_string.replaceAll("-", "");
String oauth_nonce = uuid_string;
return oauth_nonce;
}
public String oauth_timestamp() {
long millis = System.currentTimeMillis();
long secs = millis / 1000;
return String.valueOf(secs);
}`public static String hmacDigest(String msg, String keyString, String algo) {
String digest = null;
try {
SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);
Mac mac = Mac.getInstance(algo);
mac.init(key);
byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));
String encoded = Base64.encodeToString(bytes, 0);
digest = encoded;
} catch (UnsupportedEncodingException e) {
} catch (InvalidKeyException e) {
} catch (NoSuchAlgorithmException e) {
}
return digest;
}`
当我运行代码来实现此目的时:oauth_nonce=252e7f4eee134ac2bf8cc287620ec2d9 oauth_timestamp=1433957398 oauth_signature=vAcm%2FNyc7S1RISe6jk7y8N1knMo%3D
然后我有POST https://api.twitter.com/oauth/request_token?oauth_consumer_key=mcIgMJKcyhqFlAHK9batPvRvC&oauth_nonce=252e7f4eee134ac2bf8cc287620ec2d9&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1433957398&oauth_token=2320954854-eZYObTlxJ3Pqqd0YuGNlWcRQJQ4rNAyvsfsssV7&oauth_version=1.0&oauth_signature=vAcm%2FNyc7S1RISe6jk7y8N1knMo%3D
和我的结果{"errors":[{"code":32,"message":"Could not authenticate you."}]}
告诉我为什么?
答案 0 :(得分:1)
尝试更改所有&#34; http&#34; &#34; https&#34;。