【问题标题】:Twitter's statuses/update_with_media on iOS returns 500 errorTwitter 在 iOS 上的 statuses/update_with_media 返回 500 错误
【发布时间】:2011-11-11 02:52:45
【问题描述】:

有人使用新的 twitter 功能,statuses/update_with_media 吗?
我尝试使用它上传照片,但它返回内部服务器错误。
我的要求如下:

*********************** start of request *************************
POST /1/statuses/update_with_media.json HTTP/1.1
Host: upload.twitter.com
User-Agent: CGJ/0.9.7 CFNetwork/485.13.8 Darwin/10.6.0
X-Twitter-Client: MGTwitterEngine
X-Twitter-Client-Version: 1.0
X-Twitter-Client-Url: http://mattgemmell.com/source
Content-Type: multipart/form-data; boundary=--------Asrf456BGe4h
Authorization: OAuth realm="", oauth_consumer_key="wEmJfFrYHMgmCGjAzHdS7Q", oauth_token="366466306-U2RDh7faJtMyi39ALIo2PooaKNT6BxHVT23UUVe4", oauth_signature_method="HMAC-SHA1", oauth_signature="Ev2RxuZbk3DiTQryIDUTPIJxYYE%3D", oauth_timestamp="1315289679", oauth_nonce="A968261D-CB8A-4E07-87AD-47AB138D332D", oauth_version="1.0", oauth_verifier="0087890"
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Content-Length: 4528
Connection: keep-alive

----------Asrf456BGe4h
Content-Disposition: form-data; name="status"

Test image uploading
----------Asrf456BGe4h
Content-Disposition: form-data; name="media[]"; filename="1.png"
Content-Type: image/png

iVBORw0... - base64 image data
----------Asrf456BGe4h
*********************** end of request *************************

我正在使用 MGTwitterEngine,这里是生成此请求的代码:

- (NSString *) _uploadImage:(UIImage *)image requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType
{
  NSString *boundary = @"--------Asrf456BGe4h";

  NSURL *finalURL = [NSURL     URLWithString:@"http://upload.twitter.com/1/statuses/update_with_media.json"];
  if (!finalURL) {
    return nil;
  }

  OAMutableURLRequest *theRequest = [[[OAMutableURLRequest alloc] initWithURL:finalURL
                                                                 consumer:self.consumer 
                                                                    token:_accessToken 
                                                                    realm: nil
                                                        signatureProvider:nil] autorelease];
  [theRequest setHTTPMethod:@"POST"];
  [theRequest setHTTPShouldHandleCookies:NO];

  // Set headers for client information, for tracking purposes at Twitter.
  [theRequest setValue:_clientName    forHTTPHeaderField:@"X-Twitter-Client"];
  [theRequest setValue:_clientVersion forHTTPHeaderField:@"X-Twitter-Client-Version"];
  [theRequest setValue:_clientURL     forHTTPHeaderField:@"X-Twitter-Client-URL"];

  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
  [theRequest setValue:contentType forHTTPHeaderField:@"content-type"];

  // Set request body, if specified (hopefully so), with 'source' parameter if appropriate.

  NSMutableData *body = [NSMutableData dataWithLength:0];
  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"status\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[[NSString stringWithString:@"Honeymoon application uploads image\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"media[]\"; filename=\"1.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[[NSString stringWithString:@"Content-Type: image/png\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
  [body appendData:[[NSString stringWithString:[UIImagePNGRepresentation(image) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[[NSString stringWithFormat:@"--%@", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

  [theRequest setHTTPBody:body];

  [theRequest prepare];

  MGTwitterHTTPURLConnection *connection;
  connection = [[MGTwitterHTTPURLConnection alloc] initWithRequest:theRequest 
                                                      delegate:self 
                                                   requestType:requestType 
                                                  responseType:responseType];

  if (!connection) {
    return nil;
  } else {
    [_connections setObject:connection forKey:[connection identifier]];
    [connection release];
  }

  return [connection identifier];  
}

谁能帮我找出问题所在?
提前致谢!

【问题讨论】:

    标签: ios twitter uploading


    【解决方案1】:

    我已经解决了这个问题,但我真的不知道根本原因在哪里。
    因此,所做的就是以以下方式更改代码:

    - (NSString *) _uploadImage:(UIImage *)image requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType
    {
    
      NSString *boundary = @"----------------------------991990ee82f7";
    
      NSURL *finalURL = [NSURL URLWithString:@"http://upload.twitter.com/1/statuses/update_with_media.json"];
      if (!finalURL) {
        return nil;
      }
    
    
      OAMutableURLRequest *theRequest = [[[OAMutableURLRequest alloc] initWithURL:finalURL
                                                                     consumer:self.consumer 
                                                                        token:_accessToken 
                                                                        realm: nil
                                                            signatureProvider:nil] autorelease];
    
      [theRequest setHTTPMethod:@"POST"];
      [theRequest setHTTPShouldHandleCookies:NO];
    
      // Set headers for client information, for tracking purposes at Twitter.
      [theRequest setValue:_clientName    forHTTPHeaderField:@"X-Twitter-Client"];
      [theRequest setValue:_clientVersion forHTTPHeaderField:@"X-Twitter-Client-Version"];
      [theRequest setValue:_clientURL     forHTTPHeaderField:@"X-Twitter-Client-URL"];
    
    
      NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
      [theRequest setValue:contentType forHTTPHeaderField:@"content-type"];
    
      NSMutableData *body = [NSMutableData dataWithLength:0];
      [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
      [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"media_data[]\"; filename=\"1.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
      [body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"status\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithString:@"Honeymoon uploads image\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    // --------------------------------------------------------------------------------
    // modificaiton from the base clase
    // our version "prepares" the oauth url request
    // --------------------------------------------------------------------------------
        [theRequest prepare];
    
      [theRequest setHTTPBody:body];
    
      // Create a connection using this request, with the default timeout and caching policy, 
      // and appropriate Twitter request and response types for parsing and error reporting.
      MGTwitterHTTPURLConnection *connection;
      connection = [[MGTwitterHTTPURLConnection alloc] initWithRequest:theRequest 
                                                          delegate:self 
                                                       requestType:requestType 
                                                      responseType:responseType];
    
      if (!connection) {
        return nil;
      } else {
        [_connections setObject:connection forKey:[connection identifier]];
        [connection release];
      }
    
      return [connection identifier];  
    }
    

    与之前的代码有 3 处不同:
    1.数据顺序不同,表示先填充图像数据,后填充状态数据
    2. 使用 media_data[] 键代替 media[]
    3.prepare后调用set http body请求

    希望对某人有所帮助。

    【讨论】:

    • 感谢一百万你拯救了我的一天;)
    • 您好,我也遇到了同样的问题,但是当我尝试实现您的代码时,它显示此行 [theRequest prepare];准备方法不可用。请告诉您下载 mgtwitterengine 文件的链接。
    • 如果没有 cmets,照片将无法上传。我收到失败并出现错误:Error Domain=NSXMLParserErrorDomain Code=4“操作无法完成
    猜你喜欢
    • 2014-02-26
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    相关资源
    最近更新 更多