问题
I am working on an app that will need to get authorization via OAuth2 from a 3rd party web service (3rd party meaning it's not Google and I don't own it).
This article:
http://developer.android.com/training/id-auth/authenticate.html
seems to suggest that I should be using AccountManager for this purpose. After giving this some thought, I have some doubts about the benefit of doing this, or even if it's appropriate for me to do this. If I wanted to use a Google account, or some other account that was already installed into AccountManager by some other app, then obviously it would be a good idea to get the credentials from AccountManager. But since I am going to use an account that is most likely not in AccountManager, I would have to do all the work to get it installed.
Does AccountManager provide any support in actually handling OAuth2 requests? If it doesn't, then what do I gain by using it?
And since I don't own the web service associated with this account type, is it even appropriate for me to be installing such accounts into AccountManager?
Thank you!
回答1:
This might be a rather late answer after all these 4 years, but let me give you a short reply.
You cannot and should not be installing third-party accounts for Oauth yourself. It is the job of those third-party OAuth providers such as Facebook or Twitter to implement AccountManager functionality and create their own account type. This is roughly guided at https://developer.android.com/training/id-auth/custom_auth.html.
There are several services, including OAuth providers such as Facebook, Twitter, WeChat, and etc, who register user accounts in AccountManager but I believe most of them just use it to implement SyncAdapter (which requires Account), not to provide OAuth functionality to third-party applications like your app.
I think Google allows you to use their APIs using the token acquired through AccountManager; the link you provide gives an example of using AccountManager for Tasks API. However, using the client library is a better option of achieving the same thing as described in https://developers.google.com/google-apps/tasks/quickstart/java#step_3_configure_the_project_build.
If the third-party OAuth provider does not provide you any SDKs or client libraries, you have no other choice but to use REST APIs they provide.
来源:https://stackoverflow.com/questions/16270904/using-android-accountmanager-for-3rd-party-oauth2-authorization