私のAndroidアプリは現在、GoogleAuthUtilを使用してユーザーにサインインし、バックエンドに渡される_access_token
_をフェッチします(以下のコードスニペットは、GoogleApiClientの作成とGoogleAuthUtilを使用したトークンの取得を示しています) 。
_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");
_
その後、バックエンドに送信しました
新しいGoogleサインインに移行しようとしています https://developers.google.com/identity/sign-in/Android/sign-in
そのため、GoogleApiClientの作成を次のように変更しました。
_GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken("<web client id>")
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
_
次に、ログインを使用するには、
_startActivityForResult(Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient), RC_GET_TOKEN);
_
アクティビティ結果の使用(上記のリンクの例と同様)では、
_OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign-in");
handleSignInResult(opr.get());
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgress();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgress();
handleSignInResult(googleSignInResult);
}
});
}
_
しかし、今ではhandleSingInResult(GoogleSignInResult result)
ではresult.getSignInAccount().getIdToken();
でしか_id token
_を取り戻すことができないようです
これから(以前のように)アクセストークンを取得できるかどうか、そして可能であればどのように取得できるかを誰かが知っていますか?助けていただければ幸いです。
サインインすると、トークンを取得できるようになります。
final String token = GoogleAuthUtil.getToken(mAppContext, mAccountName, AUTH_TOKEN_TYPE);
それを非同期タスクにすることを忘れないでください。詳細については、 ここ をご覧ください。
編集:
メソッド名にもかかわらず、次の点に注意してください。
GoogleAuthUtil.getToken()
OAuthトークンを提供するのではなく、 ドキュメント に従って「短期間の認証コード」を返します。
GoogleAuthUtil.getToken()を呼び出して認証コードを取得した後はどうすればよいですか?
HTTPS経由で認証コードをバックエンドサーバーに送信する必要があります。アプリではなく、サーバーからのみアクセストークンや更新トークンの受信を試みる必要があります。
だから私は同じ問題を抱えていました。彼らは今それを変更したので、トークンは
GoogleSignInAccount acct = result.getSignInAccount();
Log.d(TAG, "handleSignInResult2: "+acct.getIdToken());
このトークンにアクセスするには、あなたもそれを要求します
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail().requestProfile().requestId().requestIdToken(getString(R.string.server_client_ID))
.build();
R.string.server_client_ID
それは client ID
Google DeveloperConsoleで作成したプロジェクトから。
これがお役に立てば幸いです。
これは私が従ったドキュメントでもあります。 https://developers.google.com/identity/sign-in/Android/backend-auth