0

I am using Graph API to get facebook user information as follows:

[facebook requestWithGraphPath:@"me" andDelegate:self];

How can I do this? I know that server response is in the form of JSON object but don't know how to parse it.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
WaJiyaz
  • 512
  • 1
  • 8
  • 25

2 Answers2

3

I don't find the links that helped me to resolve this issue.

This is the code I used (request:didLoad: is a FBRequestDelegate callback):

- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSDictionary class]]) {

    NSDictionary* hash = result;

    NSString *username = (NSString*)[hash valueForKey:@"name"];

    [[NSUserDefaults standardUserDefaults] setObject:username forKey:@"Username"];
}
[self publishStream]; // publishStream is a custom methods of mine to create the post to publish
};

I remember that I read the FB ios api code and maybe this and this

I hope this works for you.

Bye,

Fran

Sefran2
  • 3,578
  • 13
  • 71
  • 106
  • Hi, I tried it out but in didLoadRawResponse it's giving "OAuthException": "An active access token must be used to query information about the current user". So I guess, it isn't reaching request didLoad. Facebook iOS SDK 4 automatically detects current user login and even after authorizing the facebook application, is there more authorization needed to be gained to access user name? And How? – WaJiyaz Mar 02 '11 at 15:36
  • Is fbDidLogin called? I had a problem fbDidLogin was never called with the last api, so I found the fb october api and I resolved. When fbDidLogin is called, I do this [[NSUserDefaults standardUserDefaults] setObject:_facebook.accessToken forKey:@"AccessToken"]; [[NSUserDefaults standardUserDefaults] setObject:_facebook.expirationDate forKey:@"ExpirationDate"]; [[NSUserDefaults standardUserDefaults] synchronize]; [_facebook requestWithGraphPath:@"me" andDelegate:self]; – Sefran2 Mar 02 '11 at 15:53
  • And in viewDidLoad... _facebook = [[Facebook alloc] init]; _facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"AccessToken"]; _facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"ExpirationDate"]; _permissions = [[NSArray arrayWithObjects: @"publish_stream", @"read_stream", @"offline_access",nil] retain]; – Sefran2 Mar 02 '11 at 15:54
  • No, fbDidLogin is not called and may be because I am using latest API, as you said. But isn't it recommended to always use latest API? So, it means getting october api will resolve the issue? Do you have the link from where to download it? – WaJiyaz Mar 02 '11 at 16:27
  • Yes, it's recommended use the latest api. Consider that I use it to have a popup fb dialog and with the latest api I didn't succeed in obtaining it... a web view was opened and it wasn't that I wanted. I think you should do another question about your specific problem on stackoverflow or on the fb developer forum. However, the old api are here: http://cl.ly/3PPI – Sefran2 Mar 02 '11 at 17:24
  • Yes, you are right. I was considering the same, going over to fb developers forum. It's so surprising that guide for such an important API is so vague. I saw load many people stuck in almost similar problems. And, thankyou for the link :) – WaJiyaz Mar 02 '11 at 17:59
  • I am also struggling with the same issue, I am doing the following facebook = [[Facebook alloc] initWithAppId:kAppId]; facebook.sessionDelegate = self; permissions = [[NSArray arrayWithObjects: @"email", @"read_stream", @"user_birthday", @"user_about_me", @"publish_stream", @"offline_access", nil] retain]; and then I call this [facebook authorize:permissions delegate:self]; at this moment fbDidLogin method get called part 1 of 2 – Yogesh Mar 04 '11 at 06:10
  • but then when I call [facebook requestWithGraphPath:@"me" andDelegate:self]; nothing happens. I don't know what to do after this how do I get a hold of json string, didLoad method never gets called neither in the first authentication call neither in requestWithGraphPath. part 2 of 2 – Yogesh Mar 04 '11 at 06:10
  • @Yogesh: I don't know if this can help... I authorize with [facebook authorize:MyFBAppId permissions:permissions delegate:self]; where MyFBAppId is the FB app key corresponding to my app. – Sefran2 Mar 05 '11 at 11:55
  • @Fran: I have got it working i have pasted a link below for the solution. – Yogesh Mar 05 '11 at 17:34
  • this is different than a username? – aherlambang Mar 29 '12 at 01:32
0

I have answered this question here How to retrieve Facebook response using Facebook iOS SDK

Community
  • 1
  • 1
Yogesh
  • 1,333
  • 3
  • 16
  • 35
  • Thankyou Yogesh for your response. But I have already tried what you said. I am getting authentication error while extracting information. I have asked separate question for this specific problem. You can answer if you can solve it. – WaJiyaz Mar 05 '11 at 21:57
  • @WJK what kind of error are you getting, where did you post the question, if you can publish some code may be I can help you, as I was struggling initially and then after spending a lot of time all the things are working now for me. I hope I can help you. – Yogesh Mar 06 '11 at 01:02
  • That will be so very kind of you. [Here](http://stackoverflow.com/questions/5207025/authentication-error-while-extracting-user-information-with-facebook-graph-api) is the question which includes the code and the error. Thankyou – WaJiyaz Mar 06 '11 at 01:26