Google SignIn获取访问令牌

时间:2016-01-14 12:39:19

标签: android google-login

如何获取google sign的访问令牌

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail().requestIdToken(getString(R.string.server_client_id))
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

in

  GoogleSignInAccount acct = result.getSignInAccount();

没有获取访问令牌的选项

1 个答案:

答案 0 :(得分:-1)

我偶然发现同样的问题,使用此引用解决了:why is requestIdToken returning null?

你也可以尝试我的这个片段:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken("XXXX") //fill this with reference to string value of web client OAuth 2.0 client IDs on https://console.developers.google.com/apis/ 
        .requestEmail() //Remove these below according to your needs
        .requestProfile()
        .requestScopes(new Scope(Scopes.PROFILE))
        .requestScopes(new Scope(Scopes.PLUS_ME))
        .requestScopes(new Scope(Scopes.PLUS_LOGIN))
        .build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this, this)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .addApi(Plus.API)
        .build();

关于活动结果:

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            GoogleSignInAccount acct = result.getSignInAccount();

            if(acct!=null){
                personName = acct.getDisplayName();
                personEmail = acct.getEmail();
                personPhotoUrl = acct.getPhotoUrl().toString();
                String personKey = acct.getIdToken().toString(); //You'll get the token here
}else{

                Toast.makeText(getApplicationContext(),"Login failed!",Toast.LENGTH_LONG).show();
            }
        }
    }

注意:RC_SIGN_IN = 9001;