How can I access the Google account user_id?

一笑奈何 提交于 2020-01-03 04:35:11

问题


I'd like to access the user's Google account user_id for authentication, but I don't see any mention of this in the AccountManager.

How can my app request the user_id?


回答1:


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.




回答2:


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




回答3:


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();


来源:https://stackoverflow.com/questions/12396062/how-can-i-access-the-google-account-user-id

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