Got an error when post message with image on facebook - ios

Please help me to solve this below error :
Error Domain=com.facebook.sdk Code=5 "(null)" UserInfo={com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 2500;
"fbtrace_id" = "HkQ+T3pxGRU";
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
};
code = 400;
}, com.facebook.sdk:HTTPStatusCode=400}
I got an error when posting message with image on facebook and i m using latest facebook sdk.
I'm using this below code:
-(void)posting
{
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"]) {
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"hello hii how r u " forKey:#"message"];
[params setObject:UIImagePNGRepresentation(self.img_to_share.image) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
NSLog(#"Unable to share the photo please try later.");
}
else
{
//showing an alert for success
NSLog(#"Shared the photo successfully");
}
}];
}
}
Thanks in advance,

Edit:
check after adding access_token in the graph calls since the facebook-ios-sdk doesn't do this automatically.
[FBRequestConnection requestWithGraphPath:[NSString stringWithFormat:#"/me/photos?access_token=%#", FBRequestConnection.accessToken]
andParams:params andHttpMethod:#"POST" andDelegate:self];
For more information check: https://developers.facebook.com/docs/graph-api/reference/user#posts

Related

schedule post on facebook page using facebooksdk

I have written following code to post on facebook page but getting this error message: “(#200) Unpublished posts must be posted to a page as the page itself.“;
NSString *accesstoken = #"EAACEdEose0cBAAe49xNZBS5MqswXkRptXgLDRuZBZBPZCZCVl5rxW6Bt0jthaHXMACNTiUWePg9XkVTpttDNsLyWYBaEkQaUCm5vbSJQh8zGwkegAcQRjRggAJajunmMyg0jVIKZAZBzMjbZCKWUv1Tie1UzF8gJCpIxACIVgqx7SqKdPZBnIUaElk3meB7SYo6AZD";
NSString *pageId = [dic valueForKey:#"id"];
NSDictionary *dicPara=#{#"message": #"Hello",#"access_token":accesstoken,#"scheduled_publish_time": [NSNumber numberWithInt:timestamp],#"published": #"false"};
NSLog(#"%#",dicPara);
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[NSString stringWithFormat:#"/%#/feed",pageId]
parameters:dicPara
HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
// Insert your code here
if (error)
{
NSLog(#"%#",error);
}
else
{
[arrPostId addObject:[result valueForKey:#"id"]];
}
}];
Now I have searched for this error and found that you have to set accesstoken of the page instead of user's token. So how can I change user's access token to page accesstoken?

iOS Facebook Api, make a like to a comment post

I want to like a comment of a post on Facebook, I use the same as like the post. For like a post, it works, but for like a comment fail.
Doc:
https://developers.facebook.com/docs/graph-api/reference/object/likes
My Code:
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"/%#/likes", postId_]
parameters:nil
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{ //Error:
}];
The Error is:
Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x158999b0 {com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 100;
message = "(#100) Error finding the requested story";
type = OAuthException;
};
};
code = 400;
}, com.facebook.sdk:ErrorSessionKey=}
Here is how I do it, and it works like a charm
// post is my module object, encapsulates the info form the post
// pass the post ID
NSString *graphPath = [NSString stringWithFormat:#"%#/likes", post.postID];
FBRequest *request = [FBRequest requestForGraphPath:graphPath];
// DELETE or POST the like
NSString *method = post.liked?#"DELETE":#"POST";
[request setHTTPMethod:method];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
BOOL success = YES;
success = (error)?NO:YES;
if(success) {
}
}];
Note: make sure you have publish permissions
In my project I use the following code:
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:URL, #"object", nil];
if (FBSession.activeSession.isOpen) {
if (FBSession.activeSession.accessTokenData.accessToken) {
[FBRequestConnection startWithGraphPath:#"/me/og.likes"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"Just liked on Facebook!");
}];
} else NSLog(#"Cannot open FBSession");
}
There no code for opening or initializing FBSession - I hope, it's not a problem?
I think, we all can trust Facebook, yes? :)
Go to https://developers.facebook.com/docs/reference/opengraph/action-type/og.likes, select "IOS SDK" tab and look at the code.

Posting a map to Facebook using Facebook SDK from iOS

