在iOS中抓取Facebook的朋友时出错 [英] Error while fetching Facebook friends in iOS
问题描述
我正在尝试取得已登录用户的Facebook朋友列表,并且在我的帐户中可以正常工作,但是当我将应用详细信息迁移到新帐户并更改了应用程序ID和应用程序密码时,我收到以下错误信息
错误域= com.facebook.sdk代码= 5操作无法完成(com.facebook.sdk错误5.)UserInfo = 0xf89c0b0 {com.facebook.sdk:HTTPStatusCode = 400,com.facebook.sdk:ParsedJSONResponseKey = {
body = {
error = {
code = 100;
message =(#100)未知字段:用户名。
type = OAuthException;
};
};
code = 400;
},com.facebook.sdk:ErrorSessionKey =&FBSession:0xe5a6c30,状态:FBSessionStateOpen,loginHandler:0xe58d5a0,appID:705259966179163,urlSchemeSuffix:,tokenCachingStrategy:< FBSessionTokenCachingStrategy:0xf87b180>,expirationDate:4001-01- 01 00:00:00 +0000,refreshDate:2014-05-20 07:14:50 +0000,attemptRefreshDate:0001-12-30 00:00:00 +0000,permissions :(
status,
权限
)>}
这是我目前的代码, (NSArray *,NSError *))完成Block $($)$($)$($)$($) b $ b {
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:^(FBSession * session,
FBSessionState state,
NSError * error){
if(error){
NSLog(@Error);
} else if(session.isOpen){
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection * connection,NSDictionary< FBGraphUser> * user,NSError * error){
if(error){
//在这里抛出代码100错误
completionBlock(nil,error);
} else {
FBRequest * friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler:^(FBRequestConnection * connection,NSDictionary * result,NSError * error){
if(error)
completionBlock(nil,error);
else
{
NSArray * friendJSONs = [result objectForKey:@data];
NSMutableArray * friends = [NSMutableArray array];
for(NSDictionary * friendDict in friendJSONs)
{
SocialProfile * friend = [[SocialProfile alloc] initWithDictionary:friendDict socialMedia:kSocialMediaFacebook];
[朋友addObject:friend];
}
completionBlock(friends,nil);
}
}];
}
}];
}
}];
返回YES;
}
如果我使用我的旧应用程序ID和密码,此代码工作正常。我检查了新帐户的设置,一切都和我在旧帐户中所做的一样,我不明白问题是什么。
根据 https://developers.facebook.com/docs/apps/changelog
朋友列表现在只返回也使用您的应用程序的朋友:通过/ me / friends endpoint现在限于已经授权您的应用程序的朋友列表。
所以我把上面的代码改成了这个,它给了我的朋友列表使用应用程序而不是整个FB朋友列表
FBRequest * friendsRequest = [FBRequest requestForGraphPath:@/ me / friends];
I'm trying to fetch facebook friendlist of a logged in user and it works fine in my account, but when I migrated the app details to a new account and changed the app ID and app secret I'm getting this error below
Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0xf89c0b0 {com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 100;
message = "(#100) Unknown fields: username.";
type = OAuthException;
};
};
code = 400;
}, com.facebook.sdk:ErrorSessionKey=<FBSession: 0xe5a6c30, state: FBSessionStateOpen, loginHandler: 0xe58d5a0, appID: 705259966179163, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0xf87b180>, expirationDate: 4001-01-01 00:00:00 +0000, refreshDate: 2014-05-20 07:14:50 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(
status,
permission
)>}
This is my current code for fetching friends
- (BOOL)retrieveFacebookFriendsForProfileId:(NSString *)fbProfileId completionBlock:(void (^)(NSArray *, NSError *))completionBlock
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
if (error) {
NSLog(@"Error");
} else if (session.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (error) {
//Throws the code 100 error here
completionBlock(nil, error);
}else{
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary* result,NSError *error) {
if(error)
completionBlock(nil, error);
else
{
NSArray *friendJSONs = [result objectForKey:@"data"];
NSMutableArray *friends=[NSMutableArray array];
for(NSDictionary * friendDict in friendJSONs)
{
SocialProfile *friend=[[SocialProfile alloc] initWithDictionary:friendDict socialMedia:kSocialMediaFacebook];
[friends addObject:friend];
}
completionBlock(friends, nil);
}
}];
}
}];
}
}];
return YES;
}
This code works fine if I use my old app Id and secret. I checked the setting in new account and everything is same as I have done in my old account I dont understand what the problem is. Any help is appreciated
According to https://developers.facebook.com/docs/apps/changelog Friend list now only returns friends who also use your app: The list of friends returned via the /me/friends endpoint is now limited to the list of friends that have authorized your app.
So I changed my above code to this and it gave me list of friends that use the app and not the whole FB friendlist
FBRequest *friendsRequest = [FBRequest requestForGraphPath:@"/me/friends"];
这篇关于在iOS中抓取Facebook的朋友时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!