谷歌登錄使用新的 GoogleSignInOptions 獲取訪問令牌 (Google login get access token with new GoogleSignInOptions)


問題描述

谷歌登錄使用新的 GoogleSignInOptions 獲取訪問令牌 (Google login get access token with new GoogleSignInOptions)

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(Plus.API)
        .addScope(new Scope("profile"))
        .build();
...
...

String accessToken = GoogleAuthUtil.getToken(GoogleLoginActivity.this,
                            Plus.AccountApi.getAccountName(mGoogleApiClient),
                            "oauth2:profile email");

但現在看來,在 handleSingInResult(GoogleSignInResult result) 我只能用 result.getSignInAccount().getIdToken() 得到一個 id token ;

有誰知道是否可以從這裡獲取訪問令牌(就像以前一樣),如果可以,怎麼辦?任何幫助表示讚賞。


參考解法

方法 1:

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.

方法 2:

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

(by Bootstrapperabedfarpjapple15)

參考文件

  1. Google login get access token with new GoogleSignInOptions (CC BY‑SA 2.5/3.0/4.0)

#google-play-services #oauth-2.0 #Android #google-signin #google-oauth






相關問題

無法從 Android SDK 管理器安裝/下載 SDK (Unable to install/download SDK from Android SDK manager)

google-play-services_lib.jar bị thiếu trong thư mục lib SDK google_play_services (google-play-services_lib.jar missing from SDK google_play_services lib folder)

谷歌播放服務認證(oauth 2.0) (Google play service authentication (oauth 2.0))

在帶有 Android 的 Xamarin Studio 上使用 AdMob,Google Play 服務不存在 (Use AdMob on Xamarin Studio with Android, Google Play Services don't exist)

谷歌登錄使用新的 GoogleSignInOptions 獲取訪問令牌 (Google login get access token with new GoogleSignInOptions)

在組織內分發 Android 應用 (Distribute Android apps within organization)

如何提供啟用 GPS 的選項 (How to give option for enable GPS)

玩遊戲服務,我們能知道我們什麼時候通過另一個hiscore嗎? (Play Game Services, can we know when we pass another hiscore?)

Android 使用 Google 的位置服務對話框 (Android Use Google's location service dialog)

statusCode=SIGN_IN_REQUIRED, resolution=null 只有開發者 ID 可以登錄 else 錯誤 (statusCode=SIGN_IN_REQUIRED, resolution=null Only developer Id can Login else error)

項目中同時擁有 GMS 和 HMS (Have both GMS and HMS in the project)

谷歌cast MediaNotificationService 中的大量RemoteServiceExceptions (Large number of RemoteServiceExceptions in Google's cast MediaNotificationService)







留言討論