簡體   English   中英

你如何在android中為google apis交換OAuth2令牌的授權碼?

[英]How do you exchange Authorization code for a OAuth2 token for google apis in android?

你如何在android中為google apis交換OAuth2令牌的授權碼? 如果你去https://code.google.com/oauthplayground/你可以交換它們,但我需要能夠在 android 應用程序中做到這一點! 有任何想法嗎?

我發現了自己! :)

您需要做的就是使用授權代碼、客戶端 ID、客戶端密碼、redirect_uri 和授權類型向 accounts.google.com/o/oauth2/token 發送 HttpPost! 我將代碼添加到答案中,以便您可以准確地了解如何執行此操作! 希望能幫助到你

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM