How Do I Fetch Name and Email using Facebook SDK

£可爱£侵袭症+ 提交于 2019-12-17 21:20:35

问题


I am using a TextView to show Name and Email of logged in facebook user, but i really don't know How do I fetch Name and Email ?

private void onSessionStateChange(Session session, SessionState state,
                                  Exception exception) {
    if (session != currentSession) {
        return;
    }

    if (state.isOpened()) {
        // Log in just happened.
        Toast.makeText(getApplicationContext(), "session opened",
                Toast.LENGTH_SHORT).show();
    } else if (state.isClosed()) {
        // Log out just happened. Update the UI.
        Toast.makeText(getApplicationContext(), "session closed",
                Toast.LENGTH_SHORT).show();
    }
}

回答1:


Refer this: for details here

private class SessionStatusCallback implements Session.StatusCallback {
    private String fbAccessToken;

    @Override
    public void call(Session session, SessionState state, Exception exception) {
        updateView();
        if (session.isOpened()) {
            fbAccessToken = session.getAccessToken();
            // make request to get facebook user info
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    Log.i("fb", "fb user: "+ user.toString());

                    String fbId = user.getId();
                    String fbAccessToken = fbAccessToken;
                    String fbName = user.getName();
                    String gender = user.asMap().get("gender").toString();
                    String email = user.asMap().get("email").toString();

                    Log.i("fb", userProfile.getEmail());
                }
            });
        }
    }
}



回答2:


You can get the name and email as below:

// use their Facebook info
    JSONObject json = Util.parseJson(facebook.request("me"));
    String facebookID = json.getString("id");
    String firstName = json.getString("first_name");
    String lastName = json.getString("last_name");
    Toast.makeText(uiActivity,"Thank you for Logging In, " 
                 + firstName + " " 
                 + lastName + "!"
                 , Toast.LENGTH_SHORT).show();



回答3:


This Is An Working Example For facebook Sdk 4+ Also You Can Follow This Link

This Is Your XML

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

 <com.facebook.login.widget.LoginButton
    android:id="@+id/connectWithFbButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal"
    android:text="  connect_with_facebook" />

      </LinearLayout>

put this in onCreate

      private LoginButton loginButton;
      CallbackManager callbackManager;

     loginButton = (LoginButton) findViewById(R.id.connectWithFbButton);

        loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday"));



        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                pDialog = new ProgressDialog(MainActivity.this);
                pDialog.setMessage("Please Wait..");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show(); 

                 GraphRequest request = GraphRequest.newMeRequest(
                         loginResult.getAccessToken(),
                         new GraphRequest.GraphJSONObjectCallback() {
                             @Override
                             public void onCompleted(
                                     JSONObject object,
                                     GraphResponse response) {
                                 pDialog.dismiss();
                                 Log.d("Response",response.getJSONObject().toString());
                                 if (response.getError() != null) {
                                     // handle error
                                 } else {
                                     String email = object.optString("email");
                                     String fname = object.optString("fname");
                                     String lname = object.optString("lname");
                                   Log.d("Email",email);
                                   Log.d("fname",fname);
                                   Log.d("lname",lname);
                              //     Log.d("Response", response.getInnerJsobject.toString());

                                 }
                             }
                         });
                 Bundle parameters = new Bundle();
                 parameters.putString("fields", "id,name,email,gender, birthday");
                 request.setParameters(parameters);
                 request.executeAsync();

            }

            @Override
            public void onCancel() {
                // App code
                Log.d("LoginActivity", "cancel");
            }

            @Override
            public void onError(FacebookException exception) {
                // App code
                 Log.d("LoginActivity", exception.getCause().toString());
            }
        });

In You OnActivityResult Write Following

@Override
  protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        callbackManager.onActivityResult(requestCode, responseCode, intent);
    }


来源:https://stackoverflow.com/questions/30317540/how-do-i-fetch-name-and-email-using-facebook-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!