Twitter's statuses/update_with_media on iOS returns 500 error

狂风中的少年 提交于 2019-11-29 05:15:14

I've solved the problem, but really I don't know where root cause was.
So, what was done is changing the code in following manner:

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

There are 3 differences with previous code:
1. Different order of data, it means that firstly image data was filled, and then status data
2. Using media_data[] key instead of media[]
3. Call set http body of request after prepare

Hope it will be helpful for someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!