2011-10-10 112 views
4

我已經做了一個應用程序,在我的應用程序中我已經整合了Facebook來共享信息。用於Facebook集成的 我在應用程序中使用Graph API。 現在在我的應用程序中,我想在用戶的牆上上傳照片。 我已經使用此代碼在用戶的牆上上傳照片。如何在iPhone上使用Graph API在Facebook上上傳照片?

//上傳照片

- (void)uploadPhoto:(id)sender { 


NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Image1" ofType:@"jpg"]; 

NSString *message = [NSString stringWithFormat:@"I think this is a Great Image!"]; 

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

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
[request addFile:filePath 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 startAsynchronous]; 
} 

但是,在這裏我得到 responseString是{ 「錯誤」:{ 「消息」: 「錯誤校驗應用程序」, 「類型」: 「OAuthException」} }照片ID是:(null)

圖像不會在用戶的牆上上傳。 所以,請告訴我如何解決它。

+0

正如我在我的回答中所說的,您必須授權facebook才能獲得有效的訪問令牌。你在使用iOS的Facebook SDK嗎? http://developers.facebook.com/docs/guides/mobile/ios/ – jbat100

+0

我也有同樣的問題你解決了嗎?如果可以,請告訴造成此錯誤的原因。 – Vijay

回答

1

首先有您授權您的應用程序通過這樣(使用FBConnect SDK)上傳的圖片,您可以檢出的所有權限here

NSArray* permissions = [NSArray arrayWithObjects:@"publish_stream", @"user_photos", nil]; 
[facebook authorize:permissions]; 

下一個問題是,Facebook的不允許以這種方式發送的帖子鏈接到他們的域名託管的圖片(我知道這真的很煩人,也許值得檢查,如果事情發生了變化,自4月以來)。我花了相當多的時間來完成這一個。我破解它的方式是使用bitly創建重定向URL(您可以使用它們的API以編程方式訪問它們的服務,但它有一個obj-c包裝器,但我將其更改爲異步),並在該帖子中發送該URL。

2

可以幫助ü

//facebook post a image on wall 
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
             userImgView.image, @"picture", 
             nil]; 

[facebook requestWithGraphPath:@"me/photos" 
        andParams:params 
       andHttpMethod:@"POST" 
        andDelegate:self]; 
+1

facebook是什麼樣的數據類型 – 2014-03-20 13:54:45

1

另一種方式是波紋管在Facebook上後的圖像...

NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:2]; 

    //create a UIImage (you could use the picture album or camera too) 

    UIImage *picture = [UIImage imageNamed:"yourImageName"]; 
    //create a FbGraphFile object insance and set the picture we wish to publish on it 
    FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture]; 

    //finally, set the FbGraphFileobject onto our variables dictionary.... 
    [variables setObject:graph_file forKey:@"file"]; 

    [variables setObject:@"write your Message Here" forKey:@"message"]; 

    //the fbGraph object is smart enough to recognize the binary image data inside the FbGraphFile 
    //object and treat that is such..... 
    //FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"117795728310/photos" withPostVars:variables]; 
    FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"me/photos" withPostVars:variables]; 

希望,這幫助你... :)

0

Facebook的API使用OAuth對請求進行身份驗證。您將需要使用可以請求適當的臨時/客戶端令牌的庫,併爲請求生成適當的HTTP頭。這是一個非常複雜的過程,涉及請求參數的哈希和規範化。

你不能這樣製作你自己的手動HTTP請求。

0

您可以使用Facebook的本地函數

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: UIImageJPEGRepresentation(postImage, 1.0), @"source", self.postTextView.text, @"message", nil]; 
      /* make the API call */ 

[FBRequestConnection startWithGraphPath:@"/me/photos" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
{ 

     if (error == nil) { 
       NSLog("Success"); 
     } 
     else { 
       NSLog("Failed"); 
     } 

}]; 

使用此代碼,你可以上傳圖片與消息。 問候函