GoogleSignInAccount returns null

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 14:04:49

问题


I am trying to use the latest Google API.
I set up all the dependencies as suggested by Google.

compile 'com.google.android.gms:play-services:9.0.0'

and also created a google-service.json.
Enable the Google API in a console...
I think everything is supposed to work. I can login with Google reference and intent.
But after getting a callback

GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
    if (result.isSuccess()) {
        GoogleSignInAccount acct = result.getSignInAccount();
        if (listener != null) {
            listener.onSuccess(acct);
        }
    }

in GoogleSignInAccount all properties values are null.

this.mGoogleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            //.requestScopes(new Scope(Scopes.PLUS_LOGIN)) // "https://www.googleapis.com/auth/plus.login"
            //.requestScopes(new Scope(Scopes.PLUS_ME)) // "https://www.googleapis.com/auth/plus.me"
            //.requestScopes(new Scope(Scopes.EMAIL))
            //.requestScopes(new Scope(Scopes.PROFILE))
            //.requestEmail()
            //.requestProfile()
            .build();
    this.mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
            .enableAutoManage((FragmentActivity) mActivity /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API)
            .build();

You are supposed to be able to use either requestEmail or scope.
Is this an issue from the latest Google API?


回答1:


Try using this:

public class GooglePlusLoginUtils implements ConnectionCallbacks, OnConnectionFailedListener,OnClickListener {

private String TAG = "GooglePlusLoginUtils";
/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 0;
private static final int PROFILE_PIC_SIZE = 400;
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String PHOTO = "photo";
public static final String PROFILE= "profile";

/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;

private SignInButton btnSignIn;
private Context ctx;
private GPlusLoginStatus loginstatus;
public interface GPlusLoginStatus{
    public void OnSuccessGPlusLogin(Bundle profile);
}

public GooglePlusLoginUtils(Context ctx,int btnRes){
    Log.i(TAG, "GooglePlusLoginUtils");
    this.ctx= ctx;
    this.btnSignIn =(SignInButton) ((Activity)ctx).findViewById(btnRes);
    btnSignIn.setOnClickListener(this);
     // Initializing google plus api client
    mGoogleApiClient = new GoogleApiClient.Builder(ctx)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();


}

public void setLoginStatus(GPlusLoginStatus loginStatus){
    this.loginstatus = loginStatus;
}
@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "onConnectionFailed");
    Log.i(TAG,"Error Code "+ result.getErrorCode());
    if (!result.hasResolution()) {
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), (Activity)ctx,0).show();
        return;
    }

    if (!mIntentInProgress) {
        // Store the ConnectionResult for later usage
        mConnectionResult = result;

        if (mSignInClicked) {
            // The user has already clicked 'sign-in' so we attempt to
            // resolve all
            // errors until the user is signed in, or they cancel.
            resolveSignInError();
        }
    }       
}
public void setSignInClicked(boolean value){
    mSignInClicked  =value;
}
public void setIntentInProgress(boolean value){
    mIntentInProgress = value;
}
public void connect(){
    mGoogleApiClient.connect();
}
public void reconnect(){
    if (!mGoogleApiClient.isConnecting()) {
        mGoogleApiClient.connect();
    }
}
public void disconnect(){
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}
private void signInWithGplus() {
    Log.i(TAG, "signInWithGplus");
    if (!mGoogleApiClient.isConnecting()) {
        mSignInClicked = true;
        resolveSignInError();
    }
}
private void resolveSignInError() {
    Log.i(TAG, "resolveSignInError");
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            mConnectionResult.startResolutionForResult((Activity)ctx, RC_SIGN_IN);
        } catch (SendIntentException e) {
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
}
@Override
public void onConnected(Bundle arg0) {
    Log.i(TAG, "onConnected");
     mSignInClicked = false;
        Toast.makeText(ctx, "User is connected!", Toast.LENGTH_LONG).show();

        // Get user's information
        getProfileInformation();

}
@Override
public void onConnectionSuspended(int arg0) {
    Log.i(TAG, "onConnectionSuspended");
    mGoogleApiClient.connect();
}

private void getProfileInformation() {
    Log.i(TAG, "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);

            Log.e(TAG, "Name: " + personName + ", plusProfile: "
                    + personGooglePlusProfile + ", email: " + email
                    + ", Image: " + personPhotoUrl);


            // by default the profile url gives 50x50 px image only
            // we can replace the value with whatever dimension we want by
            // replacing sz=X
            personPhotoUrl = personPhotoUrl.substring(0,
                    personPhotoUrl.length() - 2)
                    + PROFILE_PIC_SIZE;

            Bundle profile = new Bundle();
            profile.putString(NAME, personName);
            profile.putString(EMAIL, email);
            profile.putString(PHOTO, personPhotoUrl);
            profile.putString(PROFILE, personGooglePlusProfile);

            loginstatus.OnSuccessGPlusLogin(profile);

         //   new LoadProfileImage(imgProfilePic).execute(personPhotoUrl);

        } else {
            Toast.makeText(ctx,
                    "Person information is null", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
public void onClick(View v) {
    signInWithGplus();
}
public void onActivityResult(int requestCode,int responseCode,Intent intent){
    if (requestCode == RC_SIGN_IN) {
        if (responseCode != ((Activity)ctx).RESULT_OK) {
            setSignInClicked(false);
        }
        setIntentInProgress(false);
        reconnect();
    }
}


}

Your MainActivity:

public class LoginActivity extends ActionBarActivity implements GooglePlusLoginUtils.GPlusLoginStatus {

private String TAG = "LoginActivity";
private GooglePlusLoginUtils gLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    gLogin = new GooglePlusLoginUtils(this, R.id.activity_login_gplus);
    gLogin.setLoginStatus(this);

}
@Override
protected void onStart() {
    super.onStart();
    gLogin.connect();
}
@Override
protected void onStop() {
    super.onStop();
    gLogin.disconnect();
}
@Override
protected void onActivityResult(int requestCode, int responseCode,
                                Intent intent) {
    gLogin.onActivityResult(requestCode, responseCode, intent);

}

@Override
public void OnSuccessGPlusLogin(Bundle profile) {
    Log.i(TAG,profile.getString(GooglePlusLoginUtils.NAME));
    Log.i(TAG,profile.getString(GooglePlusLoginUtils.EMAIL));
    Log.i(TAG,profile.getString(GooglePlusLoginUtils.PHOTO));
    Log.i(TAG,profile.getString(GooglePlusLoginUtils.PROFILE));
}
}

Make sure you have enable Google+ Login in the Developers Console and Add the JSON File in your app directory!



来源:https://stackoverflow.com/questions/37506840/googlesigninaccount-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!