[英]How do you exchange Authorization code for a OAuth2 token for google apis in android?
How do you exchange Authorization code for a OAuth2 token for google apis in android?你如何在android中为google apis交换OAuth2令牌的授权码? If you go to https://code.google.com/oauthplayground/ you can exchange them, but I need to be able to do it in a android app!
如果你去https://code.google.com/oauthplayground/你可以交换它们,但我需要能够在 android 应用程序中做到这一点! Any ideas?
有任何想法吗?
I found out myself!我发现了自己! :)
:)
All you need to do is make a HttpPost to accounts.google.com/o/oauth2/token with the Authorization Code, client id, client secret, redirect_uri and grant type!您需要做的就是使用授权代码、客户端 ID、客户端密码、redirect_uri 和授权类型向 accounts.google.com/o/oauth2/token 发送 HttpPost! I added the code to the answer so you can see how to do it exactly!
我将代码添加到答案中,以便您可以准确地了解如何执行此操作! Hope it helps
希望能帮助到你
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.