0

概括

我想为我的一个 Android 应用程序实现 Google 服务登录,并且在大多数情况下,通过这个有用的教程的指导,它运行良好。我正在探索使用MongoDB Stitch作为我的后端,我想让 Stitch 成为我的数据库和客户端之间的代理。Stitch 提供了这个不太有用的教程

问题

我目前被以下问题阻止:

@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. 确保我没有像这篇文章那样打错字。
  4. requestServerAuthCode通过在 Builder 中调用显式请求服务器验证码,如下所示:
    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();

可以在此处找到类似的问题。鉴于这个问题没有答案没有得到解决,我认为合并这两个问题是不可取的。

老实说,我很难过,欢迎所有贡献。

4

2 回答 2

0

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

用这个

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

于 2020-03-26T05:49:58.883 回答
0

确保您使用的是 serverClientId

String serverClientId = getString(R.string.server_client_id);
于 2020-02-09T12:26:28.170 回答