From within my Android application, i am trying to implement a simple "Like Button" functionality.
We have already executed Facebook log in from the same app.
We have also created an Open Graph Like action & a Link object as well on our App's Dashboard.
This is the code that we are using to publish the like.
public static void likeFacebookPage(Context context,String pageUrl)
{
Bundle params = new Bundle();
params.putString("object",pageUrl);
Request request = new Request(
Session.getActiveSession(),
"me/og.likes",
params,
HttpMethod.POST
);
Response response = request.executeAndWait();
// handle the response
}
In the pageUrl parameter, we have:
http://www.facebook.com/<object_id>
"object_id" is the Id of the Link Object that we have created in Open Graph.
After executing the above method, the response object received as follows:
{Response: responseCode: 200, graphObject: null, error: {HttpStatus: -1, errorCode: -1, errorType: null, errorMessage: null}, isFromCache:false}
But the Like count is not getting updated, leading us to believe that this process is not working.
We have also included the permissions "publish_actions" & "user_likes" in our code as well as on the App Dashboard.
Kindly help us with configuring this functionality.
Thanks in advance.
As described here https://developers.facebook.com/docs/reference/api/publishing/ you publish a like to an object like so with the publish_actions and publish_stream permissions:
Bundle postParams = new Bundle();
Request request = new Request(Session.getActiveSession(),
FB_OBJECT_ID+"/likes", postParams, HttpMethod.POST, new Callback() {
#Override
public void onCompleted(Response response) {
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
Where your FB_OBJECT_ID is simply the ID of the object. I.e if you want to like a photo, FB_OBJECT_ID would be the pid. For an album, FB_OBJECT_ID would be the aid.
Related
I need to get the age_range from facebook.
When i use /me?fields=age_range in my GraphRequest.newGraphPathRequest i get an error.
The error is that graphObject returns null. I've tried using /me and it works i get the correct user data though when i use /me?fields=age_range i get this error.
Testingļ¹ {Response: responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 2500, errorType: OAuthException, errorMessage: An active access token must be used to query information about the current user.}}
This is the code i've been trying to fix.
GraphRequest request = GraphRequest.newGraphPathRequest(accessToken,
"/me?fields=age_range",
new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
Log.v("Testing", graphResponse.toString());
}
});
request.executeAsync();
You should not put "/me?fields=age_range" as the path in your GraphRequest.
Instead, only use "/me" as the path, and then create a new Bundle, and put "fields" as the key, and "age_range" as a string value, and set the new bundle as the parameters on the GraphRequest.
GraphRequest request = ...
...
Bundle params = new Bundle();
params.putString("fields", "age_range");
request.setParameters(params);
request.executeAsync();
...
I would like to get realtime_update when user's friendlist changes and am trying to subscribe to friends object .I have the code below where I added params.putString("access_token", "12|wewee"); because I used to get {This method must be called with an app access_token.}, isFromCache:false} .Now I get the response as displayed below.I would like to get realtime subscription .How do I make it work? I really appreciate any help.Thanks in Advance.
code:
Bundle params = new Bundle();
params.putString("object", "user");
params.putString("access_token", "12|wewee");
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();
added param to get app_access_tokem:
params.putString("access_token", "12|wewee");
and I get this response :
{Response: responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"FACEBOOK_NON_JSON_RESULT":null}}, error: null, isFromCache:false}
I try to create a common object with this code:
Bundle params = new Bundle();
params.putString("type", "fitness.course");
params.putString("url", "http://samples.ogp.me/136756249803614");
param.putString("title", "Sample Course");
params.putString("description", "");
Request request = new Request(
Session.getActiveSession(),
"me/objects/fitness.course",
params,
HttpMethod.POST, new Request.Callback(){
#Override
public void onCompleted(Response response) {
if(response.getError()==null)
System.out.println("Object succesfully created!!");
else
System.out.println("Object NOT Created!!"+response.getError());
}}
);
Response response = request.executeAndWait();
but I read this in my Logcat:
10-07 15:33:04.009: I/System.out(23284): Object NOT created!! {HttpStatus: 400, errorCode: 2500, errorType: OAuthException, errorMessage: Cannot specify type in both the path and query parameter.}
I dont understand the error message... What do I do to create a Course object and update the data (gps status) of my course? I need of a back end server to store the data of my course? The back-end server is only an option?
Your error message says all, you're supplying og:type from both your POST request and your URL.
As of the new FB OpenGraph SDK, you can have ad-hoc URL-less objects. What you're trying to do is use that API while at the same time using the self-hosted version of the API.
I'd suggest either taking the URL parameter out, taking og:type out of the URL you have set up, and definitely giving the documentation a second read. Here is the documentation for self hosted objects and here is the article for app owned objects.
It's my fist post here, so please, go easy :)
I'm integrating Facebook SDK in an Android application. After the user logs in, I want to show him the app's last post in facebook, so I'm triYng to get it with(as graph object):
Bundle params = new Bundle();
params.putInt("limit", 1);
Request request = new Request(session, inRequestId + "/posts/?limit=1&access_token=" +
session.getAccessToken().toString(), params, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
if(response != null){
if(response.getError() != null)
Toast.makeText(getActivity(), "Error retrieveng last post", Toast.LENGTH_SHORT).show();
else {
updateFacebookView( response.getGraphObject().getInnerJSONObject() );
}
}
}
});
request.executeAsync();
This works, fine I think, but if I'm triyng to make it without graphPath's limit=1 parameter (inRequestId + "/posts/?access_token=" + session.getAccessToken().toString()), it has no limit, and if I don't put bundle param "limit", (as string or as int tested), it gives me this error:
09-30 17:51:47.094: E/caca(19703): {HttpStatus: 400, errorCode: 190, errorType:
OAuthException, errorMessage: Malformed access token
CAAT76i2gxTgBAKyLzc8SI6y7V1pGJ0fmbLWCtuKdhHIEZAQBA0jYx4YqZB8IgRDJMUlw1XrvZCLJ8kxKdZCRG3LNbrVL8fB34ZBlyvlqadT192MCWMkst1lMSdFwtRVPWiSNfBfi8Gq2RHZCWskrBVTjAwPDKyDMGLSU8sPnXfe0r2tsZBdZCXLCOJGQtE76sJkr7n8SdOU4j1KopkvT0Mux7QBGf7ZBXtCRqDnsZAZCxSNHAZDZD?access_token=CAAT76i2gxTgBAKyLzc8SI6y7V1pGJ0fmbLWCtuKdhHIEZAQBA0jYx4YqZB8IgRDJMUlw1XrvZCLJ8kxKdZCRG3LNbrVL8fB34ZBlyvlqadT192MCWMkst1lMSdFwtRVPWiSNfBfi8Gq2RHZCWskrBVTjAwPDKyDMGLSU8sPnXfe0r2tsZBdZCXLCOJGQtE76sJkr7n8SdOU4j1KopkvT0Mux7QBGf7ZBXtCRqDnsZAZCxSNHAZDZD}
Am I not using this correctly or it's a bug?
Thanks in advance
The second parameter for the constructor is the "path" of the request, but you're also putting request parameters into the path, which messes with the other parameters the Request class may try to put on your behalf. A couple of things to do:
Don't put request parameters in the path parameter, stick them into the "params" bundle.
Don't put the access token (the Request class automatically gets it from the session object).
Try something like this:
Bundle params = new Bundle();
params.putInt("limit", 1);
Request request = new Request(session, inRequestId + "/posts", params, HttpMethod.GET, new Request.Callback() {
...
});
request.executeAsync();
When trying to post an activity to Facebook from a native Android application, I get the following response after executing the request:
{
Response: responseCode: 200,
graphObject: null,
error: {
HttpStatus: -1,
errorCode: -1,
errorType: null,
errorMessage: null
},
isFromCache: false
}
My app gets the publish_action permission from the user.
public void publishActivity(){
session = Session.getActiveSession();
openSession();
Bundle params = new Bundle();
params.putString("highest", "http://www.my-url.com/graph.html");
Request request = new Request(
Session.getActiveSession(),
"me/namespace:play",
params,
HttpMethod.POST
);
Response response = request.executeAndWait();
Log.d("DEBUG",response.toString());
}
public void openSession(){
if(session.isClosed()){
Session.OpenRequest openRequest = new Session.OpenRequest(GameActivity.get());
openRequest.setPermissions(PERMISSIONS);
session.openForPublish(openRequest);
}
}
(I replaced the url and the namespace. In my code, they are correct.)
The problem appeared to be in the KeyHash provided by the namespace. It had changed, and it was not added in the Facebook Developers page yet.