I am trying to post a status on my account. I have been trying since couple of hours but can't get it working.
- (void) shareStatusUpdateButtonClicked {
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me/feed" parameters: #{ #"message" : #"hello world"}HTTPMethod:#"POST"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"POST ID: %#",result);
}
}];
}
else {
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithPublishPermissions:#[#"publish_actions"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (!error) {
NSLog(#"Permission Granted");
NSDictionary *params = #{#"message": #"This is a test message"};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me/feed" parameters:params HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,id result,NSError *error) {
if(!error){
NSLog(#"RESULT %#",result);
NSLog(#"POSTED !");
}
}];
}
else{
NSLog(#"ERROR PERMISSION NOT GRANTED");
}
}
];
}
}
The problem is when i run the app, it checks if the user has permitted 'publish_actions' in the if expression, it fails then the else block executes, however it displays you have already authorised this app. Then i click Ok but the status is not posted on my facebook profile. I don't understand where i am going wrong. Any help would be apperiated.
PS: I have not asked the user to grant 'publish_actions' permission anywhere else in the code. Also i tried removing the app from facebook setting but it did't help.
Related
I'm trying to get the profile image for a Facebook user using the Graph API in the Facebook SDK for iOS but the example code from the website does not work for me.
https://developers.facebook.com/docs/graph-api/reference/user/picture/
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/123456789/picture"
parameters:nil
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
// Insert your code here
}];
I get the following error in the console.
FBSDKLog: starting with Graph API v2.4, GET requests for //123456789/picture should contain an explicit "fields" parameter
From my understanding of the error message instead of parameters:nil I need to enter something like parameters:#{#"fields": #"id"} but whatever I try result in the completing handler is always nil. I don't know what to enter for the profile picture.
EDIT:
I'm looking to get the profile picture for other users.
Try This One:-
- (IBAction)btnFaceBook:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login setLoginBehavior:FBSDKLoginBehaviorBrowser];
[login logInWithReadPermissions:#[#"public_profile",#"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error)
{
[[FBSDKLoginManager new]logOut];
} else if (result.isCancelled)
{
// Handle cancellations
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
[self getDataFromFB];
}
}
}];
}
-(void)getDataFromFB
{
MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:true];
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:[NSDictionary dictionaryWithObject:#"political,picture.width(500).height(500),id,email,first_name,last_name,gender,name" forKey:#"fields"]]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
{
if (!error && [result count] != 0)
{
[hud hideAnimated:true];
NSLog(#"%#", result);
}
else
{
[hud hideAnimated:true];
[self displayAlertMesage:#"Some error has been occure." title:#"Oops!"];
}
}];
}
}
You can use the following code for getting the data from the facebook login:-
-(void)getFacebookProfileInfos
{
FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:#"me" parameters:#{#"fields": #"id,email,name,birthday,first_name,last_name,gender,picture.type(large)"}];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if(result)
{
dictUserData = [[NSDictionary alloc]init];
dictUserData=result;
}
}];
[connection start];
}
you will get all the data in the "dictUserData" dictionary and from that you will access the facebook profile in this way:-
UIImage * imgProfile = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#", dictUserData[#"picture"][#"data"][#"url"]]]]];
NSString * strImageData = [self imageToNSString:imgProfile];
I ma trying to get Group cover image source through Graph API but unfortunately i am getting nil value with public_profile permission and invalid scope error with user_groups permission.
I have checked following Facebook documentation :
FB Group reading
FB Cover photo
FB Permission
First I tried with [login logInWithReadPermissions:#[#"public_profile"] permission later I found that I need to used user_groups. when I have used [login logInWithReadPermissions:#[#"user_groups"] getting following scope error
I have tried following code
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:#[#"user_groups"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(#"Process error");
} else if (result.isCancelled) {
NSLog(#"Cancelled");
} else {
FBSDKAccessToken *accessToken = [FBSDKAccessToken currentAccessToken];
NSDictionary *params = #{#"access_token" : accessToken};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/190520617800186?fields=source"
parameters:params
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
NSLog(#"Success");
}];
}
}];
I have already submitted a request for Facebook app review
Here is my code , I am only getting user name and facebook id of user.
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: #[#"public_profile",#"email",#"user_friends"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(#"Process error");
} else if (result.isCancelled) {
NSLog(#"Cancelled");
} else {
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"fetched user:%#", result);
}
}];
} NSLog(#"Results=%#",result);
NSLog(#"Logged in");
}
}];
and it also give some error like this -
-canOpenURL: failed for URL: "fbauth2:/" - error: "(null)" -canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"
FBSDKLog: starting with Graph API v2.4, GET requests for /me should
contain an explicit "fields" parameter
I am using pod for Facebook sdk and it's work in iOS9.0 with xcode 7.1. Try this code
void (^sendRequestsBlock)(void) = ^{
FBSDKGraphRequest *postRequest = nil;
NSDictionary *parameters = [NSDictionary dictionaryWithObject:#"id,email,first_name,last_name,name,picture{url}" forKey:#"fields"];
postRequest = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:parameters HTTPMethod:#"GET"];
[postRequest startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
{
if (!error) {
NSDictionary *dictResult = (NSDictionary *)result;
}
}];
};
/// login start
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logOut];
NSArray *permissions = [NSArray arrayWithObjects:#"email",#"public_profile", nil];
[login logInWithReadPermissions:permissions fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
} else if (result.isCancelled) {
} else {
if ([result.grantedPermissions containsObject:#"email"]) {
sendRequestsBlock();
}
}
}]
I am trying getting info from Facebook API. The parameters are group in couple catalogs. To get data I using these code below from Facebook SDK:
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"me"
parameters:#{ #"fields" : #"name, birthday"}
HTTPMethod:#"GET"];[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
];
Name of catalog where are data 'name' and 'birthday' is "fields". But I want to get more data from other catalogs (edges, parameters), like first name, last name, email, id, about, etc. How can I write code to get it all?
Put this code
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:#[#"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Process error
NSLog(#"error is :%#",error);
}
else if (result.isCancelled)
{
// Handle cancellations
NSLog(#"error is :%#",error);
}
else
{
if ([result.grantedPermissions containsObject:#"email"])
{
[self fetchUserInfo];
}
}
}];
you can get facebook user information as bellow
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:#{#"fields": #"id,name,link,first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSString *photostring=[[[result valueForKey:#"picture"] objectForKey:#"data"] valueForKey:#"url"];
photostring = [photostring stringByReplacingOccurrencesOfString:#"&" withString:#"%26"];
NSLog(#"all data here is:%#",result);
NSLog(#"username is :%#",[result valueForKey:#"name"]);
NSLog(#"PhotoUrl is :%#",photostring);
NSLog(#"mail id is :%#",[result valueForKey:#"email"]);
}
}];
}
}
Good luck with your project
Firstly, you must put this code where login process start like bellow
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:#[#"public_profile", #"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error)
{
// There is an error here.
}
else
{
if(result.token) // This means if There is current access token.
{
// Token created successfully and you are ready to get profile info
[self getFacebookProfileInfo];
}
}
}];
if login successful , implement this method to get user's public profile
-(void)getFacebookProfileInfos {
FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:#"me" parameters:nil];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if(result)
{
if ([result objectForKey:#"email"]) {
NSLog(#"Email: %#",[result objectForKey:#"email"]);
}
if ([result objectForKey:#"first_name"]) {
NSLog(#"First Name : %#",[result objectForKey:#"first_name"]);
}
if ([result objectForKey:#"dob"]) {
NSLog(#"Date of birth : %#",[result objectForKey:#"dob"]);
}
if ([result objectForKey:#"id"]) {
NSLog(#"User id : %#",[result objectForKey:#"id"]);
}
}
}];
[connection start];
}
you can also follow this link get facbook user information
or you can get facebook user information as bellow
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
NSString *firstName = user.first_name;
NSString *lastName = user.last_name;
NSString *bateOfBirth = user.bate_Of_Birth;
NSString *facebookId = user.id;
NSString *email = [user objectForKey:#"email"];
NSString *imageUrl = [[NSString alloc] initWithFormat: #"http://graph.facebook.com/%#/picture?type=large", facebookId];
}
}];
}
I'm using Facebook SDK v4 to get all the posts of a friend. I use the following code
FBSDKLoginManager* loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithReadPermissions:#[#"user_posts"] handler:^(FBSDKLoginManagerLoginResult *result, NSError* error) {
if (error) {
// Process error
NSLog(#"");
} else if (result.isCancelled) {
// Handle cancellations
NSLog(#"");
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if ([result.grantedPermissions containsObject:#"user_posts"]) {
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/10002434324342/posts"
parameters:nil
HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
// Handle the result
if (error) {
NSLog(#"");
}
}];
}
}
}];
10002434324342 is an example of facebookID. It just return an empty data list.
Can anyone tell me how to do this
Friend permissions have been removed with v2.0, you can only get data of users who authorized your App too, with the correct permissions. user_posts should always be used with /me/posts.
More information: https://developers.facebook.com/docs/apps/changelog
You can try
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/{user-id-a}/friends/{user-id-b}"
parameters:params
HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
// Handle the result
}];
https://developers.facebook.com/docs/graph-api/reference/v2.3/user/friends