谷歌登录Null GetDisplayName的问题

问题描述 投票:0回答:1

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

我还将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。

android
1个回答
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) {

    }

}

希望它也适合你

© www.soinside.com 2019 - 2024. All rights reserved.