我正在尝试将用户的Google帐户链接到我的应用。按登录按钮时,将显示您的帐户屏幕,但是当我选择一个时,什么也没有发生。只是进度条永远运行。 这是我的代码:
我已将Firebase正确链接到我的应用程序,并且还磨碎了使用Internet访问我的应用程序的权限。
这是我的主要活动:
static final int GOOGLE_SIGN = 123;
FirebaseAuth mAuth;
Button btnLogin, btnLogout;
ImageView iv;
ProgressBar progressBar;
GoogleSignInClient inClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rankings);
btnLogin = findViewById(R.id.Login);
btnLogin.setOnClickListener(v -> SignInGoogle());
btnLogout = findViewById(R.id.Logout);
btnLogout.setOnClickListener(v -> Logout());
iv = findViewById(R.id.iv);
progressBar = findViewById(R.id.progress);
mAuth = FirebaseAuth.getInstance();
GoogleSignInOptions inOptions = new GoogleSignInOptions
.Builder()
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
inClient = GoogleSignIn.getClient(getApplicationContext(), inOptions);
}
void SignInGoogle(){
progressBar.setVisibility(View.VISIBLE);
Intent signIntent = inClient.getSignInIntent();
startActivityForResult(signIntent, GOOGLE_SIGN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GOOGLE_SIGN) {
Task<GoogleSignInAccount> taskLogin = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount acc = taskLogin.getResult(ApiException.class);
if (acc != null){
firebaseAuthWithGoogle(acc);
}
} catch (ApiException e) {
Log.w("TAG", "Google SignIn failed!", e);
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acc) {
Log.d("TAG", "firebaseAuthWithGoole: " + acc.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acc.getIdToken(), null);
mAuth.signInWithCredential(credential).addOnCompleteListener((Executor) getApplication(), task -> {
if(task.isSuccessful()) {
progressBar.setVisibility(View.INVISIBLE);
Log.d("TAG", "signin success");
FirebaseUser user = mAuth.getCurrentUser();
Toast.makeText(getApplicationContext(), "SignIn Success!", Toast.LENGTH_LONG).show();
}
else {
progressBar.setVisibility(View.INVISIBLE);
Log.w("TaG", "signin failure", task.getException());
Toast.makeText(getApplicationContext(), "SignIn Failed!", Toast.LENGTH_LONG).show();
}
});
}
void Logout(){
FirebaseAuth.getInstance().signOut();
inClient.signOut().addOnCompleteListener((Activity) getApplicationContext(), task -> Toast.makeText(getApplicationContext(), "Logout successfull!", Toast.LENGTH_LONG).show());
}
以下是我在Logcat中发现的警告: (RanksActivity是我要连接到Google的活动)
2019-06-05 21:18:39.215 18414-18414/def.jakob.bittoggle W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@59e82cc
2019-06-05 21:18:44.244 18414-18414/def.jakob.bittoggle W/TAG: Google SignIn failed!
com.google.android.gms.common.api.ApiException: 12500:
at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source:4)
at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source:8)
at def.jakob.bittoggle.RankingsActivity.onActivityResult(RankingsActivity.java:73)
at android.app.Activity.dispatchActivityResult(Activity.java:7476)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4489)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4538)
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:1906)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-06-05 21:19:09.341 18414-18414/def.jakob.bittoggle W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
2019-06-05 21:19:09.353 18414-18414/def.jakob.bittoggle W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection