【问题标题】:How do I get the feed of a public Facebook page in an iOS app without prompting the user for Facebook permissions?如何在不提示用户提供 Facebook 权限的情况下在 iOS 应用程序中获取公共 Facebook 页面的提要?
【发布时间】:2013-04-18 03:39:03
【问题描述】:

我正在尝试使用 Graph API 获取公共 Facebook 页面的提要,但如果不提示用户授予权限,我不知道该怎么做。

以下代码有效,但会提示用户授予权限:

- (void)doFacebookAuthCompletion:(void (^)(ACAccount* account))completion
{
    ACAccountStore* accountStore = [[ACAccountStore alloc] init];
    ACAccountType* accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    NSDictionary* fbOptions = @{
                                ACFacebookAppIdKey: @"<app-id-key>",
                                ACFacebookPermissionsKey: @[ @"email" ]
                                };

    [accountStore requestAccessToAccountsWithType:accountType
                                          options:fbOptions
                                       completion:^(BOOL granted, NSError *error) {
                                           if (granted) {
                                               completion([[accountStore accountsWithAccountType:accountType] lastObject]);
                                           } else {
                                               NSLog(@"%@", [error localizedDescription]);
                                           }
                                       }];
}

- (void)updateFeed
{
    [self doFacebookAuthCompletion:^(ACAccount* account) {
        // What we want to get
        NSURL* url = [NSURL URLWithString:@"https://graph.facebook.com/microsoft/feed"];
        NSDictionary* parameters = @{ };

        // The request
        SLRequest* request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                               requestMethod:SLRequestMethodGET
                                                         URL:url
                                                  parameters:parameters];
        [request setAccount:account];

        // Send it
        [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
            if (error) {
                NSLog(@"%@", [error localizedDescription]);
            } else {
                NSString *dataString = [[NSString alloc] initWithData:responseData encoding:NSStringEncodingConversionAllowLossy];
                NSLog(@"Response Data: %@", dataString);
            }
        }];
    }];
}

我什至尝试过使用https://developers.facebook.com/docs/reference/api/page/ 引用的应用程序令牌,该令牌只能在服务器端安全使用,但仍然没有成功。这段代码:

- (void)doFacebookAuthCompletion:(void (^)(ACAccount* account))completion
{
    ACAccountStore* accountStore = [[ACAccountStore alloc] init];
    ACAccountType* accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    ACAccount* account = [[ACAccount alloc] initWithAccountType:accountType];
    account.credential = [[ACAccountCredential alloc] initWithOAuthToken:@"<app-token>" tokenSecret:@"<app-secret>"];
    completion(account);
}

给出以下输出:

Response Data: {"error":{"message":"An access token is required to request this resource.","type":"OAuthException","code":104}}

虽然Graph API Explorer 在给定相同的&lt;app-token&gt; 时工作得很好(它不像- initWithOAuthToken:tokenSecret: 那样要求秘密)。

即使这种方法奏效,发布也不安全。

我现在卡住了。请帮忙?

【问题讨论】:

    标签: ios facebook access-token facebook-access-token


    【解决方案1】:

    我无法给出完整的答案,因为我从未使用过 iOS,但在 Facebook 方面,如果没有 某种 类型的令牌,Graph API 不会让您获得任何东西。安全与否,唯一的方法是使用您的应用程序 ID 和密码来生成可以使用的令牌。您可以使用例如:

    https://graph.facebook.com/oauth/access_token?
    client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&
    grant_type=client_credentials 
    

    【讨论】:

    • 我试过了,它可以在 API Explorer 中使用,但在设备上却无法使用。文档明确指出,应用令牌不应在本机应用中使用,但即使将应用类型设置为 web,它也无法正常工作......
    • 只是在您的网络浏览器中进行测试 - 如果您尝试使用上述生成的令牌浏览到 graph.facebook.com/microsoft/feed/?access_token=YOUR_TOKEN,您是否仍然收到错误 104?
    • 不,它在浏览器中运行良好。问题似乎仅限于 iOS。也许我设置令牌的方式有些问题。
    • @VladTataranu 您可以使用应用访问令牌(主要是AppID|AppSecret)来访问公共数据
    • 嗨,Anvesh!第二个 sn-p 代码尝试这样做,但由于某种原因它不起作用。
    猜你喜欢
    • 1970-01-01
    • 2014-04-19
    • 2012-07-09
    • 2012-03-11
    • 2015-10-18
    • 2012-06-19
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多