我试图将现有的OAuth1.0 3L令牌迁移到OAuth2.0以获取Web应用程序。 我按照https://developers.google.com/accounts/docs/OAuth_ref 的说明尽管我尽了最大的努力,但我一直在收到此回复: “授权标头无效。”从OAuth1 3L迁移到OAuth2:
要创建授权标头,我使用Google的Java客户端库1.0,与我在应用程序中用于与Google日历交谈的相同。 我正在使用访问令牌进行测试。令牌秘密,消费者密钥和消费者秘密,这些秘密可以毫无问题地工作(即,我可以使用这些凭据拨打Google日历等)。
这是我使用的代码:
OAuthParameters oauthParameters = new OAuthParameters();
oauthParameters.setOAuthConsumerKey("www.mywebsite.com"); // this is the same consumer key used by my app normally, without problem. mywebsite is an example, the real name is different
oauthParameters.setOAuthConsumerSecret("XXXXX");
oauthParameters.setOAuthToken("YYYYY");
oauthParameters.setOAuthTokenSecret("ZZZZZZ");
OAuthHmacSha1Signer signer = new OAuthHmacSha1Signer();
OAuthHelper oauthHelper = new GoogleOAuthHelper(signer);
String requestUrl = "https://accounts.google.com/o/oauth2/token";
String header = oauthHelper.getAuthorizationHeader(requestUrl, "POST", oauthParameters);
String payload = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Amigration%3Aoauth1&client_id="+clientId+"&client_secret="+clientSecret;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(requestUrl);
httpPost.addHeader("Authorization", header);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(new ByteArrayEntity(payload.getBytes()));
String response = httpClient.execute(httpPost, new BasicResponseHandler());
,这是HttpClient的生产线迹线:
>> "POST /o/oauth2/token HTTP/1.1[\r][\n]"
>> "Authorization: OAuth realm="", oauth_signature="ixVbjINI6pgPU2RqXGiQRbPGY%3D", oauth_nonce="486642280771700", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="www.mywebsite.com", oauth_token="YYYYY", oauth_timestamp="1395127834"[\r][\n]"
>> "Content-Type: application/x-www-form-urlencoded[\r][\n]"
>> "Content-Length: 193[\r][\n]"
>> "Host: accounts.google.com[\r][\n]"
>> "Connection: Keep-Alive[\r][\n]"
>> "User-Agent: Apache-HttpClient/4.3.2 (java 1.5)[\r][\n]"
>> "[\r][\n]"
>> "grant_type=urn%ietf%params%oauth%grant-type%migration%oauth1&client_id=12345&client_secret=ABCDE"
<< "HTTP/1.1 400 Bad Request[\r][\n]"
<< "Cache-Control: no-cache, no-store, max-age=0, must-revalidate[\r][\n]"
<< "Pragma: no-cache[\r][\n]"
<< "Expires: Fri, 01 Jan 1990 00:00:00 GMT[\r][\n]"
<< "Date: Tue, 18 Mar 2014 07:30:39 GMT[\r][\n]"
<< "Content-Type: application/json[\r][\n]"
<< "X-Content-Type-Options: nosniff[\r][\n]"
<< "X-Frame-Options: SAMEORIGIN[\r][\n]"
<< "X-XSS-Protection: 1; mode=block[\r][\n]"
<< "Server: GSE[\r][\n]"
<< "Alternate-Protocol: 443:quic[\r][\n]"
<< "Transfer-Encoding: chunked[\r][\n]"
<< "[\r][\n]"
<< "5a[\r][\n]"
<< "{[\n]"
<< " "error" : "invalid_request",[\n]"
<< " "error_description" : "Invalid authorization header."[\n]"
<< "}"
<< "[\r][\n]"
<< "0[\r][\n]"
<< "[\r][\n]"
其中12345和ABCDE显然为真正的OAuth2应用凭据的占位符。
我有双重和三重检查,在OAuthParameters中设置的所有参数与当前正在使用OAuth1的正常代码使用的相同(即使使用分步调试器来验证签名时的值由OAuthHmacSha1Signer.getSignature()计算)。
我查看了当前使用OAuth1的Google客户端API发送的HTTP请求中的授权头(并且工作正常),显然除了签名,随机数和时间戳以外,它们看起来与通过此迁移调用发送的相同。
我甚至尝试过一个测试迁移请求,它失败了,然后使用一个调试器来运行旧代码并注入迁移调用使用的方法,URL,nonce和timestamp,并且由旧代码计算的签名是相同的,给予相同的参数。
为什么授权标头仍然报告为无效的任何线索?
你可以发布一个链接到你使用的源代码吗?因为我得到相同的错误艰难,我使用POST构造字符串 – walid