脸书照片下载
Posted
技术标签:
【中文标题】脸书照片下载【英文标题】:Facebook Photo Downloading 【发布时间】:2016-08-20 03:29:20 【问题描述】:我正在尝试从我的应用中的用户下载 Facebook 相册。不幸的是,虽然我确实有一个访问令牌,但我从请求中得到了零张专辑。我没有得到错误,只是得到零。为什么?如果您想查看更多代码或提出更多问题,请提出。请注意,我在当前用户注册时已经授权了他们的 Facebook 权限,并且我已经退出该应用程序并多次打开它(不要认为这会是一个问题,因为我有一个访问令牌..?)
- (void)getAlbums:(OLFacebookAlbumRequestHandler)handler
if ([FBSDKAccessToken currentAccessToken])
// connection is open, perform the request
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *graphpath = @"me/albums?limit=100&fields=id,name,count,cover_photo";
if (self.after)
graphPath = [graphPath stringByAppendingFormat:@"&after=%@", self.after];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:graphPath parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (self.cancelled)
return;
if (error)
[OLFacebookAlbumRequest handleFacebookError:error completionHandler:handler];
return;
NSString *parsingErrorMessage = @"Failed to parse Facebook Response. Please check your internet connectivity and try again.";
NSError *parsingError = [NSError errorWithDomain:kOLErrorDomainFacebookImagePicker code:kOLErrorCodeFacebookImagePickerBadResponse userInfo:@NSLocalizedDescriptionKey: parsingErrorMessage];
id data = [result objectForKey:@"data"];
if (![data isKindOfClass:[NSArray class]])
handler(nil, parsingError, nil);
return;
NSMutableArray *albums = [[NSMutableArray alloc] init];
for (id album in data)
if (![album isKindOfClass:[NSDictionary class]])
continue;
id albumId = [album objectForKey:@"id"];
id photoCount = [album objectForKey:@"count"];
id name = [album objectForKey:@"name"];
if (!([albumId isKindOfClass:[NSString class]] && [photoCount isKindOfClass:[NSNumber class]]
&& [name isKindOfClass:[NSString class]]))
continue;
OLFacebookAlbum *album = [[OLFacebookAlbum alloc] init];
album.albumId = albumId;
album.photoCount = [photoCount unsignedIntegerValue];
album.name = name;
album.coverPhotoURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=small&access_token=%@", album.albumId, [FBSDKAccessToken currentAccessToken].tokenString]];
[albums addObject:album];
// get next page cursor
OLFacebookAlbumRequest *nextPageRequest = nil;
id paging = [result objectForKey:@"paging"];
if ([paging isKindOfClass:[NSDictionary class]])
id cursors = [paging objectForKey:@"cursors"];
id next = [paging objectForKey:@"next"]; // next will be non nil if a next page exists
if (next && [cursors isKindOfClass:[NSDictionary class]])
id after = [cursors objectForKey:@"after"];
if ([after isKindOfClass:[NSString class]])
nextPageRequest = [[OLFacebookAlbumRequest alloc] init];
nextPageRequest.after = after;
handler(albums, nil, nextPageRequest);
];
else
NSString *message = @"No Facebook user authentication found.";
handler(nil, [NSError errorWithDomain:kOLErrorDomainFacebookImagePicker code:kOLErrorCodeFacebookImagePickerNoOpenSession userInfo:@NSLocalizedDescriptionKey: message], nil);
//Code for fetching albums...
- (void)loadNextAlbumPage
self.inProgressRequest = self.albumRequestForNextPage;
self.albumRequestForNextPage = nil;
[self.inProgressRequest getAlbums:^(NSArray/*<OLFacebookAlbum>*/ *albums, NSError *error, OLFacebookAlbumRequest *nextPageRequest)
self.inProgressRequest = nil;
self.loadingIndicator.hidden = YES;
self.albumRequestForNextPage = nextPageRequest;
if (error)
if (self.parentViewController.isBeingPresented)
self.loadingIndicator.hidden = NO;
self.getAlbumError = error; // delay notification so that delegate can dismiss view controller safely if desired.
else
[self.delegate albumViewController:self didFailWithError:error];
return;
NSMutableArray *paths = [[NSMutableArray alloc] init];
for (NSUInteger i = 0; i < albums.count; ++i)
[paths addObject:[NSIndexPath indexPathForRow:self.albums.count + i inSection:0]];
[self.albums addObjectsFromArray:albums];
if (self.albums.count == albums.count)
// first insert request
[self.tableView reloadData];
else
[self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
if (nextPageRequest)
self.tableView.tableFooterView = self.loadingFooter;
else
self.tableView.tableFooterView = nil;
];
//And when they signed up:
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@ @"fields" : @"id,first_name,photos,picture.width(400).height(400)"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
if (!error) //etc etc the method continues.
【问题讨论】:
您是否申请了访问照片的权限(user_photos)?另一个问题是 FB 必须先审核您的请求,然后您的应用才能使用它。 developers.facebook.com/docs/facebook-login/… 否,每次请求不同的权限时都会生成不同的访问令牌。我敢打赌,您的访问令牌没有附加访问用户照片的权限。您是如何请求访问令牌的? 您必须在访问照片之前明确请求 user_photos 权限。 您能否添加您正在执行此类操作的某处的代码快照?这是一个相当简单的修复,您不必排除请求照片。 :) developers.facebook.com/docs/facebook-login/ios#permissions 没错。如果你没有这个,你会需要它。 【参考方案1】:FB 身份验证将为您提供一组特定权限的唯一access_token
。要访问用户照片,您需要请求user_photos
permission。使用FBSDKLoginButton
请求权限。
loginButton.readPermissions = @[@"public_profile", @"email", @"user_photos"];
一旦您拥有具有所需权限的访问令牌,请将其保存在本地(在设备上)以供将来重复使用。如果访问代码仍然有效,则无需再次请求。如果它变得无效(如果用户明确撤销了对您应用的权限),请将其发送回登录屏幕。
【讨论】:
以上是关于脸书照片下载的主要内容,如果未能解决你的问题,请参考以下文章