使用图表API在Facebook墙上上传照片时出错

时间:2011-10-18 06:16:16

标签: iphone facebook

我打算在iphone app上工作,在facebook墙上上传照片。 我正在使用this code

这里

- (void)rateTapped:(id)sender {

NSString *likeString;
NSString *filePath = nil;
if (_imageView.image == [UIImage imageNamed:@"angelina.jpg"]) {
    filePath = [[NSBundle mainBundle] pathForResource:@"angelina" ofType:@"jpg"];
    likeString = @"babe";
} else if (_imageView.image == [UIImage imageNamed:@"depp.jpg"]) {
    filePath = [[NSBundle mainBundle] pathForResource:@"depp" ofType:@"jpg"];
    likeString = @"dude";
} else if (_imageView.image == [UIImage imageNamed:@"maltese.jpg"]) {
    filePath = [[NSBundle mainBundle] pathForResource:@"maltese" ofType:@"jpg"];
    likeString = @"puppy";
}
if (filePath == nil) return;

NSString *adjectiveString;
if (_segControl.selectedSegmentIndex == 0) {
    adjectiveString = @"cute";
} else {
    adjectiveString = @"ugly";
}

NSString *message = [NSString stringWithFormat:@"I think this is a %@ %@!", adjectiveString, likeString];

NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/photos"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request addFile:filePath forKey:@"file"];
//[request setFile:filePath withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
[request setPostValue:message forKey:@"message"];
[request setPostValue:_accessToken forKey:@"access_token"];
[request setDidFinishSelector:@selector(sendToPhotosFinished:)];


[request setDelegate:self];
[request startAsynchronous];
}

这里我的照片ID是:( null)

 - (void)sendToPhotosFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];

NSMutableDictionary *responseJSON = [responseString JSONValue];
NSString *photoId = [responseJSON objectForKey:@"id"];
NSLog(@"Photo id is: %@", photoId);

NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@?access_token=%@", photoId, [_accessToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *newRequest = [ASIHTTPRequest requestWithURL:url];
[newRequest setDidFinishSelector:@selector(getFacebookPhotoFinished:)];

[newRequest setDelegate:self];
[newRequest startAsynchronous];

}

- (void)getFacebookProfileFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(@"Got Facebook Profile: %@", responseString);

NSString *likesString;
NSMutableDictionary *responseJSON = [responseString JSONValue];   
NSArray *interestedIn = [responseJSON objectForKey:@"interested_in"];
if (interestedIn != nil) {
    NSString *firstInterest = [interestedIn objectAtIndex:0];
    if ([firstInterest compare:@"male"] == 0) {
        [_imageView setImage:[UIImage imageNamed:@"depp.jpg"]];
        likesString = @"dudes";
    } else if ([firstInterest compare:@"female"] == 0) {
        [_imageView setImage:[UIImage imageNamed:@"angelina.jpg"]];
        likesString = @"babes";
    }        
} else {
    [_imageView setImage:[UIImage imageNamed:@"maltese.jpg"]];
    likesString = @"puppies";
}

NSString *username;
NSString *firstName = [responseJSON objectForKey:@"first_name"];
NSString *lastName = [responseJSON objectForKey:@"last_name"];
if (firstName && lastName) {
    username = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
} else {
    username = @"mysterious user";
}

_textView.text = [NSString stringWithFormat:@"Hi %@!  I noticed you like %@, so tell me if you think this pic is hot or not!",
                  username, likesString];

[self refresh];    
}

但是这里遇到了以下错误

在控制台中我收到了一个错误:得到Facebook照片:{“error”:{“message”:“(#803)您请求的一些别名不存在:(null)”,“type “:” OAuthException“}}

我通过更改各种应用ID来尝试此操作,但我的问题无法解决。 任何人帮助我实现这一目标.........

提前谢谢......

1 个答案:

答案 0 :(得分:0)

你试试这个,

 NSData *imagedata = UIImageJPEGRepresentation(urPhoto, 10);
 [newRequest setPostValue:imagedata forKey:@"data"];

或添加这样的图片,

 [newRequest setPostValue:link forKey:@"data"];