Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OAuthException/ code = 2500/ message = "An active access token must be used to query information about the current user."

Tags:

ios

iphone

I am trying to get data (like name,gender,birthday, profile picture etc) of the user that is logged in. I have tried following two codes, shown below:

 //first: Create request for user's Facebook data
    FBRequest *request = [FBRequest requestForMe];

// Send request to Facebook
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    NSLog(@"%@", error);
    if (!error) {
        // result is a dictionary with the user's Facebook data
        NSDictionary *userData = (NSDictionary *)result;
        NSLog(@"%@....",userData);
       _facebookID = userData[@"id"];
        _name = userData[@"name"];
        _location = userData[@"location"][@"name"];
        _gender = userData[@"gender"];
     _birthday = userData[@"birthday"];
        NSLog(@"%@",_name);


        _picutreURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", _facebookID]];

        self.nameField.text=_name;
        self.profilePicture.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:_picutreURL]];
        self.profileInfo.text=[NSString stringWithFormat:@"%@, Live in %@ , Born on %@",_gender,_location,_birthday];



    }
}];
// 2nd:Fetch user data
[FBRequestConnection
 startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                   id<FBGraphUser> user,
                                   NSError *error) {
     if (!error) {
         NSString *userInfo = @"";

         // Example: typed access (name)
         // - no special permissions required
         userInfo = [userInfo
                     stringByAppendingString:
                     [NSString stringWithFormat:@"Name: %@\n\n",
                      user.name]];

         // Example: typed access, (birthday)
         // - requires user_birthday permission
         userInfo = [userInfo
                     stringByAppendingString:
                     [NSString stringWithFormat:@"Birthday: %@\n\n",
                      user.birthday]];

         // Example: partially typed access, to location field,
         // name key (location)
         // - requires user_location permission
         userInfo = [userInfo
                     stringByAppendingString:
                     [NSString stringWithFormat:@"Location: %@\n\n",
                      user.location[@"name"]]];

         // Example: access via key (locale)
         // - no special permissions required
         userInfo = [userInfo
                     stringByAppendingString:
                     [NSString stringWithFormat:@"Locale: %@\n\n",
                      user[@"locale"]]];

         // Example: access via key for array (languages)
         // - requires user_likes permission
         if (user[@"languages"]) {
             NSArray *languages = user[@"languages"];
             NSMutableArray *languageNames = [[NSMutableArray alloc] init];
             for (int i = 0; i < [languages count]; i++) {
                 languageNames[i] = languages[i][@"name"];
             }
             userInfo = [userInfo
                         stringByAppendingString:
                         [NSString stringWithFormat:@"Languages: %@\n\n",
                          languageNames]];
         }

         // Display the user info
         NSLog(@"%@",userInfo);
     }
 }];

But I get null string in output and detected this error:

Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x9499320 {com.facebook.sdk:ParsedJSONResponseKey={
    body =     {
        error =         {
            code = 2500;
            message = "An active access token must be used to query information about the current user.";
            type = OAuthException;
        };
    };
    code = 400;
}, com.facebook.sdk:HTTPStatusCode=400}

What should I do?

like image 268
Ali Hassan Avatar asked Oct 20 '22 22:10

Ali Hassan


1 Answers

I believe I had the same problem, and the way I just got it working was to add

FBSession.activeSession = fbSession;

once the initial handler came back successfully. (fbSession is whatever variable you have with the facebook session in it.) I just randomly stumbled upon this (I think from another StackOverflow post). But I don't see it in the docs, and by grepping, it doesn't seem to be in any of the Sample apps. So maybe there's a larger issue with my code that I'm not seeing? I'd be interested to hear how others avoid setting the activeSession, and still are able to run startForMeWithCompletionHandler.

like image 164
vegashacker Avatar answered Oct 23 '22 23:10

vegashacker