我正在尝试通过FBConnect最新的SDK提取用户的基本信息。我的代码很简单:
- (void)viewDidLoad {
[super viewDidLoad];
facebook = [[Facebook alloc] initWithAppId:fbAppId];
NSArray* permissions = [[NSArray arrayWithObjects:@"user_about_me",@"read_stream",nil]retain];
[facebook authorize:permissions delegate:self];
[facebook requestWithGraphPath:@"me" andDelegate:self];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
fbAppId, @"app_id",
[NSString stringWithFormat: @"Some Text"], @"description",
@"My Test App", @"name",
@"Test Facebook Graph API",@"message",
nil];
[facebook dialog:@"feed" andParams:params andDelegate:self];
}
- (BOOL) application: (UIApplication*) application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
- (void) fbDidLogin {
NSLog(@"FB didLogin");
}
- (void) fbDidNotLogin:(BOOL)cancelled {
NSLog(@"FB didNotLogin");
}
- (void) request:(FBRequest *)request didLoad:(id)result {
NSLog(@"request-didLoad-result");
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(@"received response");
}
到目前为止,一切似乎都进展顺利。通过对话框在用户墙上发布供稿效果很好。当我尝试通过以下方式获取用户信息(例如姓名)时,会发生问题:
[facebook requestWithGraphPath:@"me" andDelegate:self];
但是fbDidLogin和requestDidLoad都没有被调用。对于requestDidLoad,我检查了didLoadRawResponse,因为它在请求didLoad之前被调用:
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData*)data {
NSString *response = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Response is = %@",response);
[response release];
}
我得到的响应是以下身份验证错误:
{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}}
原因和解决方法是什么?
最佳答案
我已经在下面的Wiki中添加了完整的代码
How to retrieve Facebook response using Facebook iOS SDK
希望这会对您有所帮助,如果您有任何问题,这是一个有效的代码,请告诉我。