Java 谷歌登录firebase
我是firebase的新手。我正在尝试将谷歌登录添加到我的应用程序中,但我在完成这项工作时遇到了一些问题 signin.javaJava 谷歌登录firebase,java,android,firebase,firebase-authentication,Java,Android,Firebase,Firebase Authentication,我是firebase的新手。我正在尝试将谷歌登录添加到我的应用程序中,但我在完成这项工作时遇到了一些问题 signin.java package com.example.app100; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; im
package com.example.app100;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.internal.SignInButtonImpl;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
public class Signin extends AppCompatActivity {
private static final int RC_SIGN_IN = 2 ;
private static final String TAG ="sign in" ;
private FirebaseAuth mAuth;
GoogleSignInClient mGoogleSignInClient;
FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signin);
SignInButton signInButton = findViewById(R.id.sign_in);
mAuth = FirebaseAuth.getInstance();
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null)
startActivity(new Intent(Signin.this , MainActivity.class));
}
};
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
startActivity(new Intent(Signin.this , MainActivity.class));
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
//updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
//Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
//updateUI(null);
}
// ...
}
});
}
}
package com.example.app100;
导入androidx.annotation.NonNull;
导入androidx.annotation.Nullable;
导入androidx.appcompat.app.appcompat活动;
导入android.content.Intent;
导入android.net.Uri;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Toast;
导入com.google.android.gms.auth.api.signin.GoogleSignIn;
导入com.google.android.gms.auth.api.signin.GoogleSignInAccount;
导入com.google.android.gms.auth.api.signin.GoogleSignInClient;
导入com.google.android.gms.auth.api.signin.GoogleSignInOptions;
导入com.google.android.gms.common.SignInButton;
导入com.google.android.gms.common.api.ApiException;
导入com.google.android.gms.common.internal.SignInButtonImpl;
导入com.google.android.gms.tasks.OnCompleteListener;
导入com.google.android.gms.tasks.Task;
导入com.google.firebase.auth.AuthCredential;
导入com.google.firebase.auth.AuthResult;
导入com.google.firebase.auth.FirebaseAuth;
导入com.google.firebase.auth.FirebaseUser;
导入com.google.firebase.auth.GoogleAuthProvider;
公共类登录扩展AppCompatActivity{
专用静态最终int RC_SIGN_IN=2;
私有静态最终字符串TAG=“登录”;
私人消防队;
GoogleSignInClient mGoogleSignInClient;
FirebaseAuth.AuthStateListener mAuthListener;
@凌驾
受保护的void onStart(){
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u signin);
SignInButton SignInButton=findViewById(R.id.sign_in);
mAuth=FirebaseAuth.getInstance();
signInButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
签名();
}
});
mAuthListener=new FirebaseAuth.AuthStateListener(){
@凌驾
AuthStateChanged上的公共void(@NonNull FirebaseAuth FirebaseAuth){
如果(firebaseAuth.getCurrentUser()!=null)
startActivity(新意图(Signin.this,MainActivity.class));
}
};
GoogleSignenOptions gso=新建GoogleSignenOptions.Builder(GoogleSignenOptions.DEFAULT\u登录)
.requestEmail()
.build();
mGoogleSignInClient=GoogleSignIn.getClient(this,gso);
}
私人无效签名(){
Intent-signinint=mGoogleSignInClient.getsigninint();
startActivityForResult(签名、注册);
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
//从GoogleSignInApi.getsigninent(…)启动Intent返回的结果;
if(requestCode==RC\u登录){
Task Task=GoogleSignIn.GetSignedAccountFromIntent(数据);
试一试{
//Google登录成功,通过Firebase验证
GoogleSignInAccount account=task.getResult(ApiException.class);
firebaseAuthWithGoogle(账户);
startActivity(新意图(Signin.this,MainActivity.class));
}捕获(APIE){
//Google登录失败,请适当更新UI
Log.w(标签“谷歌登录失败”,e);
// ...
}
}
}
私有void firebaseAuthWithGoogle(谷歌登录帐户){
Log.d(标记为“firebaseAuthWithGoogle:+account.getId());
AuthCredential credential=GoogleAuthProvider.getCredential(account.getIdToken(),null);
mAuth.SIGNWITH凭证(凭证)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//登录成功,使用登录用户的信息更新UI
Log.d(标记“signInWithCredential:success”);
FirebaseUser=mAuth.getCurrentUser();
//updateUI(用户);
}否则{
//如果登录失败,则向用户显示消息。
w(标记“signInWithCredential:failure”,task.getException());
//Snackbar.make(findviewbyd(R.id.main_布局),“身份验证失败”,Snackbar.LENGTH_SHORT.show();
//updateUI(null);
}
// ...
}
});
}
}
错误:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app100, PID: 15815
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { (has extras) }} to activity {com.example.app100/com.example.app100.Signin}: java.lang.IllegalArgumentException: Must specify an idToken or an accessToken.
at android.app.ActivityThread.deliverResults(ActivityThread.java:4461)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4503)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.IllegalArgumentException: Must specify an idToken or an accessToken.
at com.google.firebase.auth.GoogleAuthCredential.<init>(com.google.firebase:firebase-auth@@19.3.0:3)
at com.google.firebase.auth.GoogleAuthProvider.getCredential(com.google.firebase:firebase-auth@@19.3.0:2)
at com.example.app100.Signin.firebaseAuthWithGoogle(Signin.java:105)
at com.example.app100.Signin.onActivityResult(Signin.java:92)
at android.app.Activity.dispatchActivityResult(Activity.java:7598)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4454)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4503)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
I/Process: Sending signal. PID: 15815 SIG: 9
---------坠机开始
E/AndroidRuntime:致命异常:主
进程:com.example.app100,PID:15815
java.lang.RuntimeException:向活动{com.example.app100/com.example.app100.app100.Signin}传递结果ResultInfo{who=null,request=2,result=-1,data=Intent{(有附加项)}}失败:java.lang.illegargumentException:必须指定idToken或accessToken。
位于android.app.ActivityThread.deliverResults(ActivityThread.java:4461)
位于android.app.ActivityThread.handleSendResult(ActivityThread.java:4503)
在android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)中
在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)中
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)上
在android.os.Handler.dispatchMessag
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();