我試圖通過使用新的API狀態/ update_with_media在Twitter上分享圖像。然後我搜索並從dev.twitter.com/discussion頁面下面得到了代碼。這裏的代碼如下。如何通過使用http://upload.twitter.com/1/statuses/update_with_media下面的ios 5.0發佈映像到twitter
輸入代碼在這裏
- (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];
}
後來才知道是因爲我使用後的文本或圖像的OAuth方法嘰嘰喳喳,因爲我的應用程序部署目標開始表格3.0 SA_OAuthtwitterengine執行代碼(IOS,iPod的) ,所以我嘗試這種方法。
現在我的問題是如何創建代碼發送圖像Twitter的消息。
我嘗試發送圖像使用下面的類型
[_engine sendupdate:@"http://www.prepare-community.com/ShareFiles/index-fr.jpg"];
我有連接錯誤,這意味着401錯誤到Twitter。
請問我可以如何使用上面的代碼發送圖像到twitter?
http://stackoverflow.com/questions/29555971/ios-unable-to-upload-media-with-twitter-fabric-new-sdk/36509939 #36509939 – jose920405 2016-04-08 21:58:02