2014-02-26 44 views
1

我正在使用update_with_media Rest API將圖片發佈到Twitter。我得到HHTP 410錯誤每次當我打的API使用update_with_media rest向POST發佈圖像API

我的代碼片斷是:在SA_OAuthTwitterEngine.m

- (NSString *) _uploadImage:(UIImage *)image requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType 
{ 

    NSString *boundary = @"----------------------------991990ee82f7"; 

    NSURL *finalURL = [NSURL URLWithString:@"https://upload.twitter.com/1.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[]\"; 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]; 
} 

在我MGTwitterEngine.m

- (NSString *)upploadd:(UIImage *)img status:(NSString *)status 
{ 
    return [self _uploadImage:img requestType:MGTwitterImageRequest responseType:(MGTwitterStatus)]; 
} 

請幫我找出錯誤。我很困惑。

回答

0

的狀態份額僅爲替換下面的代碼

[body appendData:[[NSString stringWithFormat:@"--%@\r\n\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"%@",status] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

用下面的代碼

//Status 
    [body appendData:[[NSString stringWithFormat:@"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[statusStr dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];