Java 无法进行google登录(返回用户为空)
首先,如果这是你见过的最愚蠢的问题,请道歉。但是我在安卓谷歌登录方面遇到了麻烦。我正在申请谷歌登录Java 无法进行google登录(返回用户为空),java,android,google-signin,Java,Android,Google Signin,首先,如果这是你见过的最愚蠢的问题,请道歉。但是我在安卓谷歌登录方面遇到了麻烦。我正在申请谷歌登录 public class MainActivity extends AppCompatActivity { private static final int RC_SIGN_IN = 1; private GoogleSignInClient mGoogleSignInClient; private FirebaseAuth mAuth; SignInButton signInButton;
public class MainActivity extends AppCompatActivity {
private static final int RC_SIGN_IN = 1;
private GoogleSignInClient mGoogleSignInClient;
private FirebaseAuth mAuth;
SignInButton signInButton;
@Override
protected void onStart() {
super.onStart();
isUserSignedIn();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Result return from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed
// no need to attach a listener
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
} else
Log.d("activity", "not done");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void isUserSignedIn() {
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (account == null) {
promptSignIn();
} else updateUI(account);
}
private void promptSignIn() {
// Configure sign-in to request the user's ID, email address, and basic profile
// ID and basic profile are included in DEFAULT_SIGN_IN
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
// Build a GoogleSignInClient with the options specified by gso
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
// Set the dimensions of the sign-in button
signInButton = findViewById(R.id.google_btn);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult();
// Signed in successfully, show authenticated UI
updateUI(account);
} catch (Exception e) {
Log.d("activity", "signInResult:failed code=" + e.getMessage());
}
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// Name, email address, and profile photo Url
String name = user.getDisplayName();
String email = user.getEmail();
Uri photoUrl = user.getPhotoUrl();
// Check if user's email is verified
boolean emailVerified = user.isEmailVerified();
Log.d("activity", name);
Log.d("activity", email);
Log.d("activity", photoUrl + "");
Log.d("activity", emailVerified + "");
// The user's ID, unique to the Firebase project. Do NOT use this value to
// authenticate with your backend server if you have one. Use
// FirebaseUser.getToken() instead.
String uid = user.getUid();
} else
Log.d("activity", "user is null");
}
protected void updateUI(GoogleSignInAccount account) {
if (signInButton != null) signInButton.setVisibility(View.GONE);
}}
应用程序级别>>>
构建脚本{
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.gms:google-services:3.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files }}allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
该用户为空,因为您从未通过Firebase登录该用户。使用Google登录Firebase时,您需要通过两个API登录该用户
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success
FirebaseUser user = mAuth.getCurrentUser();
} else {
// Sign in failed
}
}
});
private void firebaseAuthWithGoogle(谷歌签名帐户){
Log.d(标记为“firebaseAuthWithGoogle:+acct.getId());
AuthCredential credential=GoogleAuthProvider.getCredential(acct.getIdToken(),null);
mAuth.SIGNWITH凭证(凭证)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//成功登录
FirebaseUser=mAuth.getCurrentUser();
}否则{
//登录失败
}
}
});
}mAuth=FirebaseAuth.getInstance();
添加到onCreate()中在我的例子中,我得到了与“空用户”相同的错误 但是,我犯的愚蠢错误是 我忘记初始化私有FirebaseAuth mAuth; 一旦我初始化了如下的变量,问题就解决了。。。 mAuth=FirebaseAuth.getInstance();
注意:添加mAuth=FirebaseAuth.getInstance();到onCreate()但它给了我“signInResult:failed code=尝试在空对象引用上调用虚拟方法'com.google.android.gms.tasks.com.google.firebase.auth.FirebaseAuth.signInWithCredential(com.google.firebase.auth.AuthCredential)'”我更新了我的答案,如果不清楚,您需要的详细信息缺失。您在Firebase项目中启用了登录方法吗?是的,谢谢您的合作。我想Marcus给了我答案。您有什么建议吗?
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success
FirebaseUser user = mAuth.getCurrentUser();
} else {
// Sign in failed
}
}
});