zeeple
zeeple

Reputation: 5617

How to get Facebook access key from code

Perplexing quandary: When I get my friends list with one URL I get the list back fine, using the same URL but with a query string (?fields=id,name,picture), and the exact same permissions, I get an error indicating "An active access token must be used to query information about the current user." What gives?

The permissions currently in effect are publish_stream, email, and read_stream. Why would adding that query string mess it up? The only thing I can think of is that the access key I have is not what I think. Is there a way to pull the actual access key out, expose it in an NSLog, and then test it on the graph explorer?

The URL that works is:

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

The URL that doesn't is:

https://graph.facebook.com/me/friends?fields=id,name,picture

This is the code that actually get the permissions. In fact this is the same code that Stuart Breckenridge offered up freely on GitHub (thanks dude!) It seems to work fine as long as I am not appending '?fields=name,id,picture' to the end of the api call:

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

Upvotes: 1

Views: 4027

Answers (1)

zeeple
zeeple

Reputation: 5617

As it turns out, the answer to my original question was simpler than I thought:

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

Kudos to all who contributed to the conversation, however.

Upvotes: 2

Related Questions