处理Twitter API 1.1 update_with_media ActionScript 3

时间:2013-09-10 07:30:22

标签: actionscript-3 flash twitter twitter-oauth

我正在使用as3作为我当前的项目,登录,发布状态到twitter已经完成..但我有update_with_media的问题。这是我的程序流程。在其他屏幕上称之为CaptureWebcam,用户可以从网络摄像头捕获图片,但无法将其保存到用户计算机。如果用户打开共享到Twitter屏幕,有弹出窗口登录和CaptureWebcam屏幕上的图像传递到Twitter屏幕,并准备上传,它无法发布...我不知道我的代码是什么错误

这是我的代码,我正在使用iotashan库

private function uploadImage(Image()):void {
            var myEncoder:JPGEncoder = new JPGEncoder(80);
            var urlLoader:URLLoader = new URLLoader();
            var urlRequest:URLRequest;
            var multipartLoader:MultipartURLLoader = new MultipartURLLoader();
            var urlHeader:URLRequestHeader = new URLRequestHeader();

            var oAuth:OAuthRequest;
            var consumer:OAuthConsumer;
            var twitterRequestUrl:String;
            var headerValue:String = "";
            var auth_header:URLRequestHeader;
            var param:Object;
            var vid:Video;

            //var bmp:Bitmap = new Bitmap();
            var bitmapData:BitmapData = new BitmapData(CaptureWebcam.vid.width, CaptureWebcam.vid.height, true, 0xFFFFFF);
            //var byteArray:ByteArray = myEncoder.encode(new Bitmap(new Turtle()).bitmapData);
            var byteArray:ByteArray = myEncoder.encode(bitmapData);

            var drawRect:Rectangle = new Rectangle(0,0,image.width, image.height);
            bitmapData.draw(image, new Matrix(), null, null, drawRect, true);

            trace("upload test");
            //header create
            twitterRequestUrl = "https://api.twitter.com/1.1/statuses/update_with_media.json";
            consumer = new OAuthConsumer(consumerKey, consumerSecret);
            oAuth = new OAuthRequest(OAuthRequest.HTTP_MEHTOD_POST, twitterRequestUrl, null, consumer, null);
            urlRequest = new URLRequest(oAuth.buildRequest(new OAuthSignatureMethod_HMAC_SHA1()));
            //

            /*
            urlRequest = twitterRequestUrl;
            urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
            urlRequest.method = URLRequestMethod.POST;
            urlRequest.data = UploadPostHelper.getPostData(file, byteArray);
            urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );

            urlLoader.dataFormat = URLLoaderDataFormat.BINARY
            urlLoader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, onStatusDelivered);
            urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatusChanged);
            urlLoader.addEventListener(Event.COMPLETE, handleUploadComplete);
            urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
            urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
            urlLoader.load(urlRequest);
            */


            /*
            multipartLoader.addEventListener(Event.COMPLETE, handleUploadComplete);
            multipartLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
            multipartLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSequrityError);
            multipartLoader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, onStatusDelivered);
            */

            /*
            headerValue = createHeaderValue(oAuth);
            auth_header = new URLRequestHeader("Authorization", headerValue);
            multipartLoader.requestHeaders.push(auth_header);
            multipartLoader.addVariable("status" , "test");
            multipartLoader.addFile(byteArray, 'image.jpg', 'media[]');
            multipartLoader.load("https://api.twitter.com/1.1/statuses/update_with_media.json");
            */


            var signedData:String = oauth.getSignedRequest( URLRequestMethod.POST, "https://upload.twitter.com/1/statuses/update_with_media.json", null );
            var authHeaderValue:String = createAuthorizationHeader( new URLVariables( signedData ) );

            // create multipart loader

            var multipartLoader : MultipartURLLoader = new MultipartURLLoader();
            multipartLoader.addEventListener( Event.COMPLETE, handleUploadComplete );   
            multipartLoader.addEventListener( IOErrorEvent.IO_ERROR, onError );
            multipartLoader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onError ); 

            // add headers

            var auth_header : URLRequestHeader = new URLRequestHeader( "Authorization", authHeaderValue );
            multipartLoader.requestHeaders.push( auth_header );

            // add requeried data

            multipartLoader.addVariable( "status" , twitter_msg );
            multipartLoader.addFile( byteArray, 'image.jpg', 'media[]');    

            // load

            multipartLoader.load( "https://upload.twitter.com/1/statuses/update_with_media.json" );
}

private function createAuthorizationHeader( _requestParams : Object, headerRealm : String = "" ) : String
{
var data:String = "";

    data += "OAuth "
    if ( headerRealm.length > 0)
    data += "realm=\"" + headerRealm + "\", ";

    for (var param : Object in _requestParams ) {
      // if this is an oauth param, include it
        if ( param.toString().indexOf("oauth") == 0){
        data += param + "=\"" + encodeURIComponent( _requestParams[param]) + "\", ";
        }
    }

    return data.substr( 0, data.length - 2);
}

0 个答案:

没有答案