0

我正在尝试通过使用新的 api statuses/update_with_media 在 Twitter 上共享图像。然后我从 dev.twitter.com/discussion 页面搜索并获得了以下代码。这里的代码如下。

在此处输入代码

- (NSString *) _uploadImage:(UIImage *)image requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType
{

    NSString *boundary = @"----------------------------991990ee82f7";

    NSURL *finalURL = [NSURL URLWithString:@"http://upload.twitter.com/1/statuses/update_with_media.json"];
    if (!finalURL) 
    {
        return nil;
    }

    NSLog(@"-> Open Connection: %@", finalURL);


    OAMutableURLRequest *theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL
                                                                      consumer:self.consumer
                                                                         token:_accessToken 
                                                                         realm: nil
                                                             signatureProvider:nil];

    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPShouldHandleCookies:NO];

    // Set headers for client information, for tracking purposes at Twitter.
    [theRequest setValue:_clientName    forHTTPHeaderField:@"X-Twitter-Client"];
    [theRequest setValue:_clientVersion forHTTPHeaderField:@"X-Twitter-Client-Version"];
    [theRequest setValue:_clientURL     forHTTPHeaderField:@"X-Twitter-Client-URL"];


    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [theRequest setValue:contentType forHTTPHeaderField:@"content-type"];

    NSMutableData *body = [NSMutableData dataWithLength:0];
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"media_data[]\"; filename=\"1.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"status\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\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];

    [theRequest setHTTPBody:body];

    // Create a connection using this request, with the default timeout and caching policy, 
    // and appropriate Twitter request and response types for parsing and error reporting.
    MGTwitterHTTPURLConnection *connection;
    connection = [[MGTwitterHTTPURLConnection alloc] initWithRequest:theRequest 
                                                            delegate:self 
                                                         requestType:requestType 
                                                        responseType:responseType];

    if (!connection) 
    {
        return nil;
    } 
    else 
    {
        [_connections setObject:connection forKey:[connection identifier]];
        //[connection release];
    }

    return [connection identifier];  
}

然后我在 SA_OAuthtwitterengine 中实现了这段代码,因为我使用 oauth 方法将文本或图像发布到 twitter,因为我的应用程序部署目标是从 3.0(ios,ipod)开始,所以我尝试使用这种方法。

现在我的问题是如何创建代码以通过消息将图像发送到 twitter。

我尝试使用以下类型将图像发送到 Twitter

[_engine sendupdate:@"http://www.prepare-community.com/ShareFiles/index-fr.jpg"];

我收到连接错误,这意味着 401 错误。

你能告诉我如何使用上面的代码将图像发送到 twitter 吗?

4

1 回答 1

0

这是使用新的 twitter api update_with_media 上传图片的解决方案。使用此 API,您可以将图像直接上传到 Twitter 帐户。上面的 _uploadimage 代码应该首先放在 SA_OAuthtwitter 引擎 .m 文件中,然后在 .h 文件中贴花。

并做一个小正确,即放置 filenamed=1.jpg 而不是 1.png。

现在只需将下面的代码放在 MGTwitterengine.m 类中。这里的代码如下

    - (NSString *)_uploadImage:(UIImage *)image withStatus:(NSString *)status
{
     return [self _uploadImage:image withStatus:status requestType:MGTwitterImageRequest responseType:MGTwitterStatuses];
}

然后我们使用此代码将图像上传到 Twitter。那是,

[_engine _uploadImage:yourpicture withStatus:yourstatus];

希望它对你有用....

于 2013-03-29T13:59:05.200 回答