facebook realtime updates from android - android

How do I get facebook realtime updates with android .Basically everytime the user's friendlist changes I need to update the count.I used the code below but I get error msg :
{This method must be called with an app access_token.}, isFromCache:false}
I really appreciate any help.Thanks in Advance.
code:
Bundle params = new Bundle();
params.putString("object", "user");
params.putString("callback_url", "http://example.com/callback/update.php");
params.putString("fields", "friends");
params.putString("verify_token", "1234");
/* make the API call */
new Request(
session,
"/12345/subscriptions",
params,
HttpMethod.POST,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
Note: session =fb.getsession();

This error:
"This method must be called with an app access_token."
is quite straight-forward. You just need to make this API call with an App access token, instead of a normal access token (user token). To use it with your call simply add another parameter: access_token and make the call.
The App Access token is nothing but, APP_ID|APP_SECRET, or you can make the call (ref.)-
GET /oauth/access_token?
client_id={app-id}
&client_secret={app-secret}
&grant_type=client_credentials
But beware, its not recommended to expose app access token on the client side, since its like a password of your app.

Related

get all facebook ids /name of my friends

I have tried using the code below and if add old facebook sdk it workd fine but new sdk I get only the friend who is on app.How can I get the list of all friends in new sdk? .I really appreciate any help .Thanks in Advance.:
new Request(
MainActivity.facebook.getSession(),
"/me/friends",
null,
HttpMethod.GET,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
/* handle the result */
Toast.makeText(activity, response.toString(), 5).show();
}
}
).executeAsync();
Short Answer: You CanĀ“t.
Since v2.0, you can only get the friends who authorized your App too, for privacy reasons. An App should not know about anyone who does not use the App.
If you want the names to tag them, there is taggable_friends. If you want to invite them, use invitable_friends. But check out the docs for their limitations.

How to fetch all photos from Facebook in Android

I am developing an application in which I am using Facebook SDK for different purposes. Currently I have implemented Login through Facebook. Now my next task is to fetch all the photos of the logged in user and display all of them in a grid. I am following the official facebook developer website as a reference for this task. Following is the code that I am using to fetch the user photos.
new Request(
Session.getActiveSession(),
"/me/photos",
getRequestParameters(),
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
Log.d("Response ","Response " + response);
mpBar.setVisibility(View.GONE);
}
}
).executeAsync();
private Bundle getRequestParameters()
{
String accessToken = LoginSigbUpCustomerNew.mytoken;
Bundle parameters = new Bundle(1);
parameters.putString("access_token", accessToken);
return parameters;
}
Here, I am getting response like this :-
{Response: responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[]}}, error: null, isFromCache:false}
I cant understand why the data array is empty. Also I am requesting the user_photos permission from user. So where the thing is wrong.
I am new to this Facebook SDK and dont know how to solve this. If anyone can help me it would be great.
Thanks in advance.
Your code to request your photos is just fine according to the documentation of Facebook SDK however I think that you forgot to request permission from the user who's logged in (you in this case).
Copied from Facebook SDK documentation: A user access token with user_photos permission is required to see all photos that person is tagged in or has uploaded.
The following link gives you a small tutorial on how to add access tokens to the login button: https://developers.facebook.com/docs/android/login-with-facebook/v2.2?locale=en_GB#step3
In your case it would be (code snippet is for activities only, for fragments: click the url above and follow the tutorial there. Pretty straight forward):
...
LoginButton authButton = (LoginButton) findViewById(R.id.ID_OF_YOUR_LOGIN_BUTTON);
authButton.setReadPermissions(Arrays.asList("user_photos"));
...
In your overriding onCreate method.
Method two:
...
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this)
.setPermissions(Arrays.asList("user_photos"))
.setCallback(YOUR_CALLBACK_CLASS_VARIABLE));
} else {
// Open an active session. Basically happens when someone is already logged in.
}
...
Before making the call to request all the user photos, you can simply open a new request when the user hasn't logged in yet. This can happen in View.OnClickListener class of a Button as it seems that you're not LoginButton of Facebook.
YOUR_CALLBACK_CLASS_VARIABLE is your class implementing Session.StatusCallback. I'm quite certain you know what it is and what it does.
Method three:
...
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, Arrays.asList("user_photos"));
session.requestNewReadPermissions(newPermissionsRequest);
...
When the user is already logged in (so after the user is logged in) and you wish to request additional permissions from the user to access his photos or whatsoever. This is by the way all written in the docs of Facebook SDK for Android.
For more information: https://developers.facebook.com/docs/android/login-with-facebook/v2.2?locale=en_GB#step3
If the request returns an empty result, check whether the used Access Token contains the user_photos permission:
https://developers.facebook.com/docs/graph-api/reference/v2.2/user/photos#readperms
You can inspect the Acces Token via
https://developers.facebook.com/docs/facebook-login/access-tokens#debug
Request:
GET /debug_token?
input_token={input-token}&
access_token={access-token}

