25987

How can I access the Google account user_id?

Question:

I'd like to access the user's Google account <a href="https://developers.google.com/accounts/docs/OAuth2Login" rel="nofollow">user_id</a> for <a href="https://developers.google.com/accounts/" rel="nofollow">authentication</a>, but I don't see any mention of this in the <a href="http://developer.android.com/reference/android/accounts/AccountManager.html" rel="nofollow">AccountManager</a>.

How can my app request the user_id?

Answer1:

Now that <a href="https://developers.google.com/android/google-play-services/reference/com/google/android/gms/auth/package-summary" rel="nofollow">Google Play Services</a> 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.

Answer2:

You will need to use the Android AccountManager's <a href="http://developer.android.com/reference/android/accounts/AccountManager.html#getAuthToken%28android.accounts.Account,%20java.lang.String,%20android.os.Bundle,%20android.app.Activity,%20android.accounts.AccountManagerCallback%3Candroid.os.Bundle%3E,%20android.os.Handler%29" rel="nofollow">getAuthToken</a> API until Google Play services is released.

Here's an example of how you can use getAuthToken to obtain an access_token: <a href="https://stackoverflow.com/a/10988589/313790" rel="nofollow">https://stackoverflow.com/a/10988589/313790</a>

Answer3:

Check out Google's <a href="https://developers.google.com/google-apps/tasks/oauth-and-tasks-on-android" rel="nofollow">AccountManager example for the Tasks API</a>.

Once you have an access token, you then instead of using the Tasks library, use the <a href="https://code.google.com/p/google-api-java-client/wiki/APIs#oauth2" rel="nofollow">oauth2 library</a> of google-api-java-client to request a <a href="http://javadoc.google-api-java-client.googlecode.com/hg/apis/oauth2/v2/index.html" rel="nofollow">Userinfo</a> object like in <a href="https://code.google.com/p/google-api-java-client/source/browse/oauth2-cmdline-sample/src/main/java/com/google/api/services/samples/oauth2/cmdline/OAuth2Sample.java?repo=samples" rel="nofollow">this example</a>:

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

Recommend

  • How to compare strings in google apps script
  • Gathering specific pairs of columns into rows by dplyr in R [duplicate]
  • How unique a 5-digit mt_rand() number is?
  • Add relation to another node in SDN4 + REST
  • Handlers initialized with Looper.getMainLooper() does not respond to message callbacks
  • Team Foundation Server: Assign work item to a group instead of an individual user
  • Read certain line in text file and display the next
  • hsqldb ignores first insert operation on table at server (server needs to be “warmed up”?)
  • Generating pdfs with jsPDF compatibility issues in Firefox?
  • Is there a way to continuously pipe data from Azure Blob into BigQuery?
  • Pass variable to a detail view controller using a table in UIViewController (not UITableViewControll
  • Ctypes: fast way to convert a return pointer to an array or Python list
  • An AppBar application not working correctly in Windows 8.x/10 (desktop working area does not get red
  • Lookup song by ISRC in Apple Music/iTunes
  • WM_POWERBROADCAST not received by message-only window in Windows XP
  • about iOS target version and SDK version
  • c++ plugin : Is it ok to pass polymorphic objects?
  • Search image on Google images with the new Custom Search API?
  • How to achieve density/heat map effect in iOS (iPhone/iPad)?
  • Parsing a complicated array with GetJSON Jquery
  • Intersection of characters in two strings
  • Alternative to bcrypt when saving passwords in PHP 5.2
  • Angular 6 illegal operation on a directory, open '/Users//.npm-global/lib/node_modules/
  • Tensorflow converging but bad predictions
  • Django self join , How to convert this query to ORM query
  • jQuery colorbox breaks postbacks in ASP.NET Web Forms
  • Getting/building the SQL (with parameters) from NHibernate 3.2
  • How can I do case insensitive string search with Linq and SQL Server?
  • Showing image on a acro text field position
  • how to add dashed border on highcharts “area” graph for every point
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Set WebClient.Builder.exchangeStrategies() without losing Spring Jackson configuration
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • How to split wav file into two or more parts using c#
  • Calculate time from document
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • Functions by reference or by variable, which to use when?
  • How to encrypt Connectionstring written in web.config from codebehind?