一开始,对不起,如果这是你见过的最愚蠢的问题。但我在 Android Google登录方面遇到了麻烦。我正在申请让Google登录。
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);
}}
以下是活动的完整代码。
在方法 handleSignInResult()中,我 总是获取空用户 。
以下是gradle文件。
模块级别&gt;&gt;&gt;
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
compile 'com.google.firebase:firebase-messaging:11.8.0'
compile 'com.google.firebase:firebase-core:11.8.0'
compile 'com.google.firebase:firebase-auth:11.8.0'
compile 'com.google.android.gms:play-services-auth:11.8.0'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'}apply plugin: 'com.google.gms.google-services'
应用级别&gt;&gt;&gt;&gt;
buildscript {
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
}
答案 0 :(得分:0)
用户为空,因为您从未通过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 FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
添加到onCreate()答案 1 :(得分:0)
就我而言,我遇到了类似的错误,例如“ NULL USER” ...
但是,我犯的一个愚蠢的错误是...
我忘记初始化私有FirebaseAuth mAuth;
一旦我初始化变量,下面的问题就解决了... mAuth = FirebaseAuth.getInstance();
注意:将 mAuth = FirebaseAuth.getInstance(); 添加到onCreate()