0

我在我的应用程序中使用 twitter+OAuth 和 MGTwitterengine 并成功向 twitter 发表评论。但现在我的问题是使用

https://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 搜索上述任务。

4

1 回答 1

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);
    }
}];

希望此代码对您有用,对我有用,在此您可以仅发布图像和文本。祝你好运

于 2013-03-22T05:09:14.863 回答