我发现当我的重定向uri是“urn:ietf:wg:oauth:2.0:oob”时,可以通过OAuth 2.0进行身份验证,但用户被强制复制代码,然后返回上一个活动并将其粘贴到一个字段中。我希望这种体验更加优雅。当重定向uri为“http://localhost”时(即使返回访问代码),我无法将其交换为api的访问令牌。以下是我的交换代码:
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));
这段代码总是返回 {"error" : "invalid_grant"},怎么回事?
我的应用程序基于示例@https://github.com/ddewaele/LatitudeOAuth2Sample,我一直在按照教程@http://code.google.com/apis/accounts/docs/OAuth2InstalledApp.html操作。