1

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?

gilush14
  • 485
  • 1
  • 9
  • 20

3 Answers3

7

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);
Budius
  • 39,391
  • 16
  • 102
  • 144
  • 1
    I guess it's, although I'm planning to always ask from the system just to 'have it fresh' from the system... even if it's the same. Also be careful that I couldn't find a way to 'de-authorise' ... even if I uninstall the app, I guess I'll have to factory reset phone to test the process again... which is a bit weird. If you find a way of doing it, let me know. – Budius Oct 11 '12 at 14:42
  • 1
    oh... and if you want for different google services it's just a matter of changing this String: `oauth2:https://gdata.youtube.com` to the correct one, for example calendar `oauth2:https://www.googleapis.com/auth/calendar.readonly` – Budius Oct 11 '12 at 15:08
  • I guess it finishes whenever it executes `public void run(AccountManagerFuture future) {} ` so inside there you can save your token, have a listener, call runOnUiThread, or something. – Budius Oct 11 '12 at 15:31
  • thanks budius. what do you that you always ask from the system just to 'have it fresh'? i can't use the token the next day? i always need to ask the user to confirm youtube account permission? – gilush14 Oct 11 '12 at 15:34
  • invalidateAuthToken("com.google", auth_token); allow you the validate the token again. Thanks for answers :) Subject close – gilush14 Oct 11 '12 at 16:26
1

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.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
0

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"

Meanman
  • 1,474
  • 20
  • 17