使用Plus登录验证用户
onCreate
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestScopes(new Scope(Scopes.PLUS_LOGIN)) .requestEmail() .build(); mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Auth.GOOGLE_SIGN_IN_API,gso) .addApi(Plus.API) .build();
onStart()
protected void onStart() { super.onStart(); mGoogleApiClient.connect(); mAuth.addAuthStateListener(mAuthListener); } protected void onStop() { super.onStop(); if (mAuthListener != null) { mAuth.removeAuthStateListener(mAuthListener); } if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Override public void onConnected(Bundle bundle) { mSignInClicked = false; getProfileInformation(); //getGoogleOAuthTokenAndLogin(); Toast.makeText(this, "User is connected! (in onConnected MActivty)",Toast.LENGTH_LONG).show(); // 获取用户信息 // 登录后更新用户界面 // updateUI(true); } @Override public void onConnectionSuspended(int i) { mGoogleApiClient.connect(); //updateUI(false); } @Override public void onConnectionFailed(ConnectionResult connectionResult) { if (!connectionResult.hasResolution()) { GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show(); } else if(connectionResult.hasResolution()) { mConnectionResult = connectionResult; resolveSignInError(); } } private void resolveSignInError() { Log.e("pavan", "User RESOLVE SIGN IN ERROR CALLED OUT OF IF"); if(mSignInClicked){ if (mConnectionResult.hasResolution()) { try { Log.e("pavan", "User RESOLVE SIGN IN ERROR CALLED OUT OF IF TRY"); mIntentInProgress = true; mConnectionResult.startResolutionForResult(this, GOOGLE_SIGIN); } catch (IntentSender.SendIntentException e) { Log.e("pavan", "User RESOLVE SIGN IN ERROR CALLED OUT OF IF CATCH"); mIntentInProgress = false; mGoogleApiClient.connect(); } } } }
获取资料信息
private void getProfileInformation() { try { if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) { Person currentPerson = Plus.PeopleApi .getCurrentPerson(mGoogleApiClient); String personName = currentPerson.getDisplayName(); String personPhotoUrl = currentPerson.getImage().getUrl(); String personGooglePlusProfile = currentPerson.getUrl(); String email = Plus.AccountApi.getAccountName(mGoogleApiClient); } catch (Exception e) { e.printStackTrace(); } }
使用Firebase进行身份验证,
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { //AuthCredential凭据= GoogleAuthProvider.getCredential(tokenCredential,null); AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(),null); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d("TAG", "signInWithCredential:onComplete:" + task.isSuccessful()); //如果登录失败,则向用户显示一条消息。如果登录成功 // 将会通知auth状态监听器,并处理逻辑 // 登录的用户可以在侦听器中处理。 if (!task.isSuccessful()) { Log.w("TAG", "signInWithCredential", task.getException()); Toast.makeText(MainActivity.this, "验证失败。", Toast.LENGTH_SHORT).show(); }else{ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); Log.e("SahajLOG", "登录PREF ISSSSSSSS ONACTIVITYRESULT "+prefs.getBoolean("AuthByGplus", AuthByGplus)); prefs.edit().putBoolean("AuthByGplus", true).commit(); Log.e("SahajLOG", "登录PREF ISSSSSSSS ONACTIVITYRESULT IFTRUE.. "+prefs.getBoolean("AuthByGplus", AuthByGplus)); Intent intent=new Intent(getApplicationContext(),MainActivity.class); startActivity(intent); finish(); } // [START_EXCLUDE] // hideProgressDialog(); // [END_EXCLUDE] } }); }
onActivityResult
@Override protected void onActivityResult(int requestCode, int responseCode, Intent intent) { if (requestCode == GOOGLE_SIGIN) { if (responseCode != RESULT_OK) { mSignInClicked = false; } mIntentInProgress = false; if (!mGoogleApiClient.isConnecting()) { mGoogleApiClient.connect(); if (mGoogleApiClient.isConnected()) { // getGoogleOAuthTokenAndLogin(); } } GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(intent); if (result.isSuccess()) { // Google登录成功,已通过Firebase进行身份验证 GoogleSignInAccount account = result.getSignInAccount(); Log.e("SahajLOG", "account " + account.getIdToken()); //getGoogleOAuthTokenAndLogin(); firebaseAuthWithGoogle(account); } else { // Google登录失败,请适当更新用户界面 // [START_EXCLUDE] Log.e("SahajLOG", "SIGN IN FAILED"); // [END_EXCLUDE] } } public void signInWithGplus() { if (!mGoogleApiClient.isConnecting()) { mSignInClicked = true; Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, GOOGLE_SIGIN); } }
登出
@Override public void onLogout() { mAuth.signOut(); if (mGoogleApiClient.isConnected()) { Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); mGoogleApiClient.disconnect(); mGoogleApiClient.connect(); SignedInWithGoogle=false; } // Google登出 switchToLoginFragment(); }
使用,https://github.com/firebase/FirebaseUI-Android/tree/master/codelabs/chat作为登录的参考,我在键入时似乎遇到了问题 我只能键入Auth的提供者,而不能键入,为什么会这样,它提示我键入社会提供者。
当使用Firebase身份验证匿名帐户时,它偶尔会在系统中创建一个新的用户ID,有时它会使用相同的用户ID。我真的希望每次都能创建相同的用户ID,这样匿名用户仍然可以在应用程序中维护相同的进度/数据。这实际上是我开始使用Firebase的原因。即使在重新启动应用程序等之后,我如何始终维护一个匿名帐户来保持相同的用户ID? 我希望用户每次以访客身份玩游戏时都能获得相同的ID。我看到有些应用程序在卸载
大家好,我对FireBase创建用户和注册用户有问题。下面是注册和登录的代码。我得到空指针异常,无法理解原因。。初始化完成了,但经过了这么多天的努力,还是弄不明白。 注册: 正在发布注册活动的日志错误: java.lang.com.google.android.gms.internal.zzdvv.zzb(未知来源)在com.google.android.gms.internal.zzdwc.zz
该应用程序应该接受用户电子邮件和密码,并使用Firebase身份验证作为后端。我使用像素2作为模拟器。每次应用程序处理登录功能时,它都会崩溃。 下面是Java文件和gradle文件 Java文件:
我有一个电子邮件/密码firebase认证的应用程序。 它从登录活动开始 然后在我的onStart中,我检查当前用户是否为空,然后它导航到我的主要活动。这部分工作得非常好。 问题来了,在主活动中我有一个按钮,在这里我从LoginActivity.java调用signout函数: 问题是当logMeOut运行时,我得到了这种美 java.lang.NullPointerException:尝试对空对
我已经在我的xamarin Android应用程序中实现了脸书认证,一切都很好。我从facebook获得令牌,并使用firebase rest Api的sigin方法,我能够sigin到firebase,并从firebase Api收到一个访问令牌,作为JWT。但是,当我想在我的Web Api核心上使用这个访问令牌时,它总是返回not authenticated。我的Web Api核心使用下面基于