Read facebook inbox mesaage using graph api. (Facebook SDK : 4.16.0) - android

I am new in using facebook api for Android. I am trying to read message from facebook using Graph Api. When I try to pass read_mailbox permission I get invalid permission error.
Here is the link I tried to refer.
https://developers.facebook.com/docs/graph-api/reference/v2.8/user/inbox
Here is the code how I am trying to do so.
loginButton.setReadPermissions(Arrays.asList("public_profile","user_friends","email","user_about_me","read_mailbox"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject json, GraphResponse response) {
if (response.getError() != null) {
System.out.println("ERROR" +response.getError().getErrorMessage());
} else {
System.out.println("Success");
try {
String jsonresult = String.valueOf(json);
System.out.println("JSON Result"+jsonresult);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email,inbox");
request.setParameters(parameters);
request.executeAsync();
}
Here is the link for permission which shows that read_mailbox is depricated.
https://developers.facebook.com/docs/facebook-login/permissions
But what is the alternative to this.
I Also wanted to send message, but I stuck here only. Any help is appriciated.
Thanks in advance

read_mailbox is deprecated and there is no alternative. It is not possible to read the user mailbox anymore.
Sending a message is easy with the Send Dialog: https://developers.facebook.com/docs/sharing/reference/send-dialog
...or the Message Dialog on Android: https://developers.facebook.com/docs/sharing/android#message

Related

Facebook SDK 4.5.0 returning wrong user id in android

In my App i am integrating facebook SDK 4.5.0.
To fetch details of user I am using method
GraphRequest request = GraphRequest.newMeRequest
In user json response i am getting wrong user id.
In case of IOS for same application and same user i get correct userId.
Can anyone help me to solve this?
This is my Code to fetch user details:
GraphRequest request = GraphRequest.newMeRequest(com.facebook.AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject user, GraphResponse response) {
if (user != null) {
doLoginFb(user);
}
}
});
request.executeAsync();
After V2.0API user id is "app-scoped" so the id can be different for same user on different apps.
You can refer to the documentation here for more details.
It also says this :
No matter what version they originally used to sign up for your app,
the ID will remain the same for people who have already logged into
your app. This change is backwards-compatible for anyone who has
logged into your app at any point in the past.
If you're not mapping IDs across apps, then no code changes should be required.
Get some public profile of user:
Bundle params = new Bundle();
params.putString("fields", "id,name,email");
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
Log.e("JSON",response.toString());
JSONObject data = response.getJSONObject();
//data.getString("id"),
//data.getString("name"),
//data.getString("email")
} catch (Exception e){
e.printStackTrace();
}
}
}
).executeAsync();

Getting all the photos in a user's profile using Facebook SDK on android [duplicate]

