Post a photo on a Facebook Public page - ios

I'm trying to post a photo from an iOS app to a public page.
I log in the app as the Admin of the FB App and when I got the activity token through Graph API:
FBSDKGraphRequest *request2 = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me/accounts"
parameters: #{#"access_token": item.token}
HTTPMethod:#"GET"];
[request2 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error)
{
NSDictionary *userData = (NSDictionary *)result;
NSArray* arraydata = [userData objectForKey:#"data"];
NSDictionary *dataD = [arraydata objectAtIndex:0];
NSString *facebookID = dataD[#"id"];
token= dataD[#"access_token"];
}
I get the page token that I need to post on the page, but even if in the json I see I have all the permissions, when I post the photo it returns a 200 error (additional permission required).
If I try to post on the Admin profile (not the public page), everything works.
Any idea? What I'm missing? Can anyone you please solve this issue?

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?

How to get user's albums id using facebook sdk in iOS

I m using Facebook SDK version 4.16 and working on iOS 9.3
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[NSString stringWithFormat:#"/%#/albums",[result objectForKey:#"id"]]
parameters:#{#"fields":#"albums"}
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (!error) {
NSLog(#"%#",result); // got success message
}
}];
I did set "user_photos" in permission at the time of user Facebook login.
In above code [result objectForKey:#"id"] this line gives Facebook user id.
but this code returns me an empty array for key data hence i am not getting album id and other details.

Facebook Graph API for pages using iOS SDK

I'm trying to retrieve the conversations that the page had with it's users/visitors. I was able to retrieve the conversations for the page using the below call. I do have the right page access token in the 'pgAccessToken' variable.
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:pgAccessToken forKey:#"access_token"];
[params setObject:#"fields" forKey:#"fields"];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/{page-id}/conversations"
parameters:nil tokenString:pgAccessToken
version:nil HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"Output : %#", result);
}];
I was able to receive a list of conversations.
{
"data": [
{
"created_time": "2015-12-12T02:24:21+0000",
"id": "m_mid.1449888061289:b3ec94g1ec0729d776"
},
{
"created_time": "2015-12-12T02:23:08+0000",
"id": "m_mid.1449887988645:94905084ecfdf89812"
}
],
"paging": {
"previous": "https://graph.facebook.com/v2.5/t_mid.1448167619182:200955bd99614aa136/messages?format=json&access_token={MYACCESSTOKEN}&limit=25&since=1449887061&__paging_token={MYPAGINGTOKEN}&__previous=1",
"next": "https://graph.facebook.com/v2.5/t_mid.1448167619182:2009335d99614aa136/messages?format=json&access_token={MYACCESSTOKEN}&limit=25&until=1448167619&__paging_token={MYPAGINGTOKEN}"
}
}
Now I'm trying to receive messages in the conversation. Which I can get by hitting the node '{conversation-id}/messages' but it just gives me a timestamp and message id as part of the response.
What I want is not just the time stamp and message id, but instead the from, to and message contents. I tried the Graph API Explorer but it would still return the same data when I use the following different nodes.
/{conversation-id}/messages
/{message-id}
Can any one help ?
++++UPDATE++++
FIGURED IT OUT. PASTED BELOW IS THE ANSWER IF SOMEBODY ELSE COMES ACROSS THE SAME ISSUE.
Finally figured this out after a few tries with Graph API Explorer tool on facebook developer page.
The way to get more fields is by supplying the fields I wanted in addition to the default that were being sent to me.
NSString *pgAccessToken = [[NSUserDefaults standardUserDefaults] objectForKey:#"PAGE_ACCESS_TOKEN"]; //I have my page accesstoken stored here.
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:#"message_count,updated_time,participants,snippet" forKey:#"fields"];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/{page-id}/conversations"
parameters:params tokenString:pgAccessToken
version:nil HTTPMethod:#"GET"];
__weak MessagesTableViewController *weakSelf = self;
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"Output : %#", result);
//Process your result here, add to local array, reload table etc.
}];

post google static map to facebook wall

I want to post a google static map to facebook wall
this is map url
and open by chrome, is fine
but it wrong on facebook, I have three marker in this map
but just last one stay,as below
(the last marker is test if facebook just keep last one)
here is my code
NSString *staticMap = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/staticmap?size=600x315&maptype=roadmap&markers=color:blue|label:S|%f,%f&markers=color:green|label:E|%f,%f&markers=color:red|label:T|25.2,122.5",dataManager.postCarpoolStartLat,dataManager.postCarpoolStartLng,dataManager.postCarpoolEndLat,dataManager.postCarpoolEndLng];
NSURL *staticMapURL = [[NSURL alloc]initWithString:[staticMap stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(#"static url : %#",staticMapURL);
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"])
{
NSDictionary *params = #{
#"message":#"this is test",
#"caption":#"this is caption",
#"description":#"description",
#"link":#"www.google.com",
#"name":#"Name",
#"picture":staticMapURL,
#"type":#"mobile",
};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"me/feed"
parameters:params
HTTPMethod:#"post"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error){
if (!error)
{
NSLog(#"post success");
}
}];
}
Is anyone know how to let facebook show the right map?
thanks in advance

Retrieve country from Facebook SDK - IOS

I'm using Facebook SDK to get user's details with permissions:
[NSArray arrayWithObjects:#"user_photos",#"public_profile",#"user_location", #"user_birthday",nil];
I know how to get the city but how can I get user's country?
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (!error)
{
NSDictionary *userData = (NSDictionary *)result;
NSString *facebookID = userData[#"id"];
NSString *name = userData[#"name"];
NSString *city = userData[#"location"][#"name"];
}
else
{
//...
}
}];
It should be [#"location"][#"country"] as #trick14 outlined. If you can't get a value from it, it can have multiple reasons:
The user you're querying has no location information in his profile
The Access Token you're using has no granted user_location permission (Check with a call to /me/permissions)
There's no country info for the location (quite unlikely)
You have a problem with your permissions string BTW, user_location is contained twice, and offline_access and publish_stream are deprecated long ago.
try this:
NSString *city = userData[#"location"][#"city"];
See here for more info.. :)

Resources