2013-03-22 71 views
0

将图像上传到Twitter。任何人都可以引导我或使用此api“http://upload.twitter.com/1/statuses/update_with_media.json”生成一些源代码。这是一个直接从iphone应用程序上传图片到Twitter的Twitter API。通过使用“http://upload.twitter.com/1/statuses/update_with_media.json”

在这里我的代码。

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的输出为<>空标记。

而且responstring为{ “错误”:[{ “消息”: “内部错误”, “代码”:131}]}

能告诉我为什么我得到这个错误。我在上面的代码中错了。如果有人知道使用udate_with_media方法将图片上传到twitter的解决方案,请指导我或发布一些源代码。

+0

**检查下面的链接。可以帮助** http://stackoverflow.com/questions/5584820/upload-submit-images-to-twitter-myspace-from-iphone https://github.com/Gurpartap/GSTwitPicEngine **这个人也有演示** https://dev.twitter.com/docs/ios/posting-images-using-twrequest – pdubal 2013-05-18 06:35:22

回答

1

您可以使用此代码在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+中使用。

+0

感谢您的回复。但我的部署目标是3.0,所以我不想使用twitter和帐户框架work.if你知道任何更改我的上面的代码,我会接受它杰伊。 – raman 2013-03-22 11:40:22

+0

Twitter API的版本1已弃用;没有大量的工作就没有简单的方法来完成你想要完成的任务。查看[MGTwitterEngine](https://github.com/mattgemmell/MGTwitterEngine),了解如何将旧应用程序与Twitter集成的示例。 – 2013-03-22 15:06:31

+0

@Ash Furrow我几乎完成使用MGTwitterEngine发送文本到Twitter。现在我尝试将图像发布到Twitter。请参阅此链接“http://stackoverflow.com/questions/15614829/upload-image-to-twitter-in-iphone”,并给我指导给我。请帮忙 – raman 2013-03-25 13:22:39