確定花了幾個小時後,我決定擺脫所有與firebase相關的東西。現在它可以工作。不要明白爲什麼你會需要那些火力點的東西。
用於登錄我有以下幾點:
您的活動
或activityFragment補充:
private static Activity mActivity;
private FragmentActivity mContext;
private GoogleApiClient mGoogleApiClient;
private GoogleSignInResult cachedSignInResult;
private String webClientId = "XXXXXXXXXXXX-xxxxxxxxxxxxxxx.apps.googleusercontent.com";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(webClientId)
.build();
mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
.enableAutoManage(mContext /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
添加邏輯來處理登錄請求。
public void requestLogin(OnLoginCompleteListener onLoginCompleteListener) {
Log.d(TAG,"request login");
//already logged in scenarios
if (isConnected() && cachedSignInResult != null){
Log.d(TAG,"you are already logged in with "+cachedSignInResult.getDisplayName());
onLoginCompleteListener.onLoginSuccess()
return;
}
else if (isConnected()){
Log.d(TAG,"already connected, but no ");
silentLogin(onLoginCompleteListener);
return;
}
forceRegularLogin(onLoginCompleteListener);
}
public boolean isConnected() {
if (mGoogleApiClient == null){
Log.d(TAG,"cannot check isConnected without api client");
return false;
}
return mGoogleApiClient.isConnecting() || mGoogleApiClient.isConnected();
}
private void silentLogin(final OnLoginCompleteListener onLoginCompleteListener){
OptionalPendingResult<GoogleSignInResult> pendingResult =
Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
pendingResult.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(@NonNull GoogleSignInResult googleSignInResult) {
if (googleSignInResult.isSuccess()){
Log.d(TAG,"result is ok with name "+googleSignInResult.getSignInAccount().getDisplayName());
cachedSignInResult = googleSignInResult;
onLoginCompleteListener.onLoginSuccess()
}
else{
Log.d(TAG,"not logged in silently. do regular login");
requestLogin(onLoginCompleteListener);
}
}
});
}
private void forceRegularLogin(OnLoginCompleteListener onLoginCompleteListener){
if (mGoogleApiClient == null){
Log.d(TAG,"cannot login in. no google api client");
onLoginCompleteListener.error("no_google_api_client_found");
return;
}
registeredLoginCallback = onLoginCompleteListener; //register for later use in activity result
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
mActivity.startActivityForResult(signInIntent, RC_SIGN_IN);
}
add onActivityResult callback。爲完成回調
public interface OnLoginCompleteListener{
public void onLoginSuccess();
public void error(String message);
}
現在在你的登錄按鈕的回調添加
requestLogin(new OnLoginCompleteListener() {
@Override
public void onLoginSuccess() {
Log.d(TAG,"logged in");
}
@Override
public void error(String message) {
Log.d(TAG,"error logging in"+message);
}
}
此外
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG,"on activity result");
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()){
if (registeredLoginCallback != null) {
registeredLoginCallback.onLoginSuccess();
}
else{
Log.e(TAG,"no callback registered for logging in result ok");
}
}
else{
if (registeredLoginCallback != null) {
registeredLoginCallback.error();
}
else{
Log.e(TAG,"no callback registered for logging in result error");
}
}
}
附加界面,你可以添加以下方法和接口獲取用戶的個人資料信息
public void requestCurrentPerson(final OnRequestSocialPersonCompleteListener onRequestSocialPersonCompleteListener) {
Log.d(TAG,"request current person");
silentLogin(new OnLoginCompleteListener() {
@Override
public void onLoginSuccess(i) {
SocialPerson socialPerson = new SocialPerson();
socialPerson.id = signInResult.getSignInAccount().getId();
socialPerson.name = signInResult.getSignInAccount().getDisplayName();
socialPerson.avatarURL = signInResult.getSignInAccount().getPhotoUrl().toString();
onRequestSocialPersonCompleteListener.onRequestSocialPersonSuccess(socialPerson);
}
@Override
public void error(String message) {
onRequestSocialPersonCompleteListener.error("error_fetching_social_person "+message);
}
});
}
public interface OnRequestSocialPersonCompleteListener {
public void onRequestSocialPersonSuccess(SocialPerson socialPerson);
public void error(String message);
}
登錄後您會撥打
requestCurrentPerson(new OnRequestSocialPersonCompleteListener() {
@Override
public void onRequestSocialPersonSuccess(SocialPerson socialPerson) {
Log.d(TAG,"this is my persoin "+socialPerson);
}
@Override
public void error(Message message) {
Log.d(TAG,"error_fetching_social_person " +message);
}
}