Unable to get users data from facebook using graph API - ios

I am making in app in which i show user's hometown,location on profile screen. I get the required data when using developer's account and get data for every user when current user is looking at its own profile.
But when i try to look at other's profile, i don't get the desired data. Here is my code for how i am getting the data out.
-(void)loadFbData{
NSMutableString *graphPath = [NSMutableString stringWithFormat:#"/?id=%#",[self.user valueForKey:#"facebookId"]];
[graphPath appendString:#"&fields=location,hometown,picture.width(640).length(342),name,gender,email,website"];
[FBRequestConnection startWithGraphPath:graphPath completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
dispatch_async(dispatch_get_main_queue(), ^{
self.personalDataView.webLabel.text = [result objectForKey:#"website"];
self.personalDataView.emailLabel.text = [result objectForKey:#"email"];
self.personalDataView.webLabel.text = [result valueForKey:#"website"];
self.profileBasicDataView.livesInLbl.text = [[result objectForKey:#"location"] objectForKey:#"name"];
self.profileBasicDataView.fromLbl.text = [[result objectForKey:#"hometown"] objectForKey:#"name"];
});
NSURL *url = [NSURL URLWithString:[[result objectForKey:#"picture"] valueForKeyPath:#"data.url"]];
[self.profileBasicDataView loadUserFbData:result];
[self.profileImage loadImage:url];
}
}];
}
**website and email is hidden when other user's profile is shown.

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?

Get Facebook Friends returns empty

I am trying to get a list of friends for my app, however this is what the console returns:
{
data = (
);
summary = {
"total_count" = 1221;
};
The total count is right but it doesn't show me any of my friends?
I have created two test users on the facebook developer portal and they have both logged in with my app, and they are friends with me on facebook, shouldn't their data be showing?
Here is my code for requesting the permissions and such, this class is called each time the user logs in.
#implementation UploadUser
NSString *userName;
NSString *userId;
NSString *userGender;
//UIImage *userPicture;
NSString *userPicture;
NSMutableURLRequest *urlRequest;
//NSData *imageData;
//NSData *pictureData;
//NSData *tempData;
//Retrieves users name, gender and id
-(void)getUserInfo {
FBSDKGraphRequest *requestUser = [[FBSDKGraphRequest alloc]initWithGraphPath:#"me" parameters:nil];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestUser completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
userName = result[#"name"];
userGender = result[#"gender"];
userId = result[#"id"];
if (connection) {
if (!error) {
[self setUserData];
[self uploadUser];
}
}else if (!connection) {
NSLog(#"Get User %#",error);
}
}];
[connection start];
//Get Friends
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me/friends"
parameters:nil
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (connection) {
NSLog(#"Friends = %#",result);
}else if (!connection) {
NSLog(#"Get Friends Error");
}
}];
}
//Saves user data to class
-(void)setUserData {
DataManager *dm = [DataManager sharedInstance];
[dm setObject:userName forKey:#"userName"];
[dm setObject:userGender forKey:#"userGender"];
[dm setObject:userId forKey:#"userId"];
NSLog(#"%#",userId);
NSLog(#"%#",userName);
NSLog(#"%#",userGender);
}
//Uploads user data online
-(void)uploadUser {
PFUser *user = [PFUser currentUser];
user[#"name"] = userName;
user[#"id"] = userId;
user[#"gender"] = userGender;
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(#"User Data Uploaded :)");
} else {
//Handle Error
NSLog(#"User Upload Error... %#",error);
}
}];
}
Why are isn't my query returning my friends? Is it because my friends are not authorised to use my app? Is my permissions not being asked properly?
Also you can use Graph API Explorer in order to quickly check if your request works:
https://developers.facebook.com/tools/explorer/
If you want to check you friends you should ask your friend also use this link and get token. In this way the facebook system understand that your friend login in Graph API app and you will get the list of friends.
Since v2.0 of the Graph API, you can only get the friends who authorized your App with user_friends too. This is subject of countless Stackoverflow threads already, for example: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app
Check this one Get Facebook friends

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.. :)

retrieve facebook birthday returns (null)

i'm trying to retrieve the current users birthday through FBRequestConnection. i've successfully retrieved id, name and gender, but birthday just returns null. Why and what can i do in order to get the current users birthday? do i need some kind of permisson?
[FBRequestConnection
startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *facebookId = [result objectForKey:#"id"];
NSString *facebookName = [result objectForKey:#"name"];
NSString *birthday = result[#"birthday"];
NSString *gender = result[#"gender"];
NSLog(#"%#", birthday);
}
}];
loginButton = [[FBSDKLoginButton alloc] init];
loginButton.frame = CGRectMake(self.view.frame.size.width/2-loginButton.frame.size.width, self.view.frame.size.height/2+100, 250, 20);
loginButton.readPermissions=#[#"email",#"user_birthday"];
try to add code above.

How to get large picture size from Facebook graph API for news feed

I am stuck with getting picture from Facebook Graph API for news feed with small size. This is the url I get https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t1.0-0/10363492_10202184985725870_7374705736674502849_s.jpg. Not sure how will get the picture with size o.jpg
Finally, figured it out. Here is what I did.
NSString *query = [NSString stringWithFormat:#"SELECT pid, object_id, src_big, src_big_width, src_big_height FROM photo WHERE object_id = %#", wallPost[#"object_id"]]; //begins from SELECT........
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
query, #"q",
nil];
[FBRequestConnection startWithGraphPath:#"/fql" parameters:params HTTPMethod:#"GET" completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
//do your stuff
if (!error) {
NSArray *photo = [result objectForKey:#"data"];
NSString *photoString = [[photo valueForKey:#"src_big"]componentsJoinedByString:#""];
//NSLog(#"result %# %#", photoString);
}
}];

Resources