如何从代码中获取Facebook访问密钥

时间:2013-04-30 02:54:47

标签: ios facebook permissions

困惑的困惑:当我通过一个URL获取我的朋友列表时,我得到的列表很好,使用相同的URL,但使用查询字符串(?fields = id,name,picture),以及完全相同的权限,我得到指示“必须使用活动访问令牌来查询有关当前用户的信息”的错误。是什么给了什么?

当前有效的权限是publish_stream,email和read_stream。为什么要添加查询字符串搞砸了?我唯一能想到的是我拥有的访问密钥并不是我的想法。有没有办法拉出实际的访问密钥,在NSLog中公开它,然后在图浏览器上测试它?

有效的网址是:

https://graph.facebook.com/me/friends

不存在的网址是:

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];
             }
         }
     }];
}

1 个答案:

答案 0 :(得分:2)

事实证明,我原来问题的答案比我想象的要简单:

NSString *acessToken = [NSString stringWithFormat:@"%@", self.facebookAccount.credential.oauthToken];
然而,

对所有为这次谈话作出贡献的人表示赞赏。