I am posting an image to facebook through FBRequestConnection.
How can I post a map to FB by giving a latitude and longitude. (Also attaching some text with this)
Please advise.
if (FBSession.activeSession.isOpen) {
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"Posting from padivattom" forKey:#"message"];
[params setObject:UIImagePNGRepresentation(image) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
}
else
{
//showing an alert for success
}
}];
I used the method:
[FBWebDialogs presentFeedDialogModallyWithSession:
parameters:
handler:
to post message and map to facebook. (Its post to news feed).
But you can try to add to your dictionary: [params setObject:[NSString stringWithFormat:#"https://maps.google.com/?q=%#,%#",latitude,longitude] forKey:#"link"];
(If it doesn't work, you can find the Feed Dialog sample on facebook developers page).
I hope it helps

Facebook API: Unknown fields: name?

My request:
<FBRequestConnection: 0x93a8ba0, 1 request(s):(
<FBRequest: 0x9395c90, session: 0xa8aeb80, graphPath: https://graph.facebook.com/me, HTTPMethod: GET, parameters:
{
"access_token" = BAAG4P63ZBHK0BAObimWrMZCGOzn3s6qOtqynyzkJmIN0fKiKGlCkmlqlKMcpbvrrhN9QJsIxrTc7PZCFoceuCwAC2ygCph27WV72iwxkbi2svKCDemDo8BBqOwc6bp6DQH7U9tjp4gZBofb4brYg;
fields = name;
format = json;
"migration_bundle" = "fbsdk:20120913";
sdk = ios;
})>
My response:
result (null)
error Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x93b1ad0 {com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 100;
message = "(#100) Unknown fields: name.";
type = OAuthException;
};
};
code = 400;
}, com.facebook.sdk:HTTPStatusCode=400}
What is the reason behind?
If I send without fields = name; then I get back some default user data.
Invoking:
FBRequest *graphRequest = [[FBRequest alloc] initWithSession:[FBSession activeSession]
graphPath:request.graphPath
parameters:request.parameters
HTTPMethod:request.httpMethod];
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
[connection addRequest:graphRequest
completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
[self requestCompleted:connection result:result error:error];
}];
[connection start];
Request object:
-(NSString*)graphPath
{ return #"https://graph.facebook.com/me"; }
-(NSDictionary*)parameters
{
return [NSDictionary dictionaryWithObjectsAndKeys:
#"name", #"fields",
nil];
}
-(NSString*)HTTPMethod
{
return #"GET";
}
Have tried with fixed parametes as well, no effect:
graphRequest = [[FBRequest alloc] initWithSession:[FBSession activeSession]
graphPath:#"https://graph.facebook.com/me"
parameters:[NSDictionary dictionaryWithObjectsAndKeys:#"name", #"fields", nil]
HTTPMethod:#"GET"];
Graph path must be relative.
Now I use only #"me" instead of #"https://graph.facebook.com/me", and works like a charm.

IOS Facebook SDK 3 upload image with message

I am using the following code to upload an image to Facebook wall :
UIImage *img = [UIImage imageNamed:#"logoits.png"];
[FBRequestConnection startForUploadPhoto:img
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"Facebook posting %#",result);
NSLog(#"Facebook posting %#",error);
}];
It works as expected but I would like to add a message or title to this uploaded photo, and looking at the documentation of FBRequestConnection there is no example of that. http://developers.facebook.com/docs/sdk-reference/iossdk/3.0/class/FBRequestConnection/
How do I upload an image with message?
In the Facebook SDK 3.0 only a image can be post using the given methods, and for other actions you need to use graph a pi's.
So for post a image with message you need to use graph-api. Here is the line of codes which lets you to post a image with message on users timeline.
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"your custom message" forKey:#"message"];
[params setObject:UIImagePNGRepresentation(_image) forKey:#"picture"];
_shareToFbBtn.enabled = NO; //for not allowing multiple hits
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
[self alertWithTitle:#"Facebook" message:#"Unable to share the photo please try later."];
}
else
{
//showing an alert for success
[UIUtils alertWithTitle:#"Facebook" message:#"Shared the photo successfully"];
}
_shareToFbBtn.enabled = YES;
}];
and also make sure _shareToFbBtn is enabled only after login is success.
linesvishy's solution works great and it's also worth to note that if you were using Facebook SDK 3.1 and you used method
FBRequest *request = [FBRequest requestForUploadPhoto:_imageToShare];
You can just use the following lines to replace it:
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:_textToShare forKey:#"message"];
[params setObject:_imageToShare forKey:#"picture"];
FBRequest *request = [FBRequest requestWithGraphPath:#"me/photos" parameters:params HTTPMethod:#"POST"];
Hope it helps! Happy Coding!
Here is an example that posts and image and a note:
http://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/publish-open-graph-story/

Resources