在Android上使用谷歌ID令牌

时间:2017-09-14 16:09:44

标签: android google-account

我能够通过GoogleSignInAccount.getIdToken()收到Google ID令牌,因此我对如何处理它几乎没有疑问

  1. 令牌大约是1kb字符串。我不能用每个服务器请求发送它。那么我在服务器端如何验证它的正确方法是什么?
  2. 我看不到刷新ID令牌的方法。这会在GoogleSignInAccount类中自动发生吗?
  3. 谷歌方面的令牌验证是否有任何限制(配额)?

1 个答案:

答案 0 :(得分:0)

1.为了不使服务器过载,我们决定生成具有相同到期时间(1小时)的内部(短)访问令牌

2.再次调用登录功能可以实现提示刷新:

private void loginGoogle(){
        //context is Activity
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(context.getString(R.string.default_web_client_id)).requestEmail()
                .build();
        if (googleApiClient!=null) {
            googleApiClient.stopAutoManage((FragmentActivity) context);
            googleApiClient.disconnect();
        }
        googleApiClient = new GoogleApiClient.Builder(context)
                .enableAutoManage((FragmentActivity) context, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Log.d("auth", "connection failed");
                    }
                })
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
        //
        OptionalPendingResult<GoogleSignInResult> opr =
                Auth.GoogleSignInApi.silentSignIn(googleApiClient);
        if (opr.isDone()) {
            GoogleSignInResult r = opr.get();
            Log.d("auth", "google silent signin sync");
            fillGoogleProfile(r.getSignInAccount());
        } else {
            opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                @Override
                public void onResult(@NonNull GoogleSignInResult result) {
                    Log.d("auth", "google silent signin async");
                    if (result.getSignInAccount()==null) {
                        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
                        //catch result in the onActivityResult
                        context.startActivityForResult(signInIntent, RC_SIGN_IN);
                        return;
                    }
                    fillGoogleProfile(result.getSignInAccount());
                }
            });
        }
    }

3.还没有找到答案