我在我的应用程序中使用twitter + OAuth和MGTwitterengine,并成功发布评论到twitter。但现在我的问题是使用
分享图片到twitterhttps://upload.twitter.com/1/statuses/update_with_media.json。我搜索了许多网站并通过使用这个直接上传到twitter api获取源代码
https://upload.twitter.com/1/statuses/update_with_media.json。这里的代码如下。
-(void)UploadimageToTwitter
{
//NSString *boundary = @"----------------------------991990ee82f7";
// NSURL *finalURL = [NSURL URLWithString:@"http://upload.twitter.com/1.1/statuses/update_with_media.json"];
NSString *accessTokenKey = kOAuthaccessTokenKey;
NSString *secretTokenKey = kOAuthsecretTokenKey;
NSURL *finalURL = [NSURL URLWithString:@"http://upload.twitter.com/1/statuses/update_with_media.json"];
/*if (!finalURL)
{
return nil;
}*/
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:kOAuthConsumerKey secret:kOAuthConsumerSecret];
//Shearing picture on twitter with Oauth without any third party api.
OAToken *token = [[OAToken alloc] initWithKey:accessTokenKey secret:secretTokenKey]; //Set user Oauth access token and secrate key
//OAConsumer *consumer = [[OAConsumer alloc] initWithKey:ConsumerToken secret:ConsumerSecrateKey]; // Application cosumer token and secrate key
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.
[theRequest setValue:DEFAULT_CLIENT_NAME forHTTPHeaderField:@"X-Twitter-Client"];
[theRequest setValue:DEFAULT_CLIENT_VERSION forHTTPHeaderField:@"X-Twitter-Client-Version"];
[theRequest setValue:DEFAULT_CLIENT_URL 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 dataWithLength:0];
NSMutableData *body=[NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//status
[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 stringWithString:@"Content-Disposition: form-data; name=\"status\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithFormat:@"%@",status] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",@"Latest Uploading"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//media
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"media_data[]\"; filename=\"sunflower.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"media[]\"; filename=\"index.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type:application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
UIImage *image1=[UIImage imageNamed:@"sunflower.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image1, 1.0);
// [body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\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 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];
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];
NSLog(@"oAuthHeader = %@ =",oAuthHeader);
NSLog(@"ResponsString = \n%@",responseString);
}
使用此代码我得到的响应字符串在
之下{"errors":[{"message":"Internal error","code":131}]}
这是服务器错误或我的错误。我真的不知道上面的代码我错在哪里。
如果有人知道答案,请指导我。你的帮助挽救了我的生命,因为我在三个月内使用Twitpic,yfrog以及post / update_with_media这三种不同的api搜索上述任务。
答案 0 :(得分:0)
NSString *status = tweetText.text;
TWRequest *sendTweet;
if(image1 == nil)
{
sendTweet = [[TWRequest alloc]
initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"]
parameters:[NSDictionary dictionaryWithObjectsAndKeys:status, @"status", nil]
requestMethod:TWRequestMethodPOST];
}
else
{
sendTweet = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"]
parameters:[NSDictionary dictionaryWithObjectsAndKeys:status, @"status",image1,@"media" , nil]
requestMethod:TWRequestMethodPOST];
[sendTweet addMultiPartData:[tweetText.text dataUsingEncoding:NSUTF8StringEncoding] withName:@"status" type:@"multipart/form-data"];
[sendTweet addMultiPartData:UIImagePNGRepresentation(image1) withName:@"media" type:@"multipart/png"];
}
sendTweet.account = self.account;
[sendTweet performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if ([urlResponse statusCode] == 200)
{
dispatch_sync(dispatch_get_main_queue(),
^{
[self displayText:@"Tweet Done"];
});
}
else
{
NSLog(@"Problem sending tweet: %@", error);
}
}];
希望此代码适合您,它适用于我,在此您可以发布图像以及仅文本。祝你好运