其他分享
首页 > 其他分享> > android-GoogleSignInAccount返回null

android-GoogleSignInAccount返回null

作者:互联网

我正在尝试使用最新的Google API.
我按照Google的建议设置了所有依赖项.

编译’com.google.android.gms:play-services:9.0.0′

并且还创建了一个google-service.json.
在控制台中启用Google API …
我认为一切都应该正常工作.
我可以使用Google参考和意图登录.
但是在收到回调后

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

在GoogleSignInAccount中,所有属性值均为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();

您应该能够使用requestEmail或scope.
这是来自最新Google API的问题吗?

解决方法:

尝试使用此:

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();
    }
}


}

您的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));
}
}

确保在开发者控制台中启用了Google登录,然后在您的应用目录中添加JSON文件!

标签:google-signin,googlesigninaccount,android
来源: https://codeday.me/bug/20191118/2029390.html