Is it possible to obtain a OAuth2
token to use with Google APIs, using the android AccountManager
? The only other way I know of would be to have a webView
and make the user log in to obtain the OAuth2
token, but it would seem to be a lot of work for that many people would want to do, so if I could just obtain it using AccountManager
that would be much better. Is there any functionality like this?
AccountManager
is not for generating tokens at all - its only for storing credentials related to an Account
. These classes are not specific to Google server accounts - you can use them for any type of account. For example, I use them to store OAuth2 tokens for Facebook, Twitter, etc.
You will need to use the Google APIs to generate an OAuth2 token, which you store in an Account
using the AccountManager
. You need to use the Google APIs, because part of the OAuth2 token generation occurs on the server itself - Google will store a record of the tokens it has given out, and the server accounts that they are related to. If you didn't call the Google APIs to generate the token, Google wouldn't know how to match the token to a Google server account.
Your approach of using a WebView
to obtain the OAuth2 token is the correct way to do this.
Once you have the OAuth2 token stored for the Account
, you just retrieve it from the AccountManager
whenever you want to use it to make a query. So, you only need to generate the OAuth2 token once (unless it expires), and then you keep using it over and over again.