我正在使用以下代码。我想要用户的出生日期,电子邮件和性别。请帮忙。如何检索这些数据?
这是片段中的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);
}
这是我的回调方法。
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);
}
};
答案 0 :(得分:315)
这不是设置权限的正确方法,因为每次方法调用都会覆盖它们。
替换它:
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假定您的电子邮件无效。要测试它,请使用Graph API Explorer并尝试获取它。即使在那里,您也无法收到电子邮件,请在配置文件设置中进行更改,然后重试。这种方法解决了这个问题,一些开发人员评论了我的答案。
答案 1 :(得分:12)
使用此代码段获取所有个人资料信息
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) {
}
};
答案 2 :(得分:11)
您无法在onSuccess()
中获得个人资料,您需要实施ProfileTracker
以及注册回调
mProfileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
// Fetch user details from New Profile
}
};
也不要忘记处理profile tracker的开始和停止
现在您将拥有一个从中获取AccessToken的配置文件(解决了null配置文件的问题)。你只需要使用&#34; https://developers.facebook.com/docs/android/graph#userdata&#34;获取任何数据。
答案 3 :(得分:3)
您可以使用GraphRequest类向Facebook Graph API发出调用以获取用户信息。有关详细信息,请参阅https://developers.facebook.com/docs/android/graph。
答案 4 :(得分:3)
登录后
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();
}
答案 5 :(得分:2)
以下是查找电子邮件ID,名称和个人资料网址等的代码
message.react(':heart:')
答案 6 :(得分:0)
这对我有用,希望能帮助别人(使用我自己的按钮而不是FB登录按钮)
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_sign_in_user);
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
Log.i("RESAULTS : ", object.getString("email"));
}catch (Exception e){
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "email");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
Log.i("RESAULTS : ", error.getMessage());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
public void signupwith_facebook(View view) {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile","email"));
}
}
答案 7 :(得分:0)
在单击按钮
上添加此行loginButton.setReadPermissions(Arrays.asList( “ public_profile”,“电子邮件”,“ user_birthday”,“ user_friends”));
答案 8 :(得分:0)
这是一个可行的解决方案(2019年): 将此代码放入您的登录逻辑中;
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject json, GraphResponse response) {
// Application code
if (response.getError() != null) {
System.out.println("ERROR");
} else {
System.out.println("Success");
String jsonresult = String.valueOf(json);
System.out.println("JSON Result" + jsonresult);
String fbUserId = json.optString("id");
String fbUserFirstName = json.optString("name");
String fbUserEmail = json.optString("email");
//String fbUserProfilePics = "http://graph.facebook.com/" + fbUserId + "/picture?type=large";
Log.d("SignUpActivity", "Email: " + fbUserEmail + "\nName: " + fbUserFirstName + "\nID: " + fbUserId);
}
Log.d("SignUpActivity", response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
setResult(RESULT_CANCELED);
Toast.makeText(SignUpActivity.this, "Login Attempt Cancelled", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException error) {
Toast.makeText(SignUpActivity.this, "An Error Occurred", Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
答案 9 :(得分:-3)
使用Profile类的FB静态方法getCurrentProfile()来检索这些信息。
Profile profile = Profile.getCurrentProfile();
String firstName = profile.getFirstName());
System.out.println(profile.getProfilePictureUri(20,20));
System.out.println(profile.getLinkUri());