如何使用Firebase登录Facebook后如何显示带有displayName的祝酒消息?

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

在我的应用程序中,可以用Facebook登录,成功登录后,我想显示一条祝词,上面写着“欢迎回来,用户名(这是displayName)”。我设法显示一条消息,但没有用户名,因为我不知道如何从Firebase获取该消息并在登录后显示它。

这里是处理Facebook登录的代码:

private void handleFacebookAccessToken(AccessToken token) {
        Log.d(TAG, "handleFacebookAccessToken:" + token);
        progressBar.setVisibility(View.VISIBLE);


        AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            updateUI(user);

// Todo make a toast with the username
                            Toast.makeText(SignInActivity.this, "Welcome back", Toast.LENGTH_SHORT).show();
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Toast.makeText(SignInActivity.this, "Error.",
                                    Toast.LENGTH_LONG).show();
                            updateUI(null);
                        }

                        progressBar.setVisibility(View.INVISIBLE);
                    }
                });
    }
android firebase-authentication facebook-authentication android-toast
1个回答
1
投票

一旦拥有Facebook访问令牌,就可以使用GraphApi获取用户的其他信息:

val request = GraphRequest.newMeRequest(
            accessToken
        ) { user, _ ->
            try {
                val name = user.getString("name")
                Toast.makeText(context, "Hello, $name!", Toast.LENGTH_SHORT).show()
            } catch (e: JSONException) {
                Timber.d("Unable to get user name")
            }
        }
val parameters = Bundle()
parameters.putString("fields", "name")
request.parameters = parameters
request.executeAsync()

这里是Java代码段

public void requestData(){
        GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object,GraphResponse response) {

                JSONObject json = response.getJSONObject();
                try {
                    if(json != null){
                        String name = user.getString("name");
                    Toast.makeText(context, "Welcome "+name, Toast.LENGTH_SHORT).show()
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link,email,picture");
        request.setParameters(parameters);
}
© www.soinside.com 2019 - 2024. All rights reserved.