2015-04-02 75 views
3

我想上傳圖片(從iOS設備)到用戶的Twitter賬戶以編程方式使用SLRequest。 Tweet文本上傳到推文中,但圖像從不出現。響應狀態代碼是200.使用SLRequest後圖像中鳴叫 - 圖片未顯示

顯然,基本工作正常,否則tweet將不會顯示在用戶的流中,我已檢查圖像是否被找到,並且(NSData)變量包含預期的編號的字節。

看看下面的代碼 - 正如你可以看到形成註釋掉的代碼,我曾嘗試沒有成功的各種不同的修改...任何想法/幫助感激!

- (void) postToTwitter:(NSString *)postMessage withImageNamed:(NSString *)imageName { 

    // Get Twitter account 
    ACAccountStore *smAccount = [[ACAccountStore alloc] init]; 
    ACAccountType *smType = [smAccount accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

    NSDictionary *postOptions = nil; 

    // Attempt to post the postMessage 
    [smAccount requestAccessToAccountsWithType:smType options:postOptions completion:^(BOOL accessGranted, NSError *error) { 

     // Access code block 

     if (accessGranted) { 

      NSLog(@"Access permitted"); 

      NSArray *smAccountList = [smAccount accountsWithAccountType:smType]; 

      if ([smAccountList count] > 0) { 

       // Get the first Twitter account 
       ACAccount *postingAccount = [smAccountList lastObject]; 

       // Create the post request 
       //SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:postMessage forKey:@"status"]]; 
       //SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://upload.twitter.com/1.1/media/upload.json"] parameters:nil]; 
       SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"] parameters:nil]; 

       // Add image to post 
       // TODO - Check which to use > 
       UIImage *postImage = [UIImage imageNamed:imageName]; 
       //UIImage *postImage = [UIImage imageWithContentsOfFile:imageName]; 
       //NSData *imageData = UIImageJPEGRepresentation (postImage, 0.5f); // Set compression 

       NSData *imageData = UIImagePNGRepresentation(postImage); 

       //[postRequest addMultipartData:imageData withName:@"media" type:@"image/png" filename:@"image.png"]; 
       [postRequest addMultipartData:imageData withName:@"media[]" type:@"multipart/form-data" filename:@"image.png"]; 

       // Add text 
       [postRequest addMultipartData:[postMessage dataUsingEncoding:NSUTF8StringEncoding] withName:@"status" type:@"multipart/form-data" filename:nil]; 

       // Execute the post 
       [postRequest setAccount:postingAccount]; 
       [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 

        // Post code block 
        if (error) { 

         NSLog(@"Post failed - %@", error); 

         if (_delegate && [_delegate respondsToSelector:@selector(didNotPost:)]) 
          [_delegate didNotPost:self]; 
        } else { 

         NSLog(@"Post complete"); 

         if (_delegate && [_delegate respondsToSelector:@selector(didPost:)]) 
          [_delegate didPost:self]; 
        } 
       }]; 
      } 
     } else { 

      NSLog(@"Access denied - %@", error); 

      if (_delegate && [_delegate respondsToSelector:@selector(didNotPost:)]) 
       [_delegate didNotPost:self]; 
     } 


    } ]; 
} 

這是響應:

(lldb) po urlResponse 
<NSHTTPURLResponse: 0x7fb9bf80> { URL: https://api.twitter.com/1.1/statuses/update.json } { status code: 200, headers { 
    "Cache-Control" = "no-cache, no-store, must-revalidate, pre-check=0, post-check=0"; 
    "Content-Disposition" = "attachment; filename=json.json"; 
    "Content-Encoding" = gzip; 
    "Content-Length" = 751; 
    "Content-Type" = "application/json;charset=utf-8"; 
    Date = "Thu, 02 Apr 2015 17:14:20 GMT"; 
    Expires = "Tue, 31 Mar 1981 05:00:00 GMT"; 
    "Last-Modified" = "Thu, 02 Apr 2015 17:14:20 GMT"; 
    Pragma = "no-cache"; 
    Server = "tsa_b"; 
    "Set-Cookie" = "lang=en"; 
    Status = "200 OK"; 
    "Strict-Transport-Security" = "max-age=631138519"; 
    "x-access-level" = "read-write"; 
    "x-connection-hash" = 6f61f3c96ba990931cca3fa950a42b38; 
    "x-content-type-options" = nosniff; 
    "x-frame-options" = SAMEORIGIN; 
    "x-response-time" = 51; 
    "x-transaction" = b2c77803db7e996d; 
    "x-tsa-request-body-time" = 426; 
    "x-twitter-response-tags" = BouncerCompliant; 
    "x-xss-protection" = "1; mode=block"; 
} } 

回答

2

這麼多的痛苦後,我意識到,我被張貼到了錯誤的Twitter的API。以下修改完成了這項工作。

SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"] parameters:nil]; 
+0

update_with_media是,雖然棄用。現在我想,但他們將來會停止。 – GeneCode 2016-01-13 03:24:03

+0

僅供參考,我也面臨着同樣的問題與你 - 用/statuses/update.json只發布文字,而不是multipartadded圖像。 – GeneCode 2016-01-13 03:26:07

4

在我發佈此時,update_with_media已棄用。儘管它現在可行,但它可能會在未來突然停止 - 這會讓您的應用程序在未來不再工作。

了Twitter的API文檔很多閱讀後,我設法使它不使用「update_with_media」工作。

涉及上載的圖像(或視頻)的方法是:

  1. 使用/media/upload.json SLRequest到第一上傳媒體(圖像或視頻)。使用無參數,並添加使用SLRequest的 「addMultipartData法」的形象。對SLRequest

  2. 呼叫performRequestWithHandler。

  3. 等待twitter的響應,從responseData獲取「媒體ID」(將數據轉換爲JSON格式),並將「media_id_string」檢索到NSString中。

  4. 使用/statuses/update.json啓動另一個SLRequest並將媒體ID(key:media_ids)與狀態文本(鍵:狀態)一起附加到參數字典 中。

NSDictionary *parameters = @{@"status": @"See my image:", 
                @"media_ids": mediaID }; 
  • 呼叫performRequestWithHandler在第二SLRequest提交鳴叫。
  • 對於詳細信息代碼,請在這裏看到我的教程博客:http://xcodenoobies.blogspot.my/2016/01/how-to-using-slrequest-to-upload-image.html

    +1

    不錯。請注意,媒體上傳路徑是:'https://upload.twitter.com/1。1/media/upload.json',它具有與狀態路徑不同的子域 – Cbas 2016-03-18 07:41:29