2

I downloaded the Facebook iPhone demo app recently. I have made several attempts to grasp the authentication process and I'm totally confused. There seems to be a lot of sample code out there that has been rendered obsolete in this version of the API.

In the .m file for my primary view controller, I have the following at the start:

#import "FBConnect.h"

The button that requires information from Facebook uses this line, unchanged from the demo app:

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

This calls the following method:

- (void)request:(FBRequest *)request didLoad:(id)result {

I have hacked my functionality into this method. I pull a few things I need from Facebook and send them to my backend in a HTTP POST command. For example, [result objectForKey:@"id"] returns the id and [result objectForKey:@"name"] returns the name. However, [result objectForKey:@"access_token"] returns nothing. This is very frustrating because I need to pass the access token to my Google App Engine user management back end to create a new user. So, hopefully the code I have included makes my authetication strategy clear (whatever it is). Does anyone know how I can get the access token in this context? I would appreciate it if you could help me out with what header files I need to have included etc. ?

Thanks,

John

Dessie
  • 131
  • 2
  • 9

2 Answers2

3

I'm doing the same exact thing you are. I'm making my own HTTP requests, so I need the accessToken, and what's more, the iOS Facebook SDK doesn't remember the accessToken past launches, even with a no-expire request!

After calling [_facebook authorize:permissions delegate:self]; In the fbDidLogin delegate call, you can easily pull (assuming you have the facebook object retained in _facebook):

- (void)fbDidLogin {
NSString *accessToken = _facebook.accessToken;
NSDate *expirationDate = _facebook.expirationDate;
}

Voilà.

To handle persistence through app launches, you can save this data and manually set those accessors prior to any requests.

Daniel Amitay
  • 6,677
  • 7
  • 36
  • 43
  • Hi Daniel,Thanks for your help. That worked perfectly. I have an embarassing follow-on question. I can NSLog accessToken within FBDidLogin and it works. However, within my later call to the method `- (void)request:(FBRequest *)request didLoad:(id)result {`, the variable accessToken is unrecognized. How do I move the variable between the two methods? – Dessie Apr 18 '11 at 12:09
  • Never mind, I figured it out. Thanks for your help Daniel. That was exactly what I needed. – Dessie Apr 18 '11 at 14:03
1

after success full login you can retrieve using,

[[FBSession activeSession] accessToken];
Banker Mittal
  • 1,918
  • 14
  • 26