How can I access the Google account user_id?

狂风中的少年 提交于 2019-12-07 00:12:42
Jason Hall

Now that Google Play Services is available, you can use it to request the user's permission to access the https://www.googleapis.com/auth/userinfo.profile scope and, with the resulting access token, make a request to https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken} to get their user ID.

Chirag Shah

You will need to use the Android AccountManager's getAuthToken API until Google Play services is released.

Here's an example of how you can use getAuthToken to obtain an access_token: https://stackoverflow.com/a/10988589/313790

Jan Gerlinger

Check out Google's AccountManager example for the Tasks API.

Once you have an access token, you then instead of using the Tasks library, use the oauth2 library of google-api-java-client to request a Userinfo object like in this example:

Oauth2 oauth2 = new Oauth2.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
   .setApplicationName("Google-OAuth2Sample/1.0").build();
Userinfo userinfo = oauth2.userinfo().get().execute();
String userId = userinfo.getId();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!