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 に実装しました。これは、アプリケーションの展開ターゲットが 3.0(ios,ipod) から開始されているため、テキストまたは画像を Twitter に投稿するために oauth メソッドを使用しているためです。

今私の問題は、メッセージで画像を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 ファイルにデカールする必要があります。

そして、1.png の代わりに filenamed=1.jpg が配置されている場所を少し修正します。

そして、以下のコードを 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 に答える