如何在Facebook中集成facebook并登录并获取用户详细信息并注销。
代码JSONObject json = Util.parseJson(mFacebook.request("me"));
给我null
我在OnComplete()
之后从包中获取访问令牌。
答案 0 :(得分:0)
你应该发布更多你已经完成的代码,这样你就可以更容易理解你的问题。另外,如果您收到错误,请发布logcat输出。检查this link out. 这是针对Android的Facebook开发人员教程,它将带您逐步将Facebook整合到您的应用中。现在,教程有时很模糊,难以理解,所以请检查this link out.这是一个视频教程,可以指导您完成各个步骤,并且非常容易理解。祝你好运!
答案 1 :(得分:0)
这对我来说可行,试试这个:
public class FacebookIntegrationActivity extends AppCompatActivity {
ActivityFacebookIntegrationBinding binding;
CallbackManager callbackManager;
private static final String EMAIL = "email";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(FacebookIntegrationActivity.this,
R.layout.activity_facebook_integration);
//[apply click on view]
applyClickOnView();
//method to integrate facebook
facebookIntegration();
}
private void applyClickOnView() {
binding.btnCustom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoginManager.getInstance().logOut();
LoginManager.getInstance().logInWithReadPermissions(FacebookIntegrationActivity.this,
Arrays.asList("public_profile", "email"));
}
});
}
//[for facebook integration]
private void facebookIntegration() {
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code to get user profile
requestUserProfile(loginResult);
}
@Override
public void onCancel() {
String s = "";
//code on cancling of request
}
@Override
public void onError(FacebookException exception) {
String s = "";
//code on error occurence
}
});
}
private void requestUserProfile(LoginResult loginResult) {
final GraphRequest request =
GraphRequest.newMeRequest(loginResult.getAccessToken(), new
GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject me, GraphResponse response) {
if (response.getError() != null) {
} else {
try {
String id = "";
String firstName = "";
String lastName = "";
String email = "";
String userPicUrl = "";
String userName = "";
if (response.getJSONObject().has("id")) {
id = response.getJSONObject().get("id").toString();
}
if (response.getJSONObject().has("first_name")) {
firstName =
response.getJSONObject().get("first_name").toString();
userName = firstName;
}
if (response.getJSONObject().has("last_name")) {
lastName =
response.getJSONObject().get("last_name").toString();
if (!firstName.equals("")) {
userName = firstName + " " + lastName;
} else {
userName = lastName;
}
}
if (response.getJSONObject().has("email")) {
email = response.getJSONObject().get("email").toString();
}
if (response.getJSONObject().has("id")) {
userPicUrl = "https://graph.facebook.com/" + id + "/picture?
type=normal";
}
//send data to api......in live projects
//else display on the view........
binding.txtName.setText(userName);
binding.txtEmail.setText(email);
//use picasso or glide to display user profile picture in any
ImageView
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email");
request.setParameters(parameters);
request.executeAsync();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
}