Getting incorrect facebook friend id

I am getting a wrong Facebook Friend Id: I am getting some long alphanumeric value in id which, among the other things, is also not unique (e.g. it changes on each call).
Actually I want to use this id as a unique identifier to login, but since it changes every time is not so helpful.
So what's wrong here? What else can I do for unique identification? Any suggestion will be appreciated.
I am using this code to get friend list:
Bundle required = new Bundle();
required.putString("fields", "uid, name, picture.type(square), gender, email");
//Request for friendlist from facebook
Request req = new Request(session, "me/taggable_friends", required, HttpMethod.GET,
new Callback() {
#Override
public void onCompleted(Response response)
{
is2=response.getGraphObject();
JSONObject json = is2.getInnerJSONObject();
readResponse(json);
String str = json.toString();
saveFileOnSD("data", str);
// generateNoteOnSD("frinedresponse", str);
Log.v(TAG, str);
}
});
Request.executeBatchAsync(req);
The problem is that you are using taggable_friends. Taggable_friends are for if you want to tag friends. It is by design that the id changes every single time you call it.
If you want friends you should use /me/friends but it will only return friends that are already using the app.
Depending on what you need all friends for some solutions can be found here: https://developers.facebook.com/docs/apps/faq
On 30th April 2014 facebook make so many changes, so if you create you facebook APP ID after this date you can't get facebook id of your friend with taggable_friend, so for getting facebook id of your friend you need to use "me/friends instead of me/taggable_friend", but you get only those friend list who are using your app.

Facebook android API function executeGraphPathRequestAsync not giving expected results

I'm trying to discover if a user likes a specific facebook page by using Graph API inside an android application using the FB SDK v3.
The following call works and reports all of the likes of the user along with every other detail to do with each resulting item.
Request.executeGraphPathRequestAsync(session, "me/likes", new Request.Callback()
Since I'm not interested in any peripheral data, I just want to get the id back so I can lookup the page I'm interested in, but when I add the parameters as follows...
Request.executeGraphPathRequestAsync(session, "me/likes?fields=id", new Request.Callback()
... no object is returned from the call. If I post the same queries in the Graph API explorer they give back the expected results.
In addition to this question, is it at all possible to limit the result of the query to just the page ID to begin with, since I already know that piece of information? I have no use for the rest of the data being returned and would prefer not to chew up user bandwidth unnecessarily. I've searched high and low and it doesn't seem possible. Even the FQL explorer doesn't work when trying to limit by both user and page iD as the example suggests. I get an empty result set when adding 'AND page_id=xxx' to the query.
As far as i know, you cant run queries with field in 'executeGraphPathRequestAsync'.
instead use 'Request' and run in with 'RequestAsyncTask'.
Short example:
String grapPath = "me/home";
Bundle params = new Bundle();
params.putString("fields", "type,id,object_id,created_time,from,picture,likes");
params.putString("limit", "500");
Request request = new Request(session, grapPath, params, HttpMethod.GET, new Callback()
{
#Override
public void onCompleted(Response response)
{
if (response != null)
{
Log.d(TAG_NAME, "response: " + response.toString());
}
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
Hope i managed to help you :)
The Facebook SDK will add the "?access_token" to the end of the graph path string. So the graph command ends up looking like:
me/likes?fields=id?access_token
Break out the fields into a params Bundle.
Found this issue resolved in a Facebook bug post:
http://developers.facebook.com/bugs/314504765319932?browse=search_5116b87d245900257505334

Facebook Android Checkins

Ok so I have been researching the facebook api and Noticed that they released a new SDK. I have been trying to implement a checkin to a known place id.
I have seen posts that use the facebook.request("me/checkins", params, "POST"); to checkin using the facebook instance.
I realize that checkins are depreciated and that i should be posting with a location/place id instead. The problem comes when i want to search for all of my friends that have been to that location within that past 24 hours. I do not want to filter through all of my friends posts to retrieve the ones with my known locations.
With checkins i simply made a call to the placeid/checkins and filtered out all checkins that were too old to matter.
Has anyone gotten checkins to work using Request?
Is there a graphpath that will allow me to post a checkin to a place? me/checkins perhaps?
I am trying to use something like Request(session, "me/checkins", parameters, POST) But i have been unsuccessful so far.
If there is not a way to do this with Session and Request then do Facebook and asyncFacebook runners work well with SSO?Can they be mixed?
This is my code to post a checkin.
params.putString("place", "166793820034304");
params.putString("message", "checking in "
params.putString("coordinates", "{\"longitude\":-122.1477782,\"latitude\":37.48566}");
Request.Callback callback6 = new Request.Callback() {
public void onCompleted(Response response) {
// response here
}
};
Request request6 = new Request(session, "me/checkins", params, HttpMethod.POST, callback6);
RequestAsyncTask task6 = new RequestAsyncTask(request6);
task6.execute();

Categories

Resources