令人困惑的困境:当我使用一个 URL 获取我的 friend 列表时,我可以很好地恢复该列表,使用相同的 URL 但具有查询字符串 (?fields=id,name,picture) 和完全相同的权限,我得到指示“必须使用事件访问 token 来查询有关当前用户的信息”的错误。给了什么?
当前有效的权限是 publish_stream、email 和 read_stream。为什么添加那个查询字符串会搞砸呢?我唯一能想到的是,我拥有的访问 key 不是我想的那样。有没有办法提取实际的访问 key ,将其公开在 NSLog 中,然后在图形浏览器上对其进行测试?
有效的 URL 是:
https://graph.facebook.com/me/friends
不存在的 URL 是:
https://graph.facebook.com/me/friends?fields=id,name,picture
这是真正获取权限的代码。事实上,这与 Stuart Breckenridge 在 GitHub 上免费提供的代码相同(感谢老兄!)只要我不在 api 调用的末尾附加 '?fields=name,id,picture',它似乎就可以正常工作:
-(void)requestPermissions
{
if (debugF) NSLog(@"FAM: requestPermissions");
// Specify the Facebook App ID.
_facebookAppID = @"123456789123456"; // You Must Specify Your App ID Here.
// Submit the first "read" request.
// Note the format of the facebookOptions dictionary. You are required to pass these three keys: ACFacebookAppIdKey, ACFacebookAudienceKey, and ACFacebookPermissionsKey
// Specify the read permission
_facebookPermissions = @[@"email"];
// Create & populate the dictionary the dictionary
_facebookOptions = @{ ACFacebookAppIdKey : _facebookAppID,
ACFacebookAudienceKey : ACFacebookAudienceFriends,
ACFacebookPermissionsKey : _facebookPermissions};
_facebookAccountType = [_facebookAccountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[_facebookAccountStore requestAccessToAccountsWithType:_facebookAccountType options:_facebookOptions completion:^(BOOL granted, NSError *error)
{
// If read permission are granted, we then ask for write permissions
if (granted) {
_readPermissionsGranted = YES;
// We change the _facebookOptions dictionary to have a publish permission request
_facebookPermissions = @[@"publish_stream", @"read_stream", @"friends_photos"];
_facebookOptions = @{ ACFacebookAppIdKey : _facebookAppID,
ACFacebookAudienceKey : ACFacebookAudienceFriends,
ACFacebookPermissionsKey : _facebookPermissions};
[_facebookAccountStore requestAccessToAccountsWithType:_facebookAccountType options:_facebookOptions completion:^(BOOL granted2, NSError *error)
{
if (granted2)
{
_publishPermissionsGranted = YES;
// Create the facebook account
_facebookAccount = [[ACAccount alloc] initWithAccountType:_facebookAccountType];
_arrayOfAccounts = [_facebookAccountStore accountsWithAccountType:_facebookAccountType];
_facebookAccount = [_arrayOfAccounts lastObject];
}
// If permissions are not granted to publish.
if (!granted2)
{
if (debugF) NSLog(@"Publish permission error: %@", [error localizedDescription]);
_publishPermissionsGranted = NO;
}
}];
}
// If permission are not granted to read.
if (!granted)
{
if (debugF) NSLog(@"Read permission error: %@", [error localizedDescription]);
_readPermissionsGranted = NO;
if ([[error localizedDescription] isEqualToString:@"The operation couldn’t be completed. (com.apple.accounts error 6.)"])
{
[self performSelectorOnMainThread:@selector(showError) withObject:error waitUntilDone:NO];
}
}
}];
}
最佳答案
事实证明,我最初问题的答案比我想象的要简单:
NSString *acessToken = [NSString stringWithFormat:@"%@", self.facebookAccount.credential.oauthToken];
然而,感谢所有为对话做出贡献的人。
关于ios - 如何从代码中获取 Facebook 访问 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16291457/