1

我的项目是在我的 iPhone 应用程序上获取我的粉丝专页的 Facebook Insights 数据。

我首先安装 Facebook SDK for iOS 以获取访问代码,而不是使用 Facebook iOS SDK 提供的 SampleLoginSample 示例程序。

它的工作完美并给出与 Facebook API Explporer for Query 相同的响应

[self.textNoteOrLink setText:
    [NSString stringWithFormat:@"https://graph.facebook.com/[PAGEID]?fields=likes&access_token=%@",appDelegate.session.accessTokenData.accessToken]];

但是如果我在下面写查询它不会像 Facebook Explorer API 那样给我响应

询问:

[self.textNoteOrLink setText:
    [NSString stringWithFormat:@"https://graph.facebook.com/PAGEID/insights/page_fans?access_token=%@",appDelegate.session.accessTokenData.accessToken]];

回复:

{"data":[

],"paging":{

"previous":"https:\/\/graph.facebook.com\/PAGEID\/insights\/page_fans?access_token=CAA...snip...h2l&since=1378984583&until=1379243783","next":"https:\/\/graph.facebook.com\/PAGEID\/insights\/page_fans?access_token=CAA...snip...h2l&since=1379502983&until=1379762183"}}

谁能帮助我在 Facebook iOS SDK 中获得具有 get_insights 权限的访问代码?

4

1 回答 1

1

SLAppDelegate.h

在 的列表下@property,添加:

-(BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI;

SLAppDelegate.m

在 下applicationDidBecomeActive,添加:

/*
 * Opens a Facebook session and optionally shows the login UX.
 */
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"read_insights",
                            nil];
    
    return [FBSession openActiveSessionWithReadPermissions:permissions
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState state,
                                                             NSError *error) {
                                             [self sessionStateChanged:session
                                                                 state:state
                                                                 error:error];
                                         }];
}

和:

/*
 * Callback for session changes.
 */
- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                NSLog(@"User session found");
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }
    
    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

SLViewController.h

在末尾viewDidLoad 添加:

[appDelegate openSessionWithAllowLoginUI:NO];
于 2013-09-20T14:31:41.163 回答