Android的Facebook 4.0 SDK如何获得电子邮件,出生date和用户的性别

我正在使用下面的代码。 我想要用户的出生date,电子邮件和性别。 请帮忙。 如何检索这些数据?

这是我的片段内的onViewCreated()

 @Override public void onViewCreated(View view, Bundle savedInstanceState) { // Setup TextView. mTextDetails = (TextView) view.findViewById(R.id.text_details); // Set up Login Button. LoginButton mButtonLogin = (LoginButton) view.findViewById(R.id.login_button); // setFragment only if you are using it inside a Fragment. mButtonLogin.setFragment(this); mButtonLogin.setReadPermissions("user_friends"); mButtonLogin.setReadPermissions("public_profile"); mButtonLogin.setReadPermissions("email"); mButtonLogin.setReadPermissions("user_birthday"); // Register a callback method when Login Button is Clicked. mButtonLogin.registerCallback(mCallbackManager, mFacebookCallback); } 

这是我的callback方法。

 private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { Log.d("Shreks Fragment", "onSuccess"); Profile profile = Profile.getCurrentProfile(); Log.d("Shreks Fragment onSuccess", "" +profile); // Get User Name mTextDetails.setText(profile.getName() + ""); } @Override public void onCancel() { Log.d("Shreks Fragmnt", "onCancel"); } @Override public void onError(FacebookException e) { Log.d("Shreks Fragment", "onError " + e); } }; 

这不是正确的方法来设置权限,因为您正在用每个方法调用覆盖它们。

replace这个:

 mButtonLogin.setReadPermissions("user_friends"); mButtonLogin.setReadPermissions("public_profile"); mButtonLogin.setReadPermissions("email"); mButtonLogin.setReadPermissions("user_birthday"); 

通过以下方法, setReadPermissions()方法接受一个ArrayList:

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

另外这里是如何查询额外的数据GraphRequest:

 private LoginButton loginButton; private CallbackManager callbackManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.setReadPermissions(Arrays.asList( "public_profile", "email", "user_birthday", "user_friends")); callbackManager = CallbackManager.Factory.create(); // Callback registration loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { // App code GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject object, GraphResponse response) { Log.v("LoginActivity", response.toString()); // Application code String email = object.getString("email"); String birthday = object.getString("birthday"); // 01/31/1980 format } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id,name,email,gender,birthday"); request.setParameters(parameters); request.executeAsync(); } @Override public void onCancel() { // App code Log.v("LoginActivity", "cancel"); } @Override public void onError(FacebookException exception) { // App code Log.v("LoginActivity", exception.getCause().toString()); } }); } 

编辑:

一个可能的问题是,Facebook假设你的电子邮件是无效的。 要testing它,使用graphicsAPI资源pipe理器并尝试获取它。 如果即使在那里,你也无法得到你的电子邮件,在你的个人资料设置中更改它,然后重试。 这种方法解决了这个问题,一些开发人员评论我的答案。

您不会在onSuccess()获取configuration文件,您需要实现ProfileTracker和注册callback

 mProfileTracker = new ProfileTracker() { @Override protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) { // Fetch user details from New Profile } }; 

另外不要忘记处理configuration文件跟踪器的启动和停止

现在你将有一个configuration文件来获取AccessToken(解决了空configuration文件的问题)。 你只需要使用“ https://developers.facebook.com/docs/android/graph#userdata ”来获取任何数据。

使用此片段获取所有个人资料信息

 private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { AccessToken accessToken = loginResult.getAccessToken(); Profile profile = Profile.getCurrentProfile(); // Facebook Email address GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted( JSONObject object, GraphResponse response) { Log.v("LoginActivity Response ", response.toString()); try { Name = object.getString("name"); FEmail = object.getString("email"); Log.v("Email = ", " " + FEmail); Toast.makeText(getApplicationContext(), "Name " + Name, Toast.LENGTH_LONG).show(); } catch (JSONException e) { e.printStackTrace(); } } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id,name,email,gender, birthday"); request.setParameters(parameters); request.executeAsync(); } @Override public void onCancel() { LoginManager.getInstance().logOut(); } @Override public void onError(FacebookException e) { } }; 

您可以使用GraphRequest类来发出对Facebook Graph API的调用来获取用户信息。 有关更多信息,请参见https://developers.facebook.com/docs/android/graph

使用Profile类的FB静态方法getCurrentProfile()来检索这些信息。

  Profile profile = Profile.getCurrentProfile(); String firstName = profile.getFirstName()); System.out.println(profile.getProfilePictureUri(20,20)); System.out.println(profile.getLinkUri()); 

login后

 private void getFbInfo() { GraphRequest request = GraphRequest.newMeRequest( AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted( JSONObject object, GraphResponse response) { try { Log.d(LOG_TAG, "fb json object: " + object); Log.d(LOG_TAG, "fb graph response: " + response); String id = object.getString("id"); String first_name = object.getString("first_name"); String last_name = object.getString("last_name"); String gender = object.getString("gender"); String birthday = object.getString("birthday"); String image_url = "http://graph.facebook.com/" + id + "/picture?type=large"; String email; if (object.has("email")) { email = object.getString("email"); } } catch (JSONException e) { e.printStackTrace(); } } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id,first_name,last_name,email,gender,birthday"); // id,first_name,last_name,email,gender,birthday,cover,picture.type(large) request.setParameters(parameters); request.executeAsync(); }