微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – OAuthException / code = 2500 / message =“必须使用活动访问令牌来查询有关当前用户的信息.”

我正在尝试获取登录用户的数据(如姓名,性别,生日,个人资料图片等).我尝试过以下两个代码,如下所示:
//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);
     }
 }];

但我在输出中得到空字符串并检测到此错误

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}

我该怎么办?

解决方法

我相信我遇到了同样的问题,而我刚开始工作的方式就是添加
FBSession.activeSession = fbSession;

一旦初始处理程序成功返回. (fbSession是你在facebook会话中的任何变量.)我随便偶然发现了这个(我想从另一个StackOverflow帖子).但我没有在文档中看到它,并且通过grepping,它似乎不在任何Sample应用程序中.那么我的代码可能存在一个更大的问题,我没有看到?我有兴趣听听其他人如何避免设置activeSession,并且仍然可以运行startForMeWithCompletionHandler.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