成功登录后,GoogleSignInAccount getServerAuthCode返回null

时间:2019-02-27 06:02:25

标签: java android mongodb mongodb-stitch

摘要

我想为我的Android应用实现Google Service登录,并且在大多数情况下,在this helpful tutorial的指导下,它一直运行良好。我正在探索使用MongoDB Stitch作为后端,我想让Stitch成为我的数据库和客户端之间的代理。 Stitch提供了此not-so-helpful tutorial

问题

我目前被以下问题所阻止:

@Override
protected void onCreate(Bundle bundle) {
    GoogleSignInOptions gso = 
        new GoogleSignInOptions
            .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

        // Build a GoogleSignInClient with the options specified by gso.
    GoogleSignInClient googleSignInClient 
        = GoogleSignIn.getClient(this, gso);

    findViewById(R.id.signinwithgooglebutton)
        .setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i(TAG, "Initiated Google onClick.");
                Intent signInIntent = 
                    googleSignInClient.getSignInIntent();
                startActivityForResult(signInIntent, RC_SIGN_IN);
        });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = 
                GoogleSignIn.getSignedInAccountFromIntent(data);

            final GoogleCredential googleCredential = 
                new GoogleCredential(account.getServerAuthCode());
                                     // ^~~ returns empty String
            return;
        }
    }

预期行为

account.getServerAuthCode()应该返回服务器身份验证代码,以便我可以将其传递给Stitch并让它为我处理会话。

尝试

  1. 刷新的oauth2-google客户机密。
  2. 为Android应用设置SHA-1签名密钥。
  3. 确保我没有打this post这样的错字。
  4. 通过在Builder中调用requestServerAuthCode明确请求服务器身份验证代码,如下所示:
    GoogleSignInOptions gso = 
        new GoogleSignInOptions
            .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestServerAuthCode(getResources()
                .getString(R.string.server_client_id)) 
                     // ^~~ gets server_client_id from string.xml
             .build();

可以找到类似的问题here。鉴于这个没有答案的问题没有解决,我不建议将两个问题合并。

老实说,我很沮丧,欢迎所有贡献。

2 个答案:

答案 0 :(得分:0)

确保您使用的是serverClientId

String serverClientId = getString(R.string.server_client_id);

答案 1 :(得分:0)

如果要使用getServerAuthCode(),请更改对象创建方法。

使用此

GoogleSignInOptions gso =新的GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestServerAuthCode(serverClientId) .requestEmail() .build();