Java Firebase Google身份验证错误
你好,我一直在做一个android应用程序的项目,所以我使用了firebase android谷歌认证,但当我启动应用程序并登录时,它工作正常,在关闭应用程序并重新启动应用程序后,请帮助我在下面发布我的文件 googleutil.javaJava Firebase Google身份验证错误,java,android,firebase,Java,Android,Firebase,你好,我一直在做一个android应用程序的项目,所以我使用了firebase android谷歌认证,但当我启动应用程序并登录时,它工作正常,在关闭应用程序并重新启动应用程序后,请帮助我在下面发布我的文件 googleutil.java public class GoogleUtil { public static boolean getBooleanPreference(Context context, String key, boolean defaultValue) {
public class GoogleUtil {
public static boolean getBooleanPreference(Context context, String key, boolean defaultValue) {
boolean value = defaultValue;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
value = preferences.getBoolean(key, defaultValue);
}
return value;
}
public static boolean setBooleanPreference(Context context, String key, boolean value) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences != null) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
return editor.commit();
}
return false;
}
}
google.java
public class Google {
private static final int RC_SIGN_IN = 10;
private GoogleApiClient mGoogleApiClient;
private FragmentActivity context;
private OnInfoLoginGoogleCallback mGoogleCallback;
public Google(FragmentActivity context, OnInfoLoginGoogleCallback mGoogleCallback) {
this.context = context;
this.mGoogleCallback = mGoogleCallback;
getConfigDefaultLogin();
}
private void getConfigDefaultLogin() {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(context.getString(R.string.default_web_client_id))
// TODO: 25-05-2017 Check With JSON default_web_client_id !!! Important
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(context)
.enableAutoManage(context /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
mGoogleCallback.connectionFailedApiClient(connectionResult);
}
}).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
mGoogleCallback.loginFailed();
}
}
}
public void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
context.startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void firebaseAuthWithGoogle(final GoogleSignInAccount acct) {
FirebaseAuth auth = FirebaseAuth.getInstance();
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
auth.signInWithCredential(credential).addOnCompleteListener(context, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
mGoogleCallback.loginFailed();
} else {
mGoogleCallback.getInfoLoginGoogle(acct);
}
}
});
}
public interface OnInfoLoginGoogleCallback {
void getInfoLoginGoogle(GoogleSignInAccount account);
void connectionFailedApiClient(ConnectionResult connectionResult);
void loginFailed();
}
}
这么多代码。
据我所知,您需要在后台进行静默登录。
干得好:
OptionalPendingreult示例:
// LogIN pending result
mGoogleApiClientPendingResult = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (mGoogleApiClientPendingResult.isDone()) {
// There's immediate result available.
// Handle you result here
handleSignInResult(mGoogleApiClientPendingResult.get());
} else {
// There's no immediate result ready, displays some progress indicator and waits for the
// async callback.
mGoogleApiClientPendingResult.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(@NonNull GoogleSignInResult result) {
// Handle you result here
handleSignInResult(result);
}
});
}
//登录挂起结果
mgoogleapiclientpendingreult=Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if(mgoogleapiclientpendingreult.isDone()){
//马上就有结果了。
//在这里处理你的结果
handleSignInResult(mgoogleapiclientPendingreult.get());
}否则{
//没有立即的结果准备就绪,显示一些进度指示器并等待结果
//异步回调。
mgoogleapiclientpendingreult.setResultCallback(新的ResultCallback(){
@凌驾
public void onResult(@NonNull GoogleSignInResult result){
//在这里处理你的结果
handleSignInResult(结果);
}
});
}
主要活动示例:
private static final int RC_SIGN_IN = 28;
private static final String RC_SIGN_IN_TAG = "GoogleSignIn";
private GoogleApiClient mGoogleApiClient;
private OptionalPendingResult<GoogleSignInResult> mGoogleApiClientPendingResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Google Log In
// 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 googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
// Change this for Your needs
.requestScopes(new Scope(Scopes.DRIVE_APPFOLDER), new Scope(Scopes.DRIVE_FILE), new Scope(Scopes.PROFILE))
.requestEmail()
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
//
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
.addApi(Drive.API)
.addScope(Drive.SCOPE_APPFOLDER)
.addScope(Drive.SCOPE_FILE)
.build();
// LogIN pending result
mGoogleApiClientPendingResult = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (mGoogleApiClientPendingResult.isDone()) {
// There's immediate result available.
// Handle you result here
handleSignInResult(mGoogleApiClientPendingResult.get());
} else {
// There's no immediate result ready, displays some progress indicator and waits for the
// async callback.
mGoogleApiClientPendingResult.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(@NonNull GoogleSignInResult result) {
// Handle you result here
handleSignInResult(result);
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(RC_SIGN_IN_TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
if (acct != null) {
// Do your things here
nameTextView.setText(acct.getDisplayName());
emailTextView.setText(acct.getEmail());
// You can gat methods here - https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInAccount
}
}
}
private static final int RC\u SIGN\u IN=28;
私有静态最终字符串RC_SIGN_IN_TAG=“GoogleSignIn”;
私人GoogleapClient MGoogleapClient;
私人期权Pendingreult MGoogleapiclientPendingreult;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//谷歌登录
//配置登录以请求用户ID、电子邮件地址和基本信息
//配置文件。ID和基本配置文件包含在默认登录中。
GoogleSignInOptions GoogleSignInOptions=新建GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT\u登录)
//根据你的需要换这个
.requestScopes(新范围(Scopes.DRIVE\u APPFOLDER)、新范围(Scopes.DRIVE\u文件)、新范围(Scopes.PROFILE))
.requestEmail()
.build();
//通过访问Google登录API和
//gso指定的选项。
mgoogleapclient=新的Googleapclient.Builder(此)
.enableautomanager(这是新的GoogleAppClient.OnConnectionFailedListener()){
@凌驾
public void onconnection失败(@NonNull ConnectionResult ConnectionResult){
//
}
})
.addApi(Auth.GOOGLE\u SIGN\u-IN\u-API、googlesignations)
.addApi(Drive.API)
.addScope(Drive.SCOPE\u APPFOLDER)
.addScope(驱动器.SCOPE\u文件)
.build();
//登录挂起结果
mgoogleapiclientpendingreult=Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if(mgoogleapiclientpendingreult.isDone()){
//马上就有结果了。
//在这里处理你的结果
handleSignInResult(mgoogleapiclientPendingreult.get());
}否则{
//没有立即的结果准备就绪,显示一些进度指示器并等待结果
//异步回调。
mgoogleapiclientpendingreult.setResultCallback(新的ResultCallback(){
@凌驾
public void onResult(@NonNull GoogleSignInResult result){
//在这里处理你的结果
handleSignInResult(结果);
}
});
}
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==RC\u登录){
GoogleSignInResult结果=Auth.GoogleSignInApi.getSignInResultFromIntent(数据);
handleSignInResult(结果);
}
}
私有无效handleSignInResult(Google SignInResult结果){
Log.d(标记“handleSignInResult:+result.issucess()”)中的RC符号;
if(result.issucess()){
//成功登录,显示已验证的UI。
GoogleSignInAccount acct=result.getSignInAccount();
如果(帐户!=null){
//在这里做你的事情
nameTextView.setText(acct.getDisplayName());
emailTextView.setText(acct.getEmail());
//你可以在这里使用gat方法-https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInAccount
}
}
}
// LogIN pending result
mGoogleApiClientPendingResult = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (mGoogleApiClientPendingResult.isDone()) {
// There's immediate result available.
// Handle you result here
handleSignInResult(mGoogleApiClientPendingResult.get());
} else {
// There's no immediate result ready, displays some progress indicator and waits for the
// async callback.
mGoogleApiClientPendingResult.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(@NonNull GoogleSignInResult result) {
// Handle you result here
handleSignInResult(result);
}
});
}
private static final int RC_SIGN_IN = 28;
private static final String RC_SIGN_IN_TAG = "GoogleSignIn";
private GoogleApiClient mGoogleApiClient;
private OptionalPendingResult<GoogleSignInResult> mGoogleApiClientPendingResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Google Log In
// 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 googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
// Change this for Your needs
.requestScopes(new Scope(Scopes.DRIVE_APPFOLDER), new Scope(Scopes.DRIVE_FILE), new Scope(Scopes.PROFILE))
.requestEmail()
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
//
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
.addApi(Drive.API)
.addScope(Drive.SCOPE_APPFOLDER)
.addScope(Drive.SCOPE_FILE)
.build();
// LogIN pending result
mGoogleApiClientPendingResult = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (mGoogleApiClientPendingResult.isDone()) {
// There's immediate result available.
// Handle you result here
handleSignInResult(mGoogleApiClientPendingResult.get());
} else {
// There's no immediate result ready, displays some progress indicator and waits for the
// async callback.
mGoogleApiClientPendingResult.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(@NonNull GoogleSignInResult result) {
// Handle you result here
handleSignInResult(result);
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(RC_SIGN_IN_TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
if (acct != null) {
// Do your things here
nameTextView.setText(acct.getDisplayName());
emailTextView.setText(acct.getEmail());
// You can gat methods here - https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInAccount
}
}
}