简体   繁体   中英

I couldn't get a Facebook access token to use the graph API on iOS

I am a newbie for StackOverflow and iOS. I am trying get user informations like groups, books etc. and I need use the Facebook graph API for that. To use graph API you must use an access token. I read article on the Facebook developer page about this topic and I wrote some code, but it didn't work. Can you help me? Is my code right or shall I use another way?

my .h file:

#import <UIKit/UIKit.h>
#import "FBConnect.h"

@interface FirstViewController : UIViewController <FBSessionDelegate,FBLoginDialogDelegate,FBRequestDelegate>{

Facebook *face;


}

@property(nonatomic,retain)Facebook *face;
-(IBAction)facePushed:(id)sender;
-(IBAction)logoutPushed:(id)sender;

my .m file:

-(IBAction)facePushed:(id)sender{

NSLog(@"FacePushed Began");
face=[[Facebook alloc]initWithAppId:@"313714785358239" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] 
    && [defaults objectForKey:@"FBExpirationDateKey"]) {
    face.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    face.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}    
if (![face isSessionValid]) {
    [face authorize:nil ];
}
else {
    NSLog(@"Session is valid");
    }
    NSLog(@"FacePushed End");
}

}
- (void)fbDidLogin {
NSLog(@"aslan");

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[face accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[face expirationDate] forKey:@"FBExpirationDateKey"];
NSLog(@"%@", [face accessToken]);
[defaults synchronize];

}

- (void)fbDialogLogin:(NSString *)token expirationDate:(NSDate *)expirationDate {
    NSLog(@"%@",token);


}

you have to check the following in your implementation :

  1. you need to get a permissions before you authorize your app

      if (![face isSessionValid]) { NSArray *permissions = [[NSArray alloc] initWithObjects: @"user_likes", @"read_stream", @"publish_stream", nil]; [face authorize:permissions]; [permissions release]; } 
  2. you need to add the following functions in your application delegate(the facebook object here is your instance value of FaceBook Class).

     // Pre 4.2 support - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { return [facebook handleOpenURL:url]; } // For 4.2+ support - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [facebook handleOpenURL:url]; } 
  3. in your info.plist add to URL types > URL Schemes > your facebok app ID with fb prefix (finally your value will be like this fb313714785358239).

Good Luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM