新GoogleSignInOptions谷歌登录获取访问令牌 [英] Google login get access token with new GoogleSignInOptions

查看:5449
本文介绍了新GoogleSignInOptions谷歌登录获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用目前使用的GoogleAuthUtil来签到的用户,并获取了的access_token 被传递到后端(低于code代码展示了创建GoogleApiClient和使用GoogleAuthUtil获得令牌)。

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 the GoogleApiClient and using GoogleAuthUtil to get the token).

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");

然后我发送给后端

which I then sent to the backend

我现在正在试图移动到新的谷歌登入 - 的https: //developers.google.com/identity/sign-in/android/sign-in

I am now trying to move to the new Google SignIn - https://developers.google.com/identity/sign-in/android/sign-in

和改变,以便创造GoogleApiClient一样,

and so changed the GoogleApiClient creation like,

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();

,然后做登录使用,

and then to do the login use,

startActivityForResult(Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient), RC_GET_TOKEN);

和对活动结果的使用(类似于上面的链接的例子),

and on activity result use (similar to the example in the link above),

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结果)我只能得到一个 ID令牌回来
result.getSignInAccount()getIdToken();

有谁知道是否有可能从该得到一个访问令牌(如previously),如果又如何?任何帮助AP preciated。

Does anyone know if it is possible to get an access token from this (like previously) and if so how? Any help appreciated.

推荐答案

在你签约之后就可以得到令牌:

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

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

不要忘了做一个AsyncTask的。对于更详细的看一下<一个href=\"https://github.com/google/iosched/blob/2531cbdbe27e5795eb78bf47d27e8c1be494aad4/android/src/main/java/com/google/samples/apps/iosched/util/LoginAndAuthHelper.java\"相对=nofollow>这里

编辑:

请注意的是,尽管该方法的名称:

Note that, despite the method name:

GoogleAuthUtil.getToken()

它不会给你一个OAuth令牌,它,而返回根据的文档

通过调用获得授权code后,我应该做什么
  GoogleAuthUtil.getToken()?

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

您应该传送授权code通过HTTPS后端服务器。只有从你的服务器,你应该尝试接受访问和/或刷新令牌,而不是在您的应用程序。

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.

这篇关于新GoogleSignInOptions谷歌登录获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