【发布时间】:2016-01-18 09:54:23
【问题描述】:
我在将个人资料图片从 facebook 上传到我的 API 并保存到 Parse 时遇到问题。此代码来自 Udemy。我正在使用 Xcode 7.2。
错误:
2016-01-18 10:14:15.296 MatchedUp[14381:5418567] Error in FB request Error Domain=com.facebook.sdk Code=5 "(null)" UserInfo={com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ErrorSessionKey=<PFReceptionist: 0x12663d2e0>, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 100;
"fbtrace_id" = "FXTRrxOh0+c";
message = "(#100) Tried accessing nonexisting field (photo) on node type (User)";
type = OAuthException;
};
};
code = 400;
}}
代码:
-(void)updateUserInformation
{
//
FBRequest *request = [FBRequest requestForGraphPath:@"me/?fields=name,birthday,first_name,location,gender,interested_in,photo"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error){
NSDictionary *userDictionary = (NSDictionary *)result;
//create URL
NSString *facebookID = userDictionary[@"id"];
NSLog(@"%@",result);
NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]];
NSData *imageData = [NSData dataWithContentsOfURL:pictureURL];
// UIImage *fbImage = [UIImage imageWithData:imageData];
NSLog(@"%@",result);
NSMutableDictionary *userProfile = [[NSMutableDictionary alloc] initWithCapacity:8];
NSLog(@"%@",result);
if(userDictionary[@"name"]){
userProfile[kCCUserProfileNameKey] = userDictionary[@"name"];
}
if(userDictionary[@"user_birthday"]){
userProfile[kCCUserProfileBirthdayKey] = userDictionary[@"user_birthday"];
}
if(userDictionary[@"first_name"]){
userProfile[kCCUserProfileFirstNameKey] = userDictionary[@"first_name"];
}
if(userDictionary[@"location"][@"name"]){
userProfile[kCCUserProfileLocationNameKey] = userDictionary[@"location"][@"name"];
}
if(userDictionary[@"gender"]){
userProfile[kCCUserProfileGenderKey] = userDictionary[@"gender"];
}
if(userDictionary[@"interested_in"]){
userProfile[kCCUserProfileInterestedInKey] = userDictionary[@"interested_in"];
}
if([pictureURL absoluteString]){
userProfile[kCCUserProfilePictureURL] = [pictureURL absoluteString];
}
[[PFUser currentUser] setObject: userProfile forKey:kCCUserProfileKey];
[[PFUser currentUser] saveInBackground];
[self requestImage];
}
else{
NSLog(@"Error in FB request %@", error);
}
}];
}
-(void) uploadPFFileToParse:(UIImage *)image
{
NSData *imageData = UIImageJPEGRepresentation(image, 0.8);
if(!imageData){
NSLog(@"imageData was not found");
return;
}
PFFile *photoFile = [PFFile fileWithData:imageData];
[photoFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
if(succeeded){
PFObject *photo = [PFObject objectWithClassName:kCCPhotoClassKey];
[photo setObject:[PFUser currentUser] forKey:kCCPhotoUserKey];
[photo setObject:photoFile forKey:kCCPhotoPictureKey];
[photo saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
NSLog(@"Photo saved successfully");
}];
}
}];
}
-(void) requestImage
{
PFQuery *query = [PFQuery queryWithClassName:kCCPhotoClassKey];
[query whereKey:kCCPhotoUserKey equalTo:[PFUser currentUser]];
[query countObjectsInBackgroundWithBlock:^(int number, NSError * _Nullable error) {
if(number == 0 )
{
PFUser *user = [PFUser currentUser];
self.imageData = [[NSMutableData alloc] init];
NSURL *profilePictureUrl = [NSURL URLWithString:user [kCCUserProfileKey][kCCUserProfilePictureURL]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:profilePictureUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:4.0f];
NSURLConnection *urlConnection = [[NSURLConnection alloc]initWithRequest:urlRequest delegate:self];
if(!urlConnection){
NSLog(@"Failed to Download Picture");
}
}
}];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.imageData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage *profileImage = [UIImage imageWithData:self.imageData];
[self uploadPFFileToParse:profileImage];
}
@end
知道如何解决这个问题吗? 非常感谢!
【问题讨论】:
-
个人资料图片的字段名称是
picture,而不是photo。 -
@CBroe 我改了,但还是不行。现在不显示错误消息,但我在 Parse 中看不到上传文件。你知道我哪里出错了吗?
标签: objective-c facebook sdk ios9