Can only add one param to Bundle before facebook request - android

I am messing around with the facebook graph api for the first time and ran into an issue that doesn't to be happening to anyone else. If this is a duplicate, I apologize.
Bundle searchParams = new Bundle();
searchParams.putString("q", "coffee");
searchParams.putString("type", "place");
String response = facebook.request("search", searchParams);
In the above code, I am attempting to duplicate the example facebook has on their developer graph api page where they search for coffee places. The interesting thing that happens is when I pass in the Bundle, I have those two params that I have set.
public String request(String graphPath, Bundle params, String httpMethod)
throws FileNotFoundException, MalformedURLException, IOException {
params.putString("format", "json");
if (isSessionValid()) {
params.putString(TOKEN, getAccessToken());
}
String url = (graphPath != null) ? GRAPH_BASE_URL + graphPath
: RESTSERVER_URL;
return Util.openUrl(url, httpMethod, params);
}
As I step through the facebook request function (seen above), I noticed that the first param I put was popped out of the Bundle after the access_token was put in. This resulted in my Bundle containing only {"type", "place"}, and facebook's params. Through the debugger I noticed that the threshold was set to 3. I'm guessing that's my issue, but don't know how or where to change that.
Any thoughts?
Update:
I haven't been able to figure the issue out that I had. But was able to do what I needed when upgrading to facebook sdk 3.0b. Thanks.

I can do this and I call the request function like this.
String response = facebook.request("search", searchParams,"GET");

Related

me/friends empty friend list

I am trying to get friend list using me/frineds tag. There is no user of the app so far so i have added some frirneds as a tester of the app. But they are not showing in the response i am getting empty data list. Please guide me what i am doing wrong here. Here is the code
Bundle required = new Bundle();
required.putString("fields", "id, name, picture.type(square), gender, email");
//Request for friendlist from facebook
Request req = new Request(session, "me/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);
With v2.0, only users who are using the App too show up in the friend list. It is not enough to add someone as Tester in the App Settings, a user has to authorize your App.
See changelog for more information: https://developers.facebook.com/docs/apps/changelog
There is also "invitable_friends" and "taggable_friends", the first one is for game invitations only and the second one is for tagging only.
This has been previously asked, and the thread listed below has a ton of details. The only work around is if your app is a game. Here is the link to the other post: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app

Facebook - Android - Total Likes - Graph API

I'm attempting to retrieve the total number of likes for a selected Facebook Page POST
According to this page:
How to get Likes Count when searching Facebook Graph API with search=xxx
I've tried to refer to the answers in that post and to build my code based on that.Here's the current code that I have based on it
private void totalLikes(String post_id){
Bundle bun = new Bundle();
bun.putString("summary","true");
new Request(Session.getActiveSession(), post_id+"/likes/",bun, HttpMethod.POST,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
Toast.makeText(getBaseContext(),String.valueOf(response),Toast.LENGTH_SHORT).show();
}
}).executeAsync();
}
Unfortunately the response given is not what was expected,I was thinking that it could be due to my Bundle parameters.So I would like to know what are the actual bundle parameters I should be putting for the code?Thank you for the help :)!
In your Bundle string addition, try changing it to bun.putString("fields","likes.summary(true)"); and change the graph path to just the post_id. There should end up being a "summary" field in the response after that.
EDIT: Here's another method that might work better with permissions.
Graph path: post_id + "/likes"
with
bun.putString("include_summary", "true");
I tried messing around in the explorer and couldn't get it to work, but you might have better luck.

URL Query parameter string "=" converts to "&"

