Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

oauth 2.0 - Android - how to get google plus access token?

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();
                    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 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<Void, Void, String> task = new AsyncTask<Void, Void, String>() {

  @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 < emailArray.length; i++) {
          JSONObject obj = (JSONObject)emailArray.get(i);

          // Find and return the primary email associated with the account
          if (obj.getString("type") == "account") {
            return obj.getString("value");
          }
        }
      }
    } catch (UserRecoverableAuthException userAuthEx) {
      // Start the user recoverable action using the intent returned by
      // getIntent()
      startActivityForResult(userAuthEx.getIntent(), RC_SIGN_IN);
      return;
   } catch (Exception e) {
      // Handle error
      // e.printStackTrace(); // Uncomment if needed during debugging.
    } finally {
      if (urlConnection != null) {
        urlConnection.disconnect();
      }
    }

    return null;
  }

  @Override
  protected void onPostExecute(String info) {
      // Store or use the user's email address
  }

};

task.execute();

Fore more info read this

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...