我創建了一個名爲「登錄Facebook」的單身人士班,旨在將用戶連接到Facebook並執行不同的請求。問題是我得到關於access_token的錯誤。那就是:Facebook Connect類與單身人士:訪問令牌問題
3 : <CFString 0x4c552f0 [0xe50400]>{contents = "message"} = <CFString 0x4c55250 [0xe50400]>{contents = "An active access token must be used to query information about the current user."}
首先,我連接到Facebook,通過從其他視圖控制器做出如下要求:
[[LoginFacebook loginFacebook] launchFacebook:self]
然後我就可以對同其他視圖控制器,但是從另一種方法第二個請求:
[[LoginFacebook loginFacebook] requireName:self]
這裏是我的單例類 「LoginFacebook」:
LoginFacebook.h :
#import <UIKit/UIKit.h>
#import "LoginFacebook.h"
@interface FirstViewController : UIViewController {
}
-(IBAction)performConnect:(id)sender;
-(IBAction)performName:(id)sender;
@end
LoginFacebook.m:
#import "LoginFacebook.h"
static LoginFacebook *loginFacebook = nil;
@implementation LoginFacebook
@synthesize name;
@synthesize facebook;
-(void)launchFacebook:(id)sender
{
permissions = [[NSArray arrayWithObjects:
@"read_stream", @"publish_stream", @"offline_access",nil] retain];
Facebook* facebookbis = [[Facebook alloc] initWithAppId:@"168938499825684"];
facebook = facebookbis;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil delegate:self];
}
}
-(NSString *)requireName:(id)sender
{
NSLog(@"requireName asked");
[facebook requestWithGraphPath:@"me" andDelegate:self];
return name;
NSLog(@"%@",[facebook accessToken]);
}
+ (LoginFacebook *)loginFacebook
{
if (loginFacebook == nil) {
loginFacebook = [[super allocWithZone:NULL] init];
}
return loginFacebook;
}
+ (id)allocWithZone:(NSZone *)zone {
return [[self loginFacebook] retain];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (NSUInteger)retainCount {
return NSUIntegerMax; //denotes an object that cannot be released
}
- (void)release {
//do nothing
}
- (id)autorelease {
return self;
}
// FBRequestDelegate
/**
* Called when the Facebook API request has returned a response. This callback
* gives you access to the raw response. It's called before
* (void)request:(FBRequest *)request didLoad:(id)result,
* which is passed the parsed response object.
*/
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(@"received response");
}
/**
* Called when a request returns and its response has been parsed into
* an object. The resulting object may be a dictionary, an array, a string,
* or a number, depending on the format of the API response. If you need access
* to the raw response, use:
*
* (void)request:(FBRequest *)request
* didReceiveResponse:(NSURLResponse *)response
*/
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSArray class]]) {
result = [result objectAtIndex:0];
}
name = [result objectForKey:@"name"];
NSLog(@"request didLoad");
};
/**
* Called when an error prevents the Facebook API request from completing
* successfully.
*/
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
name = [error localizedDescription];
NSLog(@"----request didFailWithError");
NSLog(@"%@", [error localizedDescription]);
NSLog(@"%@", [error description]);
};
////////////////////////////////////////////////////////////////////////////////
// FBDialogDelegate
/**
* Called when a UIServer Dialog successfully return.
*/
- (void)dialogDidComplete:(FBDialog *)dialog {
name = @"publish successfully";
}
@end
另外請注意,我添加了以下方法(與在.H相應FacebookLogin * Facebook的),以我的應用程序代表:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
是否之一你知道那裏發生了什麼問題嗎?我一直在努力的代碼2天...
我終於設法得到它。 問題是,FBRequestDelegate將在ViewController類中實現,該類將發送請求到單例。 您可以使用巴里真正全面的教程http://barrycenter.com/BAM!coding/2011/07/simplifying-facebook-ios-sdk/ 感謝您的時間Wolfgang Schreurs! – EricDumont