Android - how to get google plus access token?

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

Hello i am getting google plus access token without using OAuth 2.0 client ID with scopes. But with this access token does not fetch email address. How to fetch user email address?

Is there any difference between accesstoken with and without OAuth 2.0 client ID?

I have used following code,

String accessToken="";                     try {                         accessToken = GoogleAuthUtil.getToken(                                 getApplicationContext(),                                 mPlusClient.getAccountName(), "oauth2:"                                         + Scopes.PLUS_LOGIN + " "                                         + Scopes.PLUS_PROFILE);                          System.out.println("Access token==" + accessToken);                     } catch (Exception e) {                         e.printStackTrace();                     } 

回答1:

There are 2 simple ways to get user Email from Google plus,

1.Through Plus.AccountApi.getAccountName like below,

String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

2.Through plus.profile.emails.read scope and REST end point like below,

Get the GooglePlus AccessToken

You need to pass " https://www.googleapis.com/auth/plus.profile.emails.read" this scope to get the AccessToken from GooglePlus like below,

accessToken = GoogleAuthUtil.getToken(                                 getApplicationContext(),                                 mPlusClient.getAccountName(), "oauth2:"                                         + Scopes.PLUS_LOGIN + " "                                         + Scopes.PLUS_PROFILE+" https://www.googleapis.com/auth/plus.profile.emails.read"); 

Make a REST call to the endpoint and do simple JSON parsing

https://www.googleapis.com/plus/v1/people/me?access_token=XXXXXXXXXXXXX

You must declare the permission in your AndroidManifest.xml to use these methods.

Full Example from Google Developer site,

Do something like below to retrieve the authenticated user's Email from Google plus,

class UserInfo {   String id;   String email;   String verified_email; }  final String account = Plus.AccountApi.getAccountName(mGoogleApiClient);  AsyncTask task = new AsyncTask() {    @Override   protected UserInfo doInBackground(Void... params) {     HttpURLConnection urlConnection = null;      try {       URL url = new URL("https://www.googleapis.com/plus/v1/people/me");       String sAccessToken = GoogleAuthUtil.getToken(EmailTest.this, account,         "oauth2:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read");        urlConnection = (HttpURLConnection) url.openConnection();       urlConnection.setRequestProperty("Authorization", "Bearer " + sAccessToken);        String content = CharStreams.toString(new InputStreamReader(urlConnection.getInputStream(),           Charsets.UTF_8));        if (!TextUtils.isEmpty(content)) {         JSONArray emailArray =  new JSONObject(content).getJSONArray("emails");          for (int i = 0; i 

Fore more info read this

https://developers.google.com/+/mobile/android/people



回答2:

String accessToken = ""; try {   URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo");   // get Access Token with Scopes.PLUS_PROFILE   String sAccessToken;   sAccessToken = GoogleAuthUtil.getToken(     LoginCheckActivity.this,     mPlusClient.getAccountName() + "",     "oauth2:"       + Scopes.PLUS_PROFILE + " "       + "https://www.googleapis.com/auth/plus.login" + " "       + "https://www.googleapis.com/auth/plus.profile.emails.read"); } catch (UserRecoverableAuthException e) {   // TODO Auto-generated catch block   e.printStackTrace();                     Intent recover = e.getIntent();   startActivityForResult(recover, 125);   return ""; } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace(); } catch (GoogleAuthException e) {   // TODO Auto-generated catch block   e.printStackTrace(); } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!