I'm using the newest Facebook SDK for Android and try to get the albums of a user but only several fields of them.
My problem is that the fields param gets ignored and I get all data like likes etc....
What's wrong with my request?
Bundle params = new Bundle();
params.putString("fields", "count,name,id,created_time,updated_time,photos");
Request request = new Request(session, "me/albums", params, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
// TODO Auto-generated method stub
System.out.println(response);
}
});
Requires the user_photos or friend_photos permission
You need that permission!
Related
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();
I am new to android development. I am trying to post photo from an android app to user's facebook album. I added following code after I logged in the user with permissions "user_friends"
new GraphRequest(
MainFragment.mAccessToken.getCurrentAccessToken(),
"/me/photos",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
// This is my code for user login at app start
post = (Button)view.findViewById(R.id.button1);
mLoginManager = LoginManager.getInstance();
mLoginManager.registerCallback(mCallbackManager, mCallback);
mLoginManager.logInWithReadPermissions(this, Arrays.asList("user_friends")); //user_friends // publish_actions
Please guide me
I did a mistake .. Firstly, my question was incomplete .. I have found the issue .. Following seems to be working.
MainFragment.mLoginManager.logInWithPublishPermissions(this, Arrays.asList("publish_actions"));
Bundle params = new Bundle();
params.putString("url", "http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg");
/* make the API call /
new GraphRequest(
MainFragment.mAccessToken.getCurrentAccessToken(),
"/me/photos",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/ handle the result */
Log.d("MainFragment:", "onCompleted mAccessToken = " + MainFragment.mAccessToken.getCurrentAccessToken());
}
}
).executeAsync();
here is the facebook sample
Bundle params = new Bundle();
params.putString("source", "{image-data}");
/* make the API call */
new Request(
session,
"/me/photos",
params,
HttpMethod.POST,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
what is {image-data}
I tried use byte[].toString, file.toString, path of file. not work.
so how to upload photo with this api?
facebook doc is wrong.
change
params.putString("source", "{image-data}");
to
params.putByteArray("source", "{image-data}");
If you want to post a photo, use one of the Request.newUploadPhotoRequest methods, it will set everything up for you.
See https://developers.facebook.com/docs/reference/android/current/class/Request/#newUploadPhotoRequest
I want to get facebook page about, description, location, basic info etc. I have got the access token for page. I am getting likes of page like this:
public void getLikes (String id,final TextView likes,final int position)
{
Session session = Session.getActiveSession();
Bundle params = new Bundle();
params.putString("id", id);
params.putString("fields", "likes");
Request request = new Request(session, "search", params, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
try {
JSONObject res = response.getGraphObject().getInnerJSONObject().getJSONArray("data").getJSONObject(0);
if (res.length()>1){
likes.setText(String.valueOf(res.getInt("likes")));
Preferences.data.getData().get(position).setLikes(String.valueOf(res.getInt("likes")));
}
else {
Preferences.data.getData().get(position).setLikes("No Likes");
likes.setText("0");
}
} catch (Exception e) {
}
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
If I add more parameters in bundle, I get the value of last parameter added, what should I do. Facebook's documentation about graph api is not much clear.
This isn't an issue specific to the API, you're just only using one field of the response (.getJSONObject(0))
if you request multiple fields you need to look at each of them, currently you're only fetching the first
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);