GoogleSignInResult.isSuccess()为true但GoogleSignInAccount的字段为空 - Android

时间:2016-09-10 10:28:51

标签: android google-signin

我在Android应用中使用了Google登录功能。我已经使用他们的标准代码进行登录,并通过在调试模式下将其部署在我的移动设备上进行了测试。这一切都很好。

我现在已将我的应用移至Google Play商店进行封闭式测试。当我从Play商店安装我的应用程序时,它在登录步骤失败,因为我注意到它为用户的email,id和displayName字段返回null。从Play商店安装后我不知道出了什么问题。

登录代码如下:

private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private GoogleSignInAccount acct;

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();

SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
        signInButton.setSize(SignInButton.SIZE_STANDARD);
        signInButton.setScopes(gso.getScopeArray());

@Override
public void onStart() {
    super.onStart();

    OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
    if (opr.isDone()) {
        // If the user's cached credentials are valid, the OptionalPendingResult will be "done"
        // and the GoogleSignInResult will be available instantly.
        isFirstLogin = false;
        GoogleSignInResult result = opr.get();
        handleSignInResult(result);
    } else {
        // If the user has not previously signed in on this device or the sign-in has expired,
        // this asynchronous branch will attempt to sign in the user silently.  Cross-device
        // single sign-on will occur in this branch.
        isFirstLogin = true;
        showProgressDialog();
        opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(GoogleSignInResult googleSignInResult) {
                hideProgressDialog();
                handleSignInResult(googleSignInResult);
            }
        });
    }
}

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }
    // [END onActivityResult]

// [START handleSignInResult]
private void handleSignInResult(GoogleSignInResult result) {
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        acct = result.getSignInAccount();
        mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
        updateUI(true);

        final AsyncTask<Integer, Void, HelloUserResponse> addUser =
                new AsyncTask<Integer, Void, HelloUserResponse>()
                {
                    @Override
                    protected HelloUserResponse doInBackground(Integer... integers) {
                        // Retrieve service handle.
                        Helloworld apiServiceHandle = AppConstants.getApiServiceHandle();
                        try {
                            HelloUser heUser = new HelloUser();
                            heUser.setUseremail(acct.getEmail());
                            heUser.setUserid(acct.getId());
                            heUser.setUsername(acct.getDisplayName());
                            Helloworld.Greetings.AddUser addUserCommand = apiServiceHandle.greetings().addUser(heUser);
                            HelloUserResponse helloUserRsp = addUserCommand.execute();
                            return helloUserRsp;

                        } catch (IOException e) {
                            Toast.makeText(SignInActivity.this, "Something went wrong",
                                    Toast.LENGTH_SHORT).show();
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(HelloUserResponse helloUserRsp) {
                        if(helloUserRsp.getResult()!=0L)
                        {
                            Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
                            SharedPreferences myprefs = getApplicationContext().getSharedPreferences("user", MODE_WORLD_READABLE);
                            myprefs.edit().putString("user_name", acct.getDisplayName()).commit();
                            myprefs.edit().putString("user_email", acct.getEmail()).commit();
                            myprefs.edit().putLong("user_id", helloUserRsp.getResult()).commit();
                            startActivity(myIntent);
                        }
                        else {
                            updateUI(false);
                        }
                    }
                };
        if(isFirstLogin)
            addUser.execute(0);
        else
        {
            Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(myIntent);
        }
    }
    else {
        // Signed out, show unauthenticated UI.
        updateUI(false);
    }
}

0 个答案:

没有答案