我正在尝试使用iOS 6的新Facebook集成API查询有关用户的信息。这是我正在使用的代码,它与他们在WWDC上演示的内容完全相同:
{
NSDictionary *parameters = @{};
NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:url
parameters:parameters];
request.account = self.account;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Facebook request received, status code %d", urlResponse.statusCode);
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response data: %@", response);
dispatch_async(dispatch_get_main_queue(), ^{
});
}];
}
问题是我从Facebook收到错误代码2500:“必须使用活动访问令牌来查询有关当前用户的信息。”如果我将查询更改为https://graph.facebook.com/[facebook id],那么它可以正常工作。我假设问题是iOS在通过requestForServiceType发送请求时传递应用程序的访问令牌而不是用户的访问令牌。我只是不知道如何解决它。显然,预测和硬编码我的用户的Facebook ID不是一种选择。有什么建议?
答案 0 :(得分:5)
在参数中添加您的活动访问令牌,例如
NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"PUT_ACCESS_TOKEN_HERE" forKey:@"access_token"];
答案 1 :(得分:1)
好的就是我如何与iOS 6进行这种集成并从Facebook获得我想要的东西:
在AppDelegate中我这样做:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [FBSession.activeSession handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBSession.activeSession handleDidBecomeActive];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[FBSession.activeSession close];
}
在我的ViewController中,我想要检索有关我自己或朋友的信息,我这样做(注意:这是一个测试,所以它有很多权限!):
NSArray *permissions =
[NSArray arrayWithObjects:@"email", @"user_location",
@"user_birthday",
@"user_likes", @"user_interests",@"friends_interests",@"friends_birthday",@"friends_location",@"friends_hometown",@"friends_photos",@"friends_status",
@"friends_about_me", @"friends_birthday", @"friends_hometown", @"friends_interests", @"friends_likes", @"friends_location", nil];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
/* handle success + failure in block */
if (status) {
NSLog(@"Facebook Read Permission is successful!");
[self presentPostOptions];
// [self presentPostOptions];
}
}];
然后在“presentPostOptions”中我这样做(在这个例子中我试图从我的朋友那里检索一些东西):
- (void)presentPostOptions
{
[[FBRequest requestForMyFriends] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error)
{
if (!error) {
NSArray *data = [user objectForKey:@"data"];
NSLog(@"%d", [data count]);
for (FBGraphObject<FBGraphUser> *friend in data) {
NSLog(@"%@", [friend first_name]);
NSLog(@"%@", [friend last_name]);
NSLog(@"%@", [friend id]);
//make sure you have FBProfilePictureView outlet in your view
//otherwise skip the profile picture!
self.fbProfilePic.profileID = @"you'r friend's profile.id";
}
}
else
{
NSLog(@"error");
// [self didFailWithError:error];
}
}];
我不知道你还想做什么,因为在你的问题中你只是试图建立连接,但这样你就可以在iOS 6中集成任何你想要的东西。
还有一件事,请确保您在Facebook上的应用设置以及那里的配置,例如启用iOS应用和iPhone / iPad ID。还有你的plist中的FacebookAppID。
让我知道它是否适合您,
编辑:顺便说一句,我的Facebook SDK是3.1.1。答案 2 :(得分:0)
我有相同的错误消息,我在续订凭证后通过保存我的帐户来修复它。
答案 3 :(得分:0)
确保您的(ACAccount *)facebookAccount(或在您的情况下为self.account) 对象很强大,你在获得权限时正确设置它。
答案 4 :(得分:0)
答案很简单
在viewDidLoad()中使用:
accountStore= [[ACAccountStore alloc]init];
facebookAccountType= [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options= @{
ACFacebookAudienceKey: ACFacebookAudienceEveryone,
ACFacebookAppIdKey: @"<YOUR FACEBOOK APP ID>",
ACFacebookPermissionsKey: @[@"public_profile"]
};
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(@"Permission has been granted to the app");
NSArray *accounts= [accountStore accountsWithAccountType:facebookAccountType];
facebookAccount= [accounts firstObject];
[self performSelectorOnMainThread:@selector(facebookProfile) withObject:nil waitUntilDone:NO];
} else {
NSLog(@"Permission denied to the app");
}
}];
功能 - (无效)facebookProfile
NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"];
请注意,您需要的参数被添加为字典 请参阅下面的完整列表 https://developers.facebook.com/docs/graph-api/reference/user
NSDictionary *param=[NSDictionary dictionaryWithObjectsAndKeys:@"picture,id,name",@"fields", nil];
SLRequest *profileInfoRequest= [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:param];
profileInfoRequest.account= facebookAccount;
[profileInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Facebook status code is : %ld", (long)[urlResponse statusCode]);
if ([urlResponse statusCode]==200) {
NSDictionary *dictionaryData= [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
} else {
}
}];
}