When a user logs into Facebook, I'd like to get their name and profile picture. For the name, I was looking at this link with this code:
private void makeMeRequest(final Session session) {
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
// If the response is successful
if (session == Session.getActiveSession()) {
if (user != null) {
String facebookId = user.getId();
}
}
if (response.getError() != null) {
// Handle error
}
}
});
request.executeAsync();
}
But I was unable to import the Request class and it looks like the class is no longer available. Is there another class that could get the same job done?
The Session class has been removed from the the SDK at the version 4.0 - at March 25, 2015. You can find information about this in this link
Call newMeRequest using accressToken
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
String name = object.
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Why are you using Parse? Instead, go directly to Facebook and use Graph API. Here's the link:
https://developers.facebook.com/docs/android/graph
Related
I am trying to use Firebase authentication with Facebook.
I have successfully implemented the login part and stuck while getting the user details from facebook using accessToken.
Here is the code:
loginButton.setReadPermissions("email", "public_profile" );
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject me, GraphResponse response) {
Log.e("FBRESPONSE",me.toString());
if (response.getError() != null) {
// handle error
} else {
if (me.has("picture")) {
try {
f_photo = me.getJSONObject("picture").getJSONObject("data").getString("url");
} catch (JSONException e) {
e.printStackTrace();
}
}
f_email = me.optString("email");
String id = me.optString("id");
f_name=me.optString("first_name")+" "+me.optString("last_name");;
handleFacebookAccessToken(loginResult.getAccessToken());
}
}
}).executeAsync();
}
});
This is the JSON response i get when called this.
E/FBRESPONSE: {"name":"Vishnu Reddy","id":"855235691328214"}
How do i get email, profile picture URL?
Please help. Thanks in advance.
You need to use the Facebook Graph API. You need to have your user signed in and call the AccessToken.refreshCurrentAccessTokenAsync(); and initiate a request to the user data-fields you need.
I'm trying to get the facebook's user's timeline in my Android app.
Here my code :
mLoginButton.setReadPermissions(Arrays.asList("user_about_me", "user_friends", "user_likes",
"user_photos", "user_relationships", "user_posts",
"user_status"));
// If using in a fragment
mLoginButton.setFragment(this);
// Other app specific specialization
// Callback registration
mLoginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
mAccessToken = loginResult.getAccessToken();
for (String permission : loginResult.getRecentlyGrantedPermissions()) {
Log.d(LOG_TAG, "Granted Permission:" + permission);
}
getUserFeed();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
And after the login, I launch this :
private void getUserFeed() {
Bundle params = new Bundle();
params.putInt("limit", 25);
params.putString("fields", "id,name,link,full_picture,message,story,picture,type,place,from,to");
params.putBoolean("summary", true);
/* make the API call */
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/home",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
final JSONArray data = response.getJSONObject().getJSONArray("data");
//currentJson = response.getJSONObject();
} catch (JSONException e) {
Log.e("Error: ", e.toString());
}
}
}
).executeAsync();
}
I have this respond code from Facebook :
Requires extended permission: read_stream
I know this permission is depreceted, I'm using the latest API 2.5.
Do you know if we can continue to get the user's timeline now, if I replace the "/me/home" by "/me/feed" it's ok, but I just get my posts, not my entire timeline.
Thanks :)
Do you know if we can continue to get the user's timeline now,
No, you can’t.
if I replace the "/me/home" by "/me/feed" it's ok, but I just get my posts, not my entire timeline.
/me/home was deprecated together with the permission.
/me/feed is what you can get now, and that’s it.
Which posts you can expect to see is listed here: https://developers.facebook.com/docs/graph-api/reference/v2.5/user/feed#readperms
Currently, I am using this tutorial to create the facebook login.
"https://github.com/ParsePlatform/ParseUI-Android"
The Parse user JSON didn't return me an email or gender. I also have research around but I still can't find the solution (well, at least, some other still returning null). Anyone can explain the reason and how to solve it?
facebookLoginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
loadingStart(false); // Facebook login pop-up already has a spinner
if (config.isFacebookLoginNeedPublishPermissions()) {
ParseFacebookUtils.logInWithPublishPermissionsInBackground(getActivity(),
Arrays.asList(
"public_profile", "email"), facebookLoginCallbackV4);
Log.i("fbUser","Publish");
} else {
ParseFacebookUtils.logInWithReadPermissionsInBackground(getActivity(),
Arrays.asList( "public_profile", "email"), facebookLoginCallbackV4);
Log.i("fbUser", "Read");
}
}
});
private LogInCallback facebookLoginCallbackV4 = new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (isActivityDestroyed()) {
return;
}
if (user == null) {
loadingFinish();
if (e != null) {
showToast(R.string.com_parse_ui_facebook_login_failed_toast);
debugLog(getString(R.string.com_parse_ui_login_warning_facebook_login_failed) +
e.toString());
}
} else if (user.isNew()) {
GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject fbUser,
GraphResponse response) {
/*
If we were able to successfully retrieve the Facebook
user's name, let's set it on the fullName field.
*/
ParseUser parseUser = ParseUser.getCurrentUser();
if (fbUser != null && parseUser != null && fbUser.optString("name").length() > 0) {
Log.i("FbUSer", response.getJSONObject().toString());
Log.d("FbUSer-Email",fbUser.optString("email"));
Log.i("FbUSer-gender",fbUser.optString("gender"));
parseUser.put(USER_OBJECT_NAME_FIELD, fbUser.optString("name"));
parseUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
debugLog(getString(
R.string.com_parse_ui_login_warning_facebook_login_user_update_failed) +
e.toString());
}
loginSuccess();
}
});
}
loginSuccess();
}
}
).executeAsync();
} else {
loginSuccess();
}
}
};
Logcat Section return:
{"id":"1234524124124","name":"FbUser NAme"}
I just found the answer. I just need to set the parameter so I can get the data I want.
GraphRequest request = GraphRequest.newMeRequest(...);
Bundle parameters = new Bundle();
parameters.putString("fields","id,name,link,email,first_name,last_name,gender");
request.setParameters(parameters);
request.executeAsync();
Reference: https://stackoverflow.com/a/32579366/4961962
for more user info you have to add permissoin to access those detail like email, user_about_me at facebook developer console
select app at facebook developer and go -> app details -> Configure
App Center Permissions
Above is my code but i cant find out together usage, always gives error
Cannot pass a publish or manage permission (publish_actions) to a request for read authorization
This is my Permission list
private Collection<String> permissions = new ArrayList<>();
permissions.add("public_profile");
permissions.add("email");
permissions.add("user_birthday");
permissions.add("publish_actions");
And this is login request
ParseFacebookUtils.logInWithReadPermissionsInBackground(activity, permissions, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException parseException) {
if (parseUser == null) {
} else {
}
}
});
How can i use this together?
After long hours, this is solution. You must behave twice login to facebook. Once is publish and other one is read permissions. If you need public profile data , just publish permission is enough but in my case i need birthday, email, etc.. So code is below;
These are my permissions lists;
Collection<String> readPermissions = new ArrayList<>();
readPermissions.add("public_profile");
readPermissions.add("email");
readPermissions.add("user_birthday");
Collection<String> publishPermissions = new ArrayList<>();
publishPermissions.add("publish_actions");
Firstly, I should login with readpermission
ParseFacebookUtils.logInWithReadPermissionsInBackground(activity, readPermissions, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException parseException) {
if (parseUser == null) {
listener.onFailure(new UserCancelledFacebookLogin());
} else {
getPublishPermissions(parseUser);
}
}
});
After this, here my "getPublishPermissions" method; FacebookRequestListener is my own listener , don't care/mind delete it.
public void getPublishPermissions(final ParseUser parseUser) {
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) {
// User succesfully login with all permissions
// After this with these json and ParseUser , you can save your user to Parse
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,last_name,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException facebookException) {
}
});
LoginManager.getInstance().logInWithPublishPermissions(activity, publishPermissions);
}
that's all folks =)
happy coding to everyone
The error message means you should not request read and write permissions at the same time. Login with read permissions when the User enters your App, request write permission (publish_actions) only right before you post.
Use ParseFacebookUtils.logInWithPublishPermissionsInBackground for that.
That error message is already well known, take a look at some other thread about it:
How to set permission "publish_actions" in LoginButton using facebook sdk?
Facebook, setReadPermissions and setPublishPermissions
facebook login Cannot pass a publish or manage permission (email) to a request for read authorization
You need to do a POST request instead of a GET one. See:
https://developers.facebook.com/docs/graph-api/reference/v2.2/user/scores/#publish
Sample:
Bundle param = new Bundle();
param.putString("message", "picture caption");
param.putByteArray("picture", ImageBytes);
mAsyncRunner.request("me/photos", param, "POST", new SampleUploadListener());
this is your answer check link
How can i get Work and Education information plus cover from facebook.
I got other details with this function
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
fbName=user.getName();
fbId=user.getId();
String email = (String) response.getGraphObject().getProperty("email");
}
}
But i am unable to get Education info and work info?
I made sure I asked for the appropriate permissions
LoginButton lb = (LoginButton) view.findViewById(R.id.login_button);
lb.setReadPermissions("user_work_history", "user_education_history");
Then I did a me request to get data
Session s = Session.getActiveSession();
Request r = Request.newMeRequest(s, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
}
}
});
r.executeAsync();
user.getProperty("work") and user.getProperty("education") both give data then.