我正在尝试获取用户详细信息,但目前无法获取图片。这是我得到的错误:
{
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
}
这是我的代码:
if(!self.store)
{
ACAccountStore *store1 = [[ACAccountStore alloc] init];
self.store=store1;
[store1 release];
}
ACAccountType *fbAccountType =
[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray * permissions = @[@"read_stream", @"publish_stream",@"email", @"user_birthday",@"publish_actions",@"user_photos"];
NSDictionary * dict = @{ACFacebookAppIdKey : @"my_key", ACFacebookPermissionsKey : permissions, ACFacebookAudienceKey : ACFacebookAudienceOnlyMe};
// Request permission from the user to access the available Twitter accounts
[store requestAccessToAccountsWithType:fbAccountType options:dict completion:^(BOOL granted, NSError *error) {
__block NSString * statusText = nil;
if (granted) {
statusText = @"Logged in";
NSArray * accounts = [store accountsWithAccountType:fbAccountType];
store = [accounts lastObject];
account = [accounts objectAtIndex:0];
NSLog(@"account is: %@", account);
NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me?fields=picture"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:nil];
request.account = account;
[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {
if(!error){
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions error:&error];
NSLog(@"Dictionary contains: %@", list );
}
else{
//handle error gracefully
}
}];
}
如果我使用https://graph.facebook.com/me作为网址,那么它可以正常工作。但我也需要个人资料照片。怎么办?
答案 0 :(得分:4)
“fields”是一个参数,而不是URL本身的一部分。这有效:
SLRequest *videoLimitsRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:[NSURL URLWithString:@"https://graph.facebook.com/me"]
parameters:@{@"fields": @"video_upload_limits"}];
仅供参考,当您将帐户附加到SLRequest时,会自动添加令牌。
答案 1 :(得分:0)
你可以像这样使用accesstoken运行fql
来自用户的https://graph.facebook.com/fql?q=SELECT名称,其中uid = YourFAcebookID& access_token = ACCESSTOKEN
这对你有用。
答案 2 :(得分:0)
这有点旧,但我忘了回答。 从FB api 3.2开始,所有内容都在api内进行管理,适用于iOS 5和6本机登录。这是我做的:
FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];
FBRequest *request = [FBRequest requestWithGraphPath:@"me/albums" parameters:nil HTTPMethod:@"GET"];
FBRequestHandler handler =
^(FBRequestConnection *connection, id result, NSError *error)
{
if(!error)
{
NSDictionary *list =(NSDictionary*)result;
int flag = 0;
for (int index = 0; index < [[list objectForKey:@"data"] count];index++)
{
if ([[[[list objectForKey:@"data"] objectAtIndex:index] objectForKey:@"name"] isEqualToString:@"Profile Pictures"])
{
[self fetchAlbumImages:[[[list objectForKey:@"data"] objectAtIndex:index] objectForKey:@"id"]];
flag = 1;
}
}
if (flag == 0)
{
[self fetchAlbumImages:@"No Album"];
}
}
else
{
}
};
[newConnection addRequest:request completionHandler:handler];
//[self.requestConnection cancel];
[newConnection start];
[newConnection release];
}
- (void) fetchAlbumImages:(NSString*)albumId
{
if ([albumId isEqualToString:@"No Album"])
{
NSMutableArray *albumArray = [[NSMutableArray alloc]init];
[self performSelectorOnMainThread:@selector(sendRegistrationRequest:) withObject:albumArray waitUntilDone:YES];
[albumArray release];
}
else
{
FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];
FBRequest *request = [FBRequest requestWithGraphPath:[NSString stringWithFormat:@"%@/photos",albumId] parameters:nil HTTPMethod:@"GET"];
FBRequestHandler handler =
^(FBRequestConnection *connection, id result, NSError *error)
{
if(!error)
{
NSDictionary *list =(NSDictionary*)result;
NSMutableArray *albumArray = [[NSMutableArray alloc]init];
for (int index = 0; index < ([[list objectForKey:@"data"] count]<10?[[list objectForKey:@"data"] count]:10);index++)
{
[albumArray addObject:[[[list objectForKey:@"data"] objectAtIndex:index] objectForKey:@"source"]];
}
[self performSelectorOnMainThread:@selector(sendRegistrationRequest:) withObject:albumArray waitUntilDone:YES];
}
else
{
}
};
[newConnection addRequest:request completionHandler:handler];
//[self.requestConnection cancel];
self.requestConnection = newConnection;
[newConnection start];
[newConnection release];
}
}