【问题标题】:upload image to twitter by using "http://upload.twitter.com/1/statuses/update_with_media.json"使用“http://upload.twitter.com/1/statuses/update_with_media.json”将图像上传到 Twitter
【发布时间】:2013-03-22 23:05:10
【问题描述】:

谁能指导我或使用此 api“http://upload.twitter.com/1/statuses/update_with_media.json”生成一些源代码。这是一个 twitter api,用于将图像从 iphone 应用程序直接上传到 twitter。

这是我的代码。

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":"Internal error","code":131}]}

请告诉我为什么会出现此错误。我在上面的代码中错了。如果有人知道使用 udate_with_media 方法将图像上传到 Twitter 的解决方案,请指导我或发布一些源代码。

【问题讨论】:

标签: iphone objective-c


【解决方案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+ 中使用。

【讨论】:

  • 感谢您的回复。但我的部署目标是 3.0,所以我不想使用 twitter 和帐户框架。如果您知道我上面的代码有任何更改,我会接受它。
  • Twitter API 的第 1 版已弃用;如果没有大量的工作,就没有简单的方法来完成你想要完成的事情。请查看MGTwitterEngine,了解如何将旧版应用程序与 Twitter 集成的示例。
  • @Ash Furrow 我几乎完成了使用 MGTwitterEngine 将文本发送到 Twitter。现在我尝试将图像发布到 Twitter。请查看此链接“stackoverflow.com/questions/15614829/…”并给我指导。请帮忙
猜你喜欢
  • 2013-03-17
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2012-06-28
  • 2015-01-10
  • 1970-01-01
相关资源
最近更新 更多