请熟悉oauth协议的进来看看,小弟有疑惑请教
最近因项目原因需要用到oauth协议通信yahoo,大体步骤此处说来不甚清楚,可参考以下网址http://developer.yahoo.com/oauth/guide/oauth-auth-flow.html
其中oauth_signature_method 有3种方法,PLAINTEXT小弟试来没有问题,可用HMAC-SHA1的话却不行。以第二步Get a Request Token为例,此为GET请求
1.https://api.login.yahoo.com/oauth/v2/
2. get_request_token?oauth_nonce=ce2130523f788f313f76314ed3965ea6
3. &oauth_timestamp=1202956957
4. &oauth_consumer_key=123456891011121314151617181920
5. &oauth_signature_method=HMAC-SHA1
6. &oauth_signature=abcdef
7. &oauth_version=1.0
8. &xoauth_lang_pref="en-us"
9. &oauth_callback="http://yoursite.com/callback"
HMAC-SHA1算法需要2个参数data及key ,本例中data为GET&+转义后的有效URL+&+转义后的所有参数除(&oauth_signature)拼接成的字符串(此处有点疑问,不知道是否如我所想);key为Shared Secret + & + token secret 代码如下:
class Signature {
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
public static String calculateRFC2104HMAC(String data, String key)
throws java.security.SignatureException {
String result;
try {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),
HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes());
result = new String(Base64.encodeBase64(rawHmac));
// result = Base64.encodeBytes(rawHmac);
// result = Base64.encodeBase64String(rawHmac);
} catch (Exception e) {
throw new SignatureException("Failed to generate HMAC : "
+ e.getMessage());
}
return result;
}
最后让 &oauth_signature= Signature.calculateRFC2104HMAC(String data, String key). 再提交请求。。。
自己看自己说的都很乱。。。。