我正在尝试通过 twitter oauth 在 twitter 上发布图片,但它给出了错误 http 500

Posted

技术标签:

【中文标题】我正在尝试通过 twitter oauth 在 twitter 上发布图片,但它给出了错误 http 500【英文标题】:I am trying to post image on twitter through twitter oauth but it is giving error http 500 【发布时间】:2013-02-21 11:15:31 【问题描述】:
NSURL *finalURL = [NSURL URLWithString:@"https://api.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];
NSData *imageData = UIImagePNGRepresentation(image);
[theRequest setHTTPMethod:@"POST"];
[theRequest setTimeoutInterval:120];
[theRequest setHTTPShouldHandleCookies:NO];

[theRequest setValue:_clientName    forHTTPHeaderField:@"X-Twitter-Client"];
[theRequest setValue:_clientVersion forHTTPHeaderField:@"X-Twitter-Client-Version"];
[theRequest setValue:_clientURL     forHTTPHeaderField:@"X-Twitter-Client-URL"];
NSString *boundary = @"--0246824681357ACXZabcxyz";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[theRequest setValue:contentType forHTTPHeaderField:@"content-type"];

 NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[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]];
// media 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media[]\"; filename=\"1.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData  dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[theRequest prepare];
[theRequest setHTTPBody:body];

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

【问题讨论】:

【参考方案1】:
//Shearing picture on twitter with Oauth without any third party api.
OAToken *token = [[OAToken alloc] initWithKey:OauthAccessToken secret:OauthAccessSecrateKey]; //Set user Oauth access token and secrate key
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:ConsumerToken secret:ConsumerSecrateKey]; // Application cosumer token and secrate key

// Url for upload pictures
NSURL *finalURL = [NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"];

OAMutableURLRequest *theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL
                                                                   consumer:consumer
                                                                      token:token
                                                                      realm: nil
                                                          signatureProvider:nil];

[theRequest setHTTPMethod:@"POST"];
[theRequest setTimeoutInterval:120];
[theRequest setHTTPShouldHandleCookies:NO];

// Set headers for client information, for tracking purposes at Twitter.(This is optional)
[theRequest setValue:@"HomeShowAppIphone" forHTTPHeaderField:@"X-Twitter-Client"];
[theRequest setValue:@"1.0" forHTTPHeaderField:@"X-Twitter-Client-Version"];
[theRequest setValue:@"http://www.homeshowapp.com/" forHTTPHeaderField:@"X-Twitter-Client-URL"];

NSString *boundary = @"--0246824681357ACXZabcxyz"; // example taken and implemented.
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[theRequest setValue:contentType forHTTPHeaderField:@"content-type"];

NSMutableData *body = [NSMutableData data];

[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:@"%@",@"latest upload"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media[]\"; filename=\"1.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:UIImageJPEGRepresentation([UIImage imageNamed:@"box.png"], 0.5)]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[theRequest prepare];

NSString *oAuthHeader = [theRequest valueForHTTPHeaderField:@"Authorization"];
[theRequest setHTTPBody:body];

NSHTTPURLResponse *response = nil;
NSError *error = nil;

NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest
                                             returningResponse:&response                            
                                                         error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData                                
                                                 encoding:NSUTF8StringEncoding];

【讨论】:

【参考方案2】:

https://api.twitter.com/1.1/statuses/update_with_media.json 将此网址更改为 https://upload.twitter.com/1.1/statuses/update_with_media.json 那就试试吧

【讨论】:

以上是关于我正在尝试通过 twitter oauth 在 twitter 上发布图片,但它给出了错误 http 500的主要内容,如果未能解决你的问题,请参考以下文章

推特 OAuth 1.0。 Opera 重定向失败

挣扎于 Twitter Oauth 回调

无法使用R中的setup_twitter_oauth()函数来使用twitter

Twitter\OAuth2\Python 入门

为啥我的 twitter oauth 访问令牌无效/过期

AngularJS + OAuth