我正在使用下面的代码来获取 facebook 好友列表,并且它工作正常。但是也需要朋友的头像。
NSString *fbPermissions = @[ @"user_about_me",@"email",@"user_friends"];
NSString *apiIdKey = @"App_Key";
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
id options = @{
ACFacebookAppIdKey: apiIdKey,
ACFacebookPermissionsKey: fbPermissions,
ACFacebookAudienceKey: ACFacebookAudienceFriends,
};
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [accountStore accountsWithAccountType:accountType];
ACAccount *tempAccount = [arrayOfAccounts lastObject];
NSString *string = [NSString stringWithFormat:@"https://graph.facebook.com/me/friends"];
NSURL *fbURL = [NSURL URLWithString:string];
SLRequest *fbRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:fbURL
parameters:nil];
[fbRequest setAccount:tempAccount];
[fbRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error)
{
NSError *jsonError = nil;
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
NSMutableArray *array = [data valueForKey:@"data"];
NSLog(@"Friends array:- %@",array);
}
else
{
[[[UIAlertView alloc]initWithTitle:@"Error"
message:[error description]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil] show];
}
}];
}
}];
要获取个人资料图片,我已更改请求网址:-
NSString *string = [NSString stringWithFormat:@"https://graph.facebook.com/me/friends?fields=picture,name"];
我得到以下回复:-
{
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
}
我不确定它的访问令牌过期错误,因为要将请求 URL 恢复到以前的 URL,代码可以正常工作。