8

我正在尝试使用 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 进行预测和硬编码不是一种选择。有什么建议么?

4

6 回答 6

5

在参数中添加您的活动访问令牌,例如

NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"PUT_ACCESS_TOKEN_HERE" forKey:@"access_token"];
于 2012-07-10T07:24:13.950 回答
2

我遇到了同样的问题并找到了解决方法:

NSString *uid = [NSString stringWithFormat:@"%@", [[self.account valueForKey:@"properties"] valueForKey:@"uid"]] ;                
NSURL *url = [NSURL URLWithString:[@"https://graph.facebook.com" stringByAppendingPathComponent:uid]];
于 2012-09-20T13:10:00.477 回答
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。

于 2013-01-04T05:56:41.597 回答
0

我有同样的错误消息,我在更新凭证后通过保存我的帐户来修复它。

于 2012-09-14T11:47:48.510 回答
0

确保您的(ACAccount *)facebookAccount(或在您的情况下为 self.account) 对象是强大的,并且在获得权限时正确设置它。

于 2012-10-17T09:54:05.267 回答
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");
    }
}];

以及函数-(void)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 {

    }
}];


}
于 2015-07-11T03:45:50.037 回答