![](/img/trans.png)
[英]Twitter's statuses/update_with_media on iOS returns 500 error
[英]how to post image to twitter by using http://upload.twitter.com/1/statuses/update_with_media for below ios 5.0
我正在嘗試通過使用新的api statuss / update_with_media在Twitter上共享圖像。 然后我搜索並從dev.twitter.com/discussion頁面獲得以下代碼。 代碼在下面。
在這里輸入代碼
- (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;
}
NSLog(@"-> Open Connection: %@", finalURL);
OAMutableURLRequest *theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL
consumer:self.consumer
token:_accessToken
realm: nil
signatureProvider:nil];
[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.jpg\"\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];
}
然后我在SA_OAuthtwitterengine中實現了此代碼,因為我使用oauth方法將文本或圖像發布到Twitter,因為我的應用程序部署目標是從3.0(ios,ipod)形式開始的,所以我嘗試使用此方法。
現在我的問題是如何創建代碼以將消息發送到Twitter。
我嘗試使用以下類型將圖像發送到Twitter
[_engine sendupdate:@"http://www.prepare-community.com/ShareFiles/index-fr.jpg"];
我收到連接錯誤,這意味着401錯誤。
能否請您發送給我如何使用上述代碼將圖像發送到Twitter?
這是使用新的Twitter api update_with_media上傳圖像的解決方案。 使用此API,您可以將圖片直接上傳到Twitter帳戶。 上面的_uploadimage代碼應首先放在SA_OAuthtwitter引擎.m文件中,並在.h文件中刪除。
並做一個小正確的放置文件名= 1.jpg而不是1.png的位置。
現在,只需將以下代碼放入MGTwitterengine.m類。 這是下面的代碼
- (NSString *)_uploadImage:(UIImage *)image withStatus:(NSString *)status
{
return [self _uploadImage:image withStatus:status requestType:MGTwitterImageRequest responseType:MGTwitterStatuses];
}
然后,我們使用此代碼將圖像上傳到Twitter。 那是,
[_engine _uploadImage:帶有狀態的圖片:您的狀態];
希望它對您有用...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.