Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google account get Token

I have a problem with kitkat api while tringy to get access token of google account services, google music in my case. So, if user trying get token at first by using next method:

 public String getAuthToken(Account account)
            throws AuthenticatorException, IOException {
        String s1;
        if (account == null) {
            Log.e("MusicAuthInfo", "Given null account to MusicAuthInfo.getAuthToken()", new Throwable());
            throw new AuthenticatorException("Given null account to MusicAuthInfo.getAuthToken()");
        }
        String s = getAuthTokenType(mContext);
        try {
            s1 = AccountManager.get(mContext).blockingGetAuthToken(account, s, true);
        } catch (OperationCanceledException operationcanceledexception) {
            throw new AuthenticatorException(operationcanceledexception);
        }
        if (s1 == null) {
            throw new AuthenticatorException("Received null auth token.");
        }

        return s1;
    }

here i get s1 == null and the system push notification:

notification

When user tap on notification, next dialog appear:

dialog

When user click "ok", all next iterations getting token get success.

Question: How to circumvent this confirmation or show just dialog, without click to notification ?

like image 491
smail2133 Avatar asked Nov 07 '14 10:11

smail2133


People also ask

How do I get my Google ID token?

An ID token is available when a Credential object's user ID matches the user ID of a Google account that is signed in on the device. To sign in with an ID token, first retrieve the ID token with the getIdTokens method. Then, send the ID token to your app's backend.

How do I use my Google ID token?

To sign in or sign up a user with an ID token, send the token to your app's backend. On the backend, verify the token using either a Google API client library or a general-purpose JWT library. If the user hasn't signed in to your app with this Google Account before, create a new account.


1 Answers

It's not a direct answer to your question, but you can use Google Play Services instead.

String token = GoogleAuthUtil.getToken(context, userEmail, "oauth2:https://mail.google.com/");

You just have to specify the oauth2 scope you need. For instance for Google+ you would need "https://www.googleapis.com/auth/plus.login" instead of what I post in the snippet for Gmail. You can also specify multiple scopes in one token request. The permission request pops up right away.

You can read all about it here: Authorizing with Google for REST APIs, Login scopes

like image 69
Chris Avatar answered Sep 19 '22 16:09

Chris