In my Android project I'm using Robospice with spring-android. Which works fine for all REST communication. But for the below request query parameter "=" is getting converted to "&". Because of this the request is getting failed.
Query String: tags=["keywords:default=hello"]
By checking the logs the request is converted as below for making call by the library.
http://XXX/rest/media/search?token=123&tags=%5B%22keywords:default&hello%22%5D
here "=" sign is converted to "&" in "keywords:default=hello"
Request Class
here tags = String.format("[\"keywords:default=%s\"]", mTag);
#Override
public MVMediaSearch loadDataFromNetwork() throws Exception
{
String search="";
if(!tags.equals(Constants.EMPTY_DATA))
search="&tags="+tags;
return getRestTemplate().getForObject( Constants.BASE_URL+"/media/search?token="+token+search, MVMediaSearch.class );
}
If I fire the URL in a browser, I'm getting error. And if I change the '&' sign to its corresponding url encoded value in browser, it works fine.
I also have the same issue.
For alternative, I use getForObject(java.net.URI, java.lang.Class).
URI uri = new URI(Constants.BASE_URL+"/media/search?token="+token+search);
getRestTemplate().getForObject(uri, MVMediaSearch.class );
http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html#getForObject(java.net.URI, java.lang.Class)
You can do something like this:
URI uri = new URI(
"http",
Constants.BASE_URL,
"/media/search?token=",
token,
search,
null);
String request = uri.toASCIIString();
take a look at THIS and see if you understand (you have to adapt to your code - this is not completely done for you)

Android Facebook Oauthexception with valid access token

I am trying to communicate with Facebook doing simple things. At the moment I can log a user in and post to their wall as them. But for whatever reason, I can't seem to access public information such as their name. I consistently get this error:
{"error":{"message":"Syntax error \"Expected end of string instead of \"?\".\" at character 4: name?access_token=MYACCESSTOKEN","type":"OAuthException","code":2500}}
Here is the call:
SampleRequestListener srl = new SampleRequestListener();
AsyncFacebookRunner afr = new AsyncFacebookRunner(facebook);
afr.request("http://graph.facebook.com/me?fields=name", (RequestListener)srl);
That call is made within a validated session (in the onComplete portion of the DialogListener for .Authorize). Using my access_token and the exact same string as above I can get the request to work just fine at http://developers.facebook.com/tools/explorer/
The error occurs whilst parsing the response in the RequestListener.onComplete
JSONObject json = Util.parseJson(response);
final String name = json.getString("name");
System.out.println("Hi, my name is " + name);
Thank you for your time. All input is welcomed.
UPDATE *
There are two things going on. In the facebook API, Util.openUrl was appending a "?" between the field name and the access_token (as the answer below pointed out). This seems odd. I wonder if I pulled an old version of the API or something. You would think that would be set up correctly.
Also, I called the method incorrectly:
This:
afr.request("http://graph.facebook.com/me?fields=name", (RequestListener)srl);
Should be:
afr.request("me?fields=name", (RequestListener)srl);
If you are using com.facebook.Request class then just use the following form of constructor: Request(Session session, String graphPath, Bundle parameters, HttpMethod httpMethod, Callback callback) and pass your parameters in "parameters" parameter.
Just like:
if (GlobalApplication.accessToken != null && !GlobalApplication.accessToken.isExpired()) {
/* make the API call */
Bundle b = new Bundle();
b.putString("fields", "id,created_time,description,embed_html,name,source,picture");
new GraphRequest(GlobalApplication.accessToken, "/me/videos",
b, HttpMethod.GET, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
/* handle the result */
Log.i("", "");
String string = response.toString();
JSONObject object = response.getJSONObject();
JSONArray array = response.getJSONArray();
Log.i("", "");
}
}).executeAsync();
Looks like the actual request being sent is something like
/me?fields=name?access_token=MYACCESSTOKEN
and that of course is wrong; it should be an ampersand before the second parameter and not a question mark.
You’d have to look for the location in the code where the access token is added as parameter. At that point there should be a check for whether this URL already contains a question mark or not before appending the access_token parameter.

Not able to tag photos on facebook using the android sdk

I am using the following code (from sdk examples "Hackbook") to add a tag to a photo after uploading it.
json = Util.parseJson(response);
photo_id = json.getString("id");
String relativePath = photo_id + "/tags/" + Utility.userUID;
Bundle params = new Bundle();
params.putString("x", "25");
params.putString("y", "25");
Utility.mAsyncRunner.request(relativePath, params, "POST", new TagPhotoRequestListener(), null);
However, I sometimes (not always) receive the the response from facebook as "false" instead of "true". Is there a specific reason for this?
Additionally, is there a way to tag the photo while uploading it, instead of making an additional call?

Categories

Resources