androidgoogle-plus-signinandroid-accountgoogle-identity

Auth.GoogleSignInApi as GoogleAuthUtil replacement for google plus sign in


My goal is to log into a BaaS with the Google plus sdk, the BaaS requires a tokenId via googleSignInAccount.getIdToken() and an accessToken.

GoogleAuthUtil.getToken(activity, accountName, scopes); is depreciated, and was used to get the accessToken.

GoogleAuthUtil.invalidateToken(activity, token); is also depreciated.

What is the alternative way to get the accessToken and invalidate(is invalidation needed now)?

I am aware of and have tried: GoogleAuthUtil.getToken(Context, Account, String); and GoogleAuthUtil.clearToken(activity, token);

Account seems to require a first part permission(thus not an option) Ex. account = new Account(googleSignInAccount.getEmail(), "com.google"); nor do I know if clear is synonymous with invalidate(seems to work with the depreciated method however, I don't know the repercussions), or if there are some other call/api's to use.

Like from:com.google.android.gms.auth.api.Auth


Solution

  • Google developer site has a page on migrating from GoogleAuthUtil.getToken

    https://developers.google.com/identity/sign-in/android/migration-guide

    This code is no longer complaiming of using depreciated API

        private void loginInBackendless(final GoogleSignInAccount acct) {
        Log.d(TAG, "handleSignInResult: try login to backendless");
    
        //final MainActivity mainActivity = (MainActivity)this.getActivity();
        //final String accountName = acct.getEmail();
        final String scopes = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME + " " + Scopes.PROFILE + " " + Scopes.EMAIL;
        final FragmentActivity fragmentActivity = this;
        AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String token = null;
                try {
                    token = GoogleAuthUtil.getToken(fragmentActivity, acct.getAccount(), scopes);
                    GoogleAuthUtil.clearToken(fragmentActivity, token);
                    handleAccessTokenInBackendless(acct.getIdToken(), token);
                } catch (UserRecoverableAuthException e) {
                    startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return token;
            }
        };
    
        task.execute();
    }