iPhone:无法在墙上发布图像(Facebook)

发布于 2024-11-04 10:58:09 字数 3737 浏览 0 评论 0原文

我正在尝试将图像上传到 facebook,并且我尝试在 facebook 中上传图像 id,在获得该 id 后,我尝试创建一个请求,将该照片 ID 附加到图形库中,但每次我都会收到错误。该代码最初有效,但现在其失败

    NSString *message = [NSString stringWithFormat:@"For test only --> I think this its uploaded !"];

    NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/photos"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request addFile:savedImagePath forKey:@"file"];
    [request setPostValue:message forKey:@"message"];
    [request setPostValue:_accessToken forKey:@"access_token"];
    [request setDidFinishSelector:@selector(sendToPhotosFinished:)];

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


}

    - (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 startSynchronous];

}

    - (void)getFacebookPhotoFinished:(ASIHTTPRequest *)request
{
    NSString *responseString = [request responseString];
    NSLog(@"Got Facebook Photo: %@", responseString);

    NSMutableDictionary *responseJSON = [responseString JSONValue];   

    NSString *link = [responseJSON objectForKey:@"link"];
    if (link == nil) return;   
    NSLog(@"Link to photo: %@", link);

    NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];
    ASIFormDataRequest *newRequest = [ASIFormDataRequest requestWithURL:url];
    [newRequest setPostValue:@"I'm learning how to post to Facebook from an iPhone app!" forKey:@"message"];
    [newRequest setPostValue:@"Check out the tutorial!" forKey:@"name"];
    [newRequest setPostValue:@"This tutorial shows you how to post to Facebook using the new Open Graph API." forKey:@"caption"];
    [newRequest setPostValue:@"From Ray Wenderlich's blog - an blog about iPhone and iOS development." forKey:@"description"];
    [newRequest setPostValue:@"http://google.com" forKey:@"link"];
    [newRequest setPostValue:link forKey:@"picture"];
    [newRequest setPostValue:_accessToken forKey:@"access_token"];
    [newRequest setDidFinishSelector:@selector(postToWallFinished:)];

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

}

    - (void)postToWallFinished:(ASIHTTPRequest *)request
{
    NSString *responseString = [request responseString];

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

    UIAlertView *av = [[[UIAlertView alloc] 
                        initWithTitle:@"Sucessfully posted to photos & wall!" 
                        message:@"Check out your Facebook to see!"
                        delegate:nil 
                        cancelButtonTitle:@"OK"
                        otherButtonTitles:nil] autorelease];
    [av show];

}

错误是

-JSONValue 失败。错误跟踪是:( “Error Domain=org.brautaset.JSON.ErrorDomain Code=4 \“有效片段,但不是 JSON\” UserInfo=0x6078ae0 {NSLocalizedDescription=有效片段,但不是 JSON}”

请赐教。

问候 安基特

I am trying to upload a Image to facebook and I gam getting image id uploaded in facebook and after getting that id I am trying to create a request appending that photo ID in grap library but everytime I am getting error. The code worked initially but now its failing

    NSString *message = [NSString stringWithFormat:@"For test only --> I think this its uploaded !"];

    NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/photos"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request addFile:savedImagePath forKey:@"file"];
    [request setPostValue:message forKey:@"message"];
    [request setPostValue:_accessToken forKey:@"access_token"];
    [request setDidFinishSelector:@selector(sendToPhotosFinished:)];

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


}

    - (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 startSynchronous];

}

    - (void)getFacebookPhotoFinished:(ASIHTTPRequest *)request
{
    NSString *responseString = [request responseString];
    NSLog(@"Got Facebook Photo: %@", responseString);

    NSMutableDictionary *responseJSON = [responseString JSONValue];   

    NSString *link = [responseJSON objectForKey:@"link"];
    if (link == nil) return;   
    NSLog(@"Link to photo: %@", link);

    NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];
    ASIFormDataRequest *newRequest = [ASIFormDataRequest requestWithURL:url];
    [newRequest setPostValue:@"I'm learning how to post to Facebook from an iPhone app!" forKey:@"message"];
    [newRequest setPostValue:@"Check out the tutorial!" forKey:@"name"];
    [newRequest setPostValue:@"This tutorial shows you how to post to Facebook using the new Open Graph API." forKey:@"caption"];
    [newRequest setPostValue:@"From Ray Wenderlich's blog - an blog about iPhone and iOS development." forKey:@"description"];
    [newRequest setPostValue:@"http://google.com" forKey:@"link"];
    [newRequest setPostValue:link forKey:@"picture"];
    [newRequest setPostValue:_accessToken forKey:@"access_token"];
    [newRequest setDidFinishSelector:@selector(postToWallFinished:)];

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

}

    - (void)postToWallFinished:(ASIHTTPRequest *)request
{
    NSString *responseString = [request responseString];

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

    UIAlertView *av = [[[UIAlertView alloc] 
                        initWithTitle:@"Sucessfully posted to photos & wall!" 
                        message:@"Check out your Facebook to see!"
                        delegate:nil 
                        cancelButtonTitle:@"OK"
                        otherButtonTitles:nil] autorelease];
    [av show];

}

error is

-JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=4 \"Valid fragment, but not JSON\" UserInfo=0x6078ae0 {NSLocalizedDescription=Valid fragment, but not JSON}"

Please enlighten me on this.

Regards
Ankit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文