任何人都可以指导我或使用此 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 的解决方案,请指导我或发布一些源代码。