0

このメソッドを使用して画像を twitter にアップロードする方法

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

リンクがここにあるstackoverflow.comからこのメソッドを取得します。

iOS で Twitter の statuses/update_with_media を実行すると 500 エラーが返されます

上記のリンクでは、Ahmed、olivarseF、Gypsa、そしてもう 1 人が Tk189 という 4 人の素晴らしいメンバーの仕事です。

ここで、コードは以下のとおりです。

ここにコードを入力してください

- (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 クラスとこの行に統合されていましたが、

[body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];

エラーレポートが表示されたので、NSData + Base64ファイルをインポートしましたが、エラーは明らかでした。

次に、この行を使用して画像を送信しようとします [_engine sendupdate:@" http://www.xxxx.com/ccc.jpg "];

ここで画像をURLとして送信したいのですが、ここでこのエラーが発生しました"エラードメイン= HTTPコード= 401 "操作を完了できませんでした. (HTTP エラー 401)」

なぜ?この回答を知っている場合は、これを送信する方法を教えてください。特に、ミスター (ahmed、oliversf、gypsa、tk189) にお願いします。saouthtwitter エンジン クラスでの使用方法を説明してください。

4

1 に答える 1

0

特定のフレームワークを使用していますか? アプリケーションがアプリのサポート >= iOS 5 をサポートしている場合、iOS SDK に既に統合されているソーシャル フレームワークを次のように使用できます。

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
    NSString *title = [self.elementInfo objectForKey:@"title"];
    [tweetSheet setInitialText:@"message you want to display"];
    [self presentViewController:tweetSheet animated:YES completion:nil];
}
于 2013-03-25T13:33:08.953 に答える