我想为我的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并让它为我处理会话。
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。鉴于这个没有答案的问题没有解决,我不建议将两个问题合并。
老实说,我很沮丧,欢迎所有贡献。
答案 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();