2
如何在android中为谷歌apis交换OAuth2令牌的授权码?如果你去https://code.google.com/oauthplayground/你可以交换他们,但我需要能够在Android应用程序中做到这一点!有任何想法吗?如何在android中为Google apis交换OAuth2令牌的授权码?
如何在android中为谷歌apis交换OAuth2令牌的授权码?如果你去https://code.google.com/oauthplayground/你可以交换他们,但我需要能够在Android应用程序中做到这一点!有任何想法吗?如何在android中为Google apis交换OAuth2令牌的授权码?
我发现了我自己! :)
所有你需要做的是使HttpPost与授权码,客户端ID,客户端密钥,REDIRECT_URI并授予类型accounts.google.com/o/oauth2/token!我将代码添加到答案中,以便您可以看到如何完全做到这一点!希望它可以帮助
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("code", "<your_auth_code>"));
pairs.add(new BasicNameValuePair("client_id", "<your_client_id>"));
pairs.add(new BasicNameValuePair("client_secret", "<your_client_secret>"));
pairs.add(new BasicNameValuePair("redirect_uri", "<your_redirect_uri>"));
pairs.add(new BasicNameValuePair("grant_type", "authorization_code")); //Leave this line how it is
post.setEntity(new UrlEncodedFormEntity(pairs));
org.apache.http.HttpResponse response = client.execute(post);
String responseBody = EntityUtils.toString(response.getEntity());
Log.v("message", responseBody); // That just logs it into logCat
此外,还可以使用的AccountManager用“的oauth2:范围” tokenType获得大多数服务令牌,而无需手动验证(它使用谷歌帐户的设备上注册)。 –
哈哈我花了最长的时间试图让客户经理工作,但它不适用于goo.gl,所以这就是为什么我必须使用webView。 –
正确,有点半支持 - 与一些服务合作,但与其他服务不兼容。 –