我发现我可以通过OAuth 2.0,当我重定向URI是认证为“urn:IETF:WG:OAuth的:2.0:OOB”,但用户被迫复制代码,然后返回一个活动并将其粘贴到一个字段中。我希望体验比这更优雅。当重定向URI为“http:// localhost”的,(即使是返回的接入代码)我无法换取一个访问令牌API。这里是我的交易所代码:的Oauth 2不能为接入令牌交换代码,返回“invalid_grant”
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
0);
nameValuePairs.add(new BasicNameValuePair("client_id",
OAuth2ClientCredentialsMark1.CLIENT_ID));
nameValuePairs.add(new BasicNameValuePair("client_secret",
OAuth2ClientCredentialsMark1.CLIENT_SECRET));
nameValuePairs.add(new BasicNameValuePair("code", accessCode));
nameValuePairs.add(new BasicNameValuePair("grant_type",
"authorization_code"));
nameValuePairs.add(new BasicNameValuePair("redirect_uri",
OAuth2ClientCredentialsMark1.REDIRECT_URI));
//"http://localhost"
String url = "https://accounts.google.com/o/oauth2/token";
//url += URLEncodedUtils.format(nameValuePairs, "utf-8");
Log.d("print", url);
HttpPost hPost = new HttpPost(
url);
hPost.setHeader("content-type", "application/x-www-form-urlencoded");
hPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
该代码总是返回{ “错误”: “invalid_grant”}是什么给了?
我的应用程序是基于样本@https://github.com/ddewaele/LatitudeOAuth2Sample的,我一直在关注本教程@http://code.google.com/apis/accounts/docs/OAuth2InstalledApp.html
我有同样的问题。使用Scala而不是Java,但除此之外它是相同的。我使用http:// localhost:9000作为重定向URI。 – Magnus 2012-03-07 16:23:15
我在安装的应用程序上面临OAuth的相同问题。你有没有解决这个问题?请给出意见。 – Khushboo 2013-02-05 11:28:24