我正在尝试使用 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在相同的情况下工作得很好<app-token>
(它不会像这样要求秘密- initWithOAuthToken:tokenSecret:
)。
即使这种方法有效,发布也不安全。
我现在被困住了。请帮忙?