我正在尝试使用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/引用的app令牌,这种令牌只能安全地使用服务器端,但仍然没有到达任何地方。这段代码:
- (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:
那样要求秘密)。
即使这种方法有效,也不安全。
我现在被困住了。请帮帮忙?
答案 0 :(得分:2)
我无法给出完整的答案,因为我从未使用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