简体   繁体   English

你如何在android中为google apis交换OAuth2令牌的授权码?

[英]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.

相关问题 如何将OAuth 2.0授权代码交换为访问令牌? - How do I exchange a OAuth 2.0 authorization code for an access token? 如何在Android中使用webView获取oAuth2令牌? - How do you obtain a oAuth2 token using webView in android? Spring-OAuth2-交换刷新和访问令牌的授权代码 - Spring - oauth2 - exchange authorization code for refresh and access tokens 部署的应用程序无法将 oauth2 代码交换为令牌 - Deployed app cannot exchange oauth2 code for token Spring Boot 2 + OAuth2:为令牌配置身份验证代码交换 - Spring Boot 2 + OAuth2: Configure Exchange of Auth Code for Token Google OAuth Java的访问令牌和ID令牌的交换代码 - Exchange code for access token and id token for google oauth java Android上的Google oauth2 - google oauth2 on android Google Oauth2-令牌调用 - Google Oauth2 - Call to Token 春季启动OAuth2-OAuth令牌不返回授权令牌 - Spring boot OAuth2 - OAuth Token does not return authorization token OAuth2 授权代码已被兑换,请使用新的有效代码重试或使用现有的刷新令牌 - OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM