Iphone 使用“上传图像到twitter”;http://upload.twitter.com/1/statuses/update_with_media.json"
任何人都可以指导我或使用此api“”生成一些源代码。这是一个twitter api,用于直接将图像从iphone应用程序上传到twitter 这是我的密码Iphone 使用“上传图像到twitter”;http://upload.twitter.com/1/statuses/update_with_media.json",iphone,objective-c,Iphone,Objective C,任何人都可以指导我或使用此api“”生成一些源代码。这是一个twitter api,用于直接将图像从iphone应用程序上传到twitter 这是我的密码 enter code here -(void)PostToTwitter { NSString *consumerKey = kOAuthConsumerKey; NSString *consumerSecret = kOAuthConsumerSecret; NSString *accessTokenKey =
enter code here
-(void)PostToTwitter
{
NSString *consumerKey = kOAuthConsumerKey;
NSString *consumerSecret = kOAuthConsumerSecret;
NSString *accessTokenKey = kOAuthaccessTokenKey;
NSString *secretTokenKey = kOAuthsecretTokenKey;
NSString *url = @"https://upload.twitter.com/1/statuses/update_with_media.json";
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:consumerKey secret:consumerSecret];
OAToken *authToken = [[OAToken alloc] initWithKey:accessTokenKey secret:secretTokenKey];
OAMutableURLRequest *postRequest = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] consumer:consumer token:authToken realm:nil signatureProvider:nil];
[postRequest setHTTPMethod:@"POST"];
[postRequest prepare];
NSString *message = @"Testing Tweet and image upload to Twitter server directly.";
//NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
NSString *stringBoundary = @"----------------------------9fa90e137c50";
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data;boundary=%@",stringBoundary];
[postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
// message part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[message dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"media[]\"; filename=\"sunflower.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
UIImage *img=[UIImage imageNamed:@"sunflower.jpg"] ;//=[self correctImageOrientation:img]; //cFun
NSData *imageData = UIImageJPEGRepresentation(img, 90);
/*NSURL *imageURL = [NSURL URLWithString:@"http://www.prepare-community.com/ShareFiles/index-fr.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];*/
// NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"road.jpg"]);
[postBody appendData:imageData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];
if (theConnection)
{
webData = [NSMutableData data];
NSLog(@"web data = %@ =",webData);
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//[responseData appendData:data];
[webData appendData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
//[connection release];
NSString* responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSLog(@"result: %@", responseString);
// [responseString release];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"error - read error object for details");
}
在上面的代码中,我得到了WebData的输出是空标记
响应字符串是{“errors”:[{“message”:“internalerror”,“code”:131}]}
请告诉我为什么会出现这个错误。我在上面的代码中出错的地方。如果有人知道使用udate_with_media方法将图像上传到twitter的解决方案,请指导我或发布一些源代码。您可以使用此代码在twitter上发布
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *twitterComposer = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
//add initial text
[twitterComposer setInitialText:@"Tweeting from iPhone 5 #TesingApp"];
//present composer
[self presentViewController:twitterComposer animated:YES completion:nil];
} else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"twitter Error"
message:@"You may not have set up twitter service on your device or\n You may not connected to internent.\nPlease check ..."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alertView show];
}
这将仅在iOS 6+中使用。谢谢回复。但是我的部署目标是3.0,所以我不想使用twitter和帐户框架。如果您知道我上面的代码有任何更改,我将接受它。twitter API的版本1已弃用;没有大量的工作,就没有简单的方法来完成你想要完成的事情。请看一个如何将旧应用程序与Twitter集成的示例。@Ash Furrow我几乎用MGTwitterEngine向Twitter发送了文本。现在我试着把图片上传到twitter上。请查看此链接“”并为我提供指南。请查看以下链接。可能会有帮助,这个也有演示