谷歌登录问题白衣Null GetDisplayName

时间:2019-04-20 13:02:56

标签: android

我正在尝试学习如何在whit firebase中进行这种Google登录。 用户名为空,我不明白为什么: acct.getDisplayName()。 我遵循了此指南: https://www.youtube.com/watch?time_continue=11&v=SXlidHy-Tb8的Firebase。

我还将OAuth 2.0客户端ID与firebase项目相关联。

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

    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSingInResult(result);
    }
}

private void handleSingInResult(GoogleSignInResult result) {
    if (result.isSuccess()) {
        GoogleSignInAccount acct = result.getSignInAccount();
        Message.setText(acct.getDisplayName());
    } else {

    }
}

在这里使用Google SignInOptions

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestProfile()
                .build();
        Client = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, this)
                .addApi(Auth.GOOGLE_SIGN_IN_API)
                .build();

我希望输出为用户名,但实际输出为Null。

1 个答案:

答案 0 :(得分:1)

这是我尝试的方式。它对我有用。

class SignInActivity : AppCompatActivity(),GoogleApiClient.OnConnectionFailedListener,
        GoogleApiClient.ConnectionCallbacks {

   override fun onCreate(savedInstanceState: Bundle?) {
               gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build()

        mApiClient = GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso!!)
                .build()
   }

      private val clickListener: View.OnClickListener = View.OnClickListener { view ->
        when (view.id) {
                       R.id.linear_login_google -> {
                googleLogin()
            }
                  }
    }
    fun googleLogin() {
        try {
            if (mApiClient != null) {
                if (mApiClient!!.isConnected()) {
                    val intent = Auth.GoogleSignInApi.getSignInIntent(mApiClient)
                    startActivityForResult(intent, 25)
                } else {
                    mApiClient!!.connect()
                }
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

   private fun callGoogleAccountSelection() {
        try {
            if (mApiClient != null) {
                if (mApiClient!!.isConnected()) {
                    Auth.GoogleSignInApi.signOut(mApiClient)
                    mApiClient!!.clearDefaultAccountAndReconnect().setResultCallback {
                        val intent = Auth.GoogleSignInApi.getSignInIntent(mApiClient)
                        startActivityForResult(intent, 25)
                    }
                }
            }
        } catch (e: Exception) {
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        callbackManager!!.onActivityResult(requestCode, resultCode, data)

        if (resultCode == Activity.RESULT_OK)
            when (requestCode) {
                101 -> try {
                    val task = GoogleSignIn.getSignedInAccountFromIntent(data)
                    val account = task.getResult(ApiException::class.java)
                } catch (e: ApiException) {
                    Log.e("", "signInResult:failed code=" + e.statusCode)
                }

            }

        if (requestCode == 25) {
            val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
            if (result.isSuccess) {
                val account = result.signInAccount
                mSocialID = account!!.id
                mSocialName = account.givenName + " " + account.familyName
                mSocialLName = account.familyName
                mSocialEmail = account.email
                try {
                    if (account.photoUrl != null) {
                        mSocialProfilePhoto = account.photoUrl!!.toString()
                    }

                } catch (e: Exception) {
                    Toast.makeText(applicationContext, "Exception", Toast.LENGTH_SHORT).show()

                }
            }
        }
    }

    override fun onConnectionFailed(p0: ConnectionResult) {

    }

    override fun onConnected(p0: Bundle?) {
        if (!calledOnce) {
            calledOnce = true
            callGoogleAccountSelection()
        }
    }

    override fun onConnectionSuspended(p0: Int) {

    }

}

希望它也对您有用