Google login get access token with new GoogleSignInOptions

前端 未结 2 1715
余生分开走
余生分开走 2021-02-14 18:08

My android app currently uses the GoogleAuthUtil to signin users and fetch an access_token which is passed to the backend (code snippets below which show creating t

相关标签:
2条回答
  • 2021-02-14 18:54

    So i was having the same problem. they have changed it now so the token comes through in

    GoogleSignInAccount acct = result.getSignInAccount();
    Log.d(TAG, "handleSignInResult2: "+acct.getIdToken());
    

    To get access too this token you have too ask for it in

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail().requestProfile().requestId().requestIdToken(getString(R.string.server_client_ID))
                        .build();
    

    The R.string.server_client_ID is the client ID from the project that you make in your Google developer Console.

    I hope this helps you.

    here is also the documentation i followed. https://developers.google.com/identity/sign-in/android/backend-auth

    0 讨论(0)
  • 2021-02-14 18:57

    After signing in you'll be able to get the token:

    final String token = GoogleAuthUtil.getToken(mAppContext, mAccountName, AUTH_TOKEN_TYPE);
    

    dont forget to do it an Asynctask. for more detail have a look at here

    EDIT:

    Note that, despite the method name:

    GoogleAuthUtil.getToken()
    

    it does not give you an OAuth Token, it rather returns a "short-lived authorization code" according to the documentation.

    What I should do after getting the Authorization Code by calling the GoogleAuthUtil.getToken() ?

    You should transmit the Authorization Code to your backend server over HTTPS. Only from your server you should attempt to receive Access and/or Refresh token, not in your app.

    0 讨论(0)
提交回复
热议问题