如何在 android 中集成 facebook 并登录并获取用户详细信息和注销。
代码 JSONObject json = Util.parseJson(mFacebook.request("me"));
给我null。
之后我从捆绑包中获得访问令牌OnComplete()
。
如何在 android 中集成 facebook 并登录并获取用户详细信息和注销。
代码 JSONObject json = Util.parseJson(mFacebook.request("me"));
给我null。
之后我从捆绑包中获得访问令牌OnComplete()
。
这对我有用,试试这个:
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);
}
}