Access youtube account with accountmanager

淺唱寂寞╮ 提交于 2019-11-28 22:11:14

you're in luck as I just finished cracking this problem on the app I'm developing.

AccountManager.get(getApplicationContext()).getAuthTokenByFeatures("com.google", "oauth2:https://gdata.youtube.com", null, this,
    null, null, new AccountManagerCallback<Bundle>() {

        @Override
        public void run(AccountManagerFuture<Bundle> future) {
        try {
            Bundle bundle = future.getResult();
            String acc_name = bundle.getString(AccountManager.KEY_ACCOUNT_NAME);
            String auth_token = bundle.getString(AccountManager.KEY_AUTHTOKEN);

            Log.d(DEBUG_TAG, "name: " + acc_name + "; token: " + auth_token);

        } catch (Exception e) {
            Log.e(DEBUG_TAG, e.getClass().getSimpleName() + ": " + e.getMessage());
        }
        }
    }, null);

I haven't used it myself in an app yet, and it might not be available on all Android devices, but my understanding is that the Google Play services now provides the best approach to getting OAuth 2 tokens (including those scoped to https://gdata.youtube.com). There's more info at

http://android-developers.blogspot.com/2012/09/google-play-services-and-oauth-identity.html

You could go with the AccountManager approach for wider compatibility, though.

The confusing part is the authTokenType parameter, it must be complete with the spec for OAuth2 access, e.g.

"oauth2:https://gdata.youtube.com"

or

"oauth2:https://www.googleapis.com/auth/tasks"

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