Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

android - Access youtube account with accountmanager

Im trying to access youtube account with account manager, meaning i want to access youtube with a account linked to my device and with this get youtube token to access user playlist and whatever. Freedi application for android doing somthing like this.

I used this code to get token

    am.getAuthToken(accounts[0], "youtube", true, new AccountManagerCallback<Bundle>() {

        @Override
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                Bundle bundle = future.getResult();
                if (bundle.containsKey(AccountManager.KEY_INTENT)) {
                    Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT);
                    intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivityForResult(intent, 0);
                } else if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
                    my_access_token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                    onActivityResult(0,1,null);
                }
            } catch (Exception e) {
                Log.e("TEST", e.getMessage(), e);
            }
        }
    }, null);

but the var my_access_token filled with token that youtube not recognize... how can i get youtube token from it? and how to get the user playlist?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...