Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android GoogleSignInAccount getIdToken()为空_Android_Firebase_Firebase Authentication_Google Login - Fatal编程技术网

Android GoogleSignInAccount getIdToken()为空

Android GoogleSignInAccount getIdToken()为空,android,firebase,firebase-authentication,google-login,Android,Firebase,Firebase Authentication,Google Login,嘿,我正在使用Firebaseauth,并启用了谷歌登录功能。 谷歌登录运行良好,我得到了所有信息。但是当我想在Firebase用户列表中保存用户时,getIdToken()为空: private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { //here the acct.getIdToken() is null AuthCredential credential = GoogleAuthPro

嘿,我正在使用Firebaseauth,并启用了谷歌登录功能。 谷歌登录运行良好,我得到了所有信息。但是当我想在Firebase用户列表中保存用户时,
getIdToken()
为空:

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {       

    //here the acct.getIdToken() is null

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    firebaseAuth.signInWithCredential(credential)
      .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
          @Override
          public void onComplete(@NonNull Task<AuthResult> task) {
              if (!task.isSuccessful()) {
                  Toast.makeText(SignIn.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
              }

              // [START_EXCLUDE]
              progressDialog.hide();
              // [END_EXCLUDE]
          }
      });

}
private void firebaseAuthWithGoogle(谷歌签名帐户){
//此处acct.getIdToken()为空
AuthCredential credential=GoogleAuthProvider.getCredential(acct.getIdToken(),null);
firebaseAuth.signInWithCredential(凭证)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
如果(!task.issusccessful()){
Toast.makeText(SignIn.this,“身份验证失败”,Toast.LENGTH_SHORT.show();
}
//[开始时不包括]
progressDialog.hide();
//[完]
}
});
}

您需要覆盖活动中的
onActivityResult
方法。添加此代码:

if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);
        }
    }
您的
handleSignInResult
方法应该是这样的:

private void handleSignInResult(GoogleSignInResult result) {
    if (result.isSuccess()) {
        GoogleSignInAccount googleSignInAccount = result.getSignInAccount();
        if (googleSignInAccount != null) {
            String userId = googleSignInAccount.getId();
        }
        updateUI(true);
    } else {
        updateUI(false);
    }
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    System.out.println("firebaseAuthWithGoogle:" + acct.getId());

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    auth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    System.out.println("signInWithCredential:onComplete:" + task.isSuccessful());

                    if (!task.isSuccessful()) {
                        System.out.println("signInWithCredential" + task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",Toast.LENGTH_SHORT).show();
                    }
                }
            });
}
您还需要在您的
Firebase
帐户中,在
Authentication
->
登录方法中启用提供商
->
。您需要启用您正在使用的,
电子邮件/密码
谷歌
等等

您还需要将
compile'com.google.firebase:firebase auth:10.2.0'
放入
build.gradle
文件中

在AndroidManifest中,您必须设置这两个权限:
”和“

您还需要设置SHA1。你可以看看怎么做。最后,别忘了再次将您的google-services.json放在正确的位置

这就是
firebaseAuthWithGoogle
方法的外观:

private void handleSignInResult(GoogleSignInResult result) {
    if (result.isSuccess()) {
        GoogleSignInAccount googleSignInAccount = result.getSignInAccount();
        if (googleSignInAccount != null) {
            String userId = googleSignInAccount.getId();
        }
        updateUI(true);
    } else {
        updateUI(false);
    }
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    System.out.println("firebaseAuthWithGoogle:" + acct.getId());

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    auth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    System.out.println("signInWithCredential:onComplete:" + task.isSuccessful());

                    if (!task.isSuccessful()) {
                        System.out.println("signInWithCredential" + task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",Toast.LENGTH_SHORT).show();
                    }
                }
            });
}

希望有帮助

再次确认我的更新代码:

private void signInGoogle(){
    Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleSignInApi);
    startActivityForResult(intent, REQ_CODE);
}

...

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

    GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

    if (requestCode == REQ_CODE){
        GoogleSignInAccount account = result.getSignInAccount();
        handleResult(result);
    }
}

...


private void handleResult(GoogleSignInResult result){
    if (result.isSuccess()){

        // Google Sign In was successful, authenticate with Firebase
        GoogleSignInAccount account = result.getSignInAccount();

        String name = account.getDisplayName();
        String email = account.getEmail();

        googleProfilName.setText(name);
        googleProfilEmail.setText(email);

        if (account.getPhotoUrl() != null) {
            String img_url = account.getPhotoUrl().toString();
            Glide.with(this).load(img_url).into(profilPic);
        }

        String idTest = account.getId();

        firebaseAuthWithGoogle(account);

        updateUI(true);
    }
    else{
        updateUI(false);
    }
}

...



private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    firebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    System.out.println("signInWithCredential:onComplete:" + task.isSuccessful());

                    if (!task.isSuccessful()) {
                        System.out.println("signInWithCredential" + task.getException());
                        Toast.makeText(SignIn.this, "Authentication failed.",Toast.LENGTH_SHORT).show();
                    }
                }
            });

}
private void signingogle(){
Intent Intent=Auth.GoogleSignInApi.getsigninent(GoogleSignInApi);
startActivityForResult(意向、要求代码);
}
...
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
GoogleSignInResult结果=Auth.GoogleSignInApi.getSignInResultFromIntent(数据);
if(请求代码==请求代码){
GoogleSignInAccount account=result.getSignInAccount();
handleResult(结果);
}
}
...
私有无效HandlerResult(谷歌签名结果){
if(result.issucess()){
//Google登录成功,通过Firebase验证
GoogleSignInAccount account=result.getSignInAccount();
字符串名称=account.getDisplayName();
字符串email=account.getEmail();
googleProfilName.setText(名称);
googleProfilEmail.setText(电子邮件);
if(account.getPhotoUrl()!=null){
字符串img_url=account.getPhotoUrl().toString();
使用(this.load)(img_url).into(profilPic);
}
字符串idTest=account.getId();
firebaseAuthWithGoogle(账户);
更新(真);
}
否则{
更新(假);
}
}
...
私有void firebaseAuthWithGoogle(谷歌签名帐户){
AuthCredential credential=GoogleAuthProvider.getCredential(acct.getIdToken(),null);
firebaseAuth.signInWithCredential(凭证)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
System.out.println(“signInWithCredential:onComplete:+task.isSuccessful());
如果(!task.issusccessful()){
System.out.println(“signInWithCredential”+task.getException());
Toast.makeText(SignIn.this,“身份验证失败”,Toast.LENGTH_SHORT.show();
}
}
});
}

您的Google OAuth客户端凭据应该用于Web应用程序,而不是Android。您的Google登录选项应该与下面的代码类似(它是C#,但非常接近Java源代码):


那么它应该可以工作了。

也许你忘了在GoogleSignInOptions对象上请求IDToken()

试试看:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

现在,您需要配置您的项目,以便在您的项目中添加GoogleSignin工具。这里是链接

仅链接的答案往往不好。我们的目标是,即使其中的链接断开,答案仍然有用。谢谢,我花了两个小时试图解决这个问题。创建Web应用程序类型的OAuth客户机Id就做到了。