This question already has answers here:
Getting user photos using Facebook login on android
(2 answers)
Closed 7 years ago.
I'm able to get profile photo of a user using Facebook SDK 4.0 on android using this -
loginButton = (LoginButton)findViewById(R.id.login_button);
profile = (ImageView)findViewById(R.id.profile);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
String userId = loginResult.getAccessToken().getUserId();
String imageUrl = String.format("https://graph.facebook.com/%s/picture?type=large", userId);
Picasso.with(getBaseContext()).load(imageUrl).into(profile);
}
}
How to go about getting all the photos in a user's profile ? Any help is appreciated.
Firstly Make sure the AccessToken has user_photos permission
Then on the app, call a GraphRequest with a photos{link} field
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
try {
//This contains all the photos with array data>>link
JSONObject photosobject = object.getJSONObject("photos");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,picture,photos{link}");
request.setParameters(parameters);
request.executeAsync();
Then please refer to the Facebook Graph API tool to test the values you want
https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me%3Ffields%3Dphotos%7Blink%7D
Note: also make sure that you have the latest FacebookSDK

How to get user email and birthday from Facebook API Version v2.4

I am not able to get the facebook user email in API version v2.4. I have one app in api version2.3 it returns the email and other details of the user but now facebook updated the api version to new application. if i use the old application like veriosn <=2.3 its returning the user email by using the following code. But in api version 2.4 i am not able to get the email and date of birth.
GraphRequest.newMeRequest(result.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
#Override
public void onCompleted(JSONObject me, GraphResponse response)
{
if (response.getError() != null)
{
// handle error
}
else
{
Log.e("",""+me.toString());
Log.e("",""+response.getJSONObject().toString());
}
}
}).executeAsync();
You'll have to explicitly define which fields you want to retrieve with your request, for example /me?fields=id,name,email,birthday instead of just /me
See
https://developers.facebook.com/docs/apps/changelog#v2_4
To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data. For more details see the docs on how to request specific fields.
Since v2.4 you need include in your request the fields that you need.
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject userMe, GraphResponse response) {
if(userMe!=null){
//...... do your things
}
}
});
Bundle parameters = new Bundle();
//Add the fields that you need, you dont forget add the right permission
parameters.putString("fields","email,id,name,picture,birthday");
request.setParameters(parameters);
//Now you can execute
GraphRequest.executeBatchAsync(request);
Here is code block that worked for me:
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
final JSONObject jsonObject = response.getJSONObject();
try{
email = jsonObject.getString("email");
first_name = jsonObject.getString("first_name");
last_name = jsonObject.getString("last_name");
gender = jsonObject.getString("gender");
birthday = jsonObject.getString("birthday");
} catch (JSONException e){
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "email,first_name,last_name,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
Hope this helps!

Can't get Email address through Facebook Android SDK

I tried to use below code to get my facebook email address via my app, but return value is null.
I don't know what the reason, but I can get email address using Graph API explorer. Is there anybody know what is wrong with this code?
#Override
public void onCreate(Bundle savedInstanceState) {
// Request email address
GraphRequest.newMeRequest(
loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
public void onCompleted(JSONObject me, GraphResponse response) {
Log.d("TEST", response.toString());
if (response.getError() != null) {
// handle error
} else {
Log.d("TEST", me.toString());
String email = me.optString("email");
String id = me.optString("id");
Log.d("TEST", "Email:" + email);
}
}
}).executeAsync();
LoginButton authButton =(LoginButton) findViewById(R.id.login_button);
List<String> permissions = new ArrayList<>();
permissions.add("public_profile");
permissions.add("email");
authButton.setReadPermissions(permissions);
}
Finally I found the solution, using newMeRequest method fetches fields from a user object. In the case, I need email as an additional fields, so added “email” into fields parameter and request specific fields:
GraphRequest request = GraphRequest.newMeRequest(
//...
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
Set the read permissions before you make the request and do it in the following way:
GraphRequestAsyncTask request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject user, GraphResponse response) {
final Profile profile = Profile.getCurrentProfile();
if ((user != null) && (profile != null)) {
accessToken = AccessToken.getCurrentAccessToken();
if (accessToken.getDeclinedPermissions().isEmpty()) {
try {
String email = user.get("email").toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}).executeAsync();
You have to set permissions before the request.
Anywayt the email field may return null for two reasons:
1 - user didn't confirm the email address during the registration
2 - users could be signed up in Facebook using a phone number instead of email.
Documentation says:
Note, even if you request the email permission it is not guaranteed
you will get an email address. For example, if someone signed up for
Facebook with a phone number instead of an email address, the email
field may be empty.

Get friend interests using android facebook SDK

In my app my intention is to get some interests of a friend like music, books, tv. This information is public. For this I think I don't need to send a permission.
Based on facebook (poor)documentation, mainly this links:
Field Expansion to get friend interest I only need to pass friend ID in the expanded friends field.
Doing some tests on GraphExplorer I see it creates the following query to get movies form some friend:
MY_ID?fields=friends.uid(FRIEND_ID).fields(movies)
I use this and execute execute this code on a Request.newMyFriendRequest asynchronously:
Session activeSession = Session.getActiveSession();
if(activeSession.getState().isOpened()){
Request request = Request.newMyFriendsRequest(activeSession,
new GraphUserListCallback(){
#Override
public void onCompleted(List<GraphUser> users, Response response){
GraphUser user = users.get(0);
JSONObject friendLikes = user.getInnerJSONObject();
try {
JSONArray data = friendLikes.getJSONObject("friends").getJSONArray("data");
Log.i("JSON", friendLikes.toString());
addInterests(data, 0);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle bundle = new Bundle();
bundle.putString("fields", "friends.uid("+friendID+").fields(movies)");
request.setParameters(bundle);
request.executeAsync();
All times I execute this code I receive the following error from JSON:
W/System.err(694): org.json.JSONException: No value for friends.
If this isn't the way to get friend interests using GraphAPI, what do I need to do to get these values?
[EDIT] Solved posting friends_likes permission on Login. Permission is not sended inside Request.
I modified a bit my solution. If anyone is interested:
String fqlQuery = "SELECT music, books, movies FROM user where uid ="+friendID;
Bundle bundle = new Bundle();
bundle.putString("q", fqlQuery);
Request request = new Request(activeSession, "/fql", bundle, HttpMethod.GET,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
// TODO Auto-generated method stub
Log.i("INFO", response.toString());
}
});
Request.executeBatchAsync(request);

Categories

Resources