facebook照片通过图像数据上传

时间:2014-08-12 10:06:43

标签: facebook actionscript-3 facebook-graph-api

我使用的是图api v2.1。 AS3代码生成数据,通过ExternalInterface调用JS代码,JS代码调用FB API

我可以通过{image-url}上传照片,但在尝试通过{image-data}上传照片时收到错误:

  

{message:"(#324)需要上传文件",输入:" OAuthException",代码:   324}

我猜,我格式化{image-data}错误。这是我的代码:

AS3:

var id:int = Math.random()*10000;

var stream:ByteArray = new ByteArray();
var imageName:String = id.toString() + ".png";
var boundary:String = "I" + id.toString();
stream.writeUTFBytes(
            'Content-Type: multipart/form-data; boundary=' + boundary +

            '\r\n\r\n--' + boundary +
            '\r\nContent-Disposition: file; filename="' + imageName + '"' +
            '\r\nContent-Type: image/png' +
            '\r\nContent-Transfer-Encoding: binary' +
            '\r\n\r\n');
stream.writeBytes(picture);
stream.writeUTFBytes("\r\n--" + boundary + '--\r\n');

if (ExternalInterface.available)
{
    ExternalInterface.call('savePhoto', stream, null, id);
}

JS:

function savePhoto(bytes, url, requestId)
{
    var data;
    if (bytes != null)
        data = {"source": bytes, "no_story":true}; //getting error
    else
        data = {"url": url, "no_story":true}; //works fine

    FB.api(
        "/me/photos",
        "POST",
        data,
        function (response)
        {
            //handle response
        }
    );
}

UPD:

以下是picture初始化的方式:

[Embed(source="../res/logo_2.png", mimeType="image/png")]
private var testImage:Class;

<...>

var data:Bitmap = new testImage() as Bitmap;
var picture:ByteArray = PNGEncoder.encode(data.bitmapData);

2 个答案:

答案 0 :(得分:0)

您尝试过base64吗?

var params: Object = new Object;
var encoder:JPGEncoder = new JPGEncoder(75);
var bytes:ByteArray = encoder.encode(bmp.bitmapData);
params.message = message;
params.image = bytes;
params.fileName = "image.jpg";
Facebook.api("/me/photos",null,params,"POST");

https://code.google.com/p/as3corelib/

答案 1 :(得分:0)

尝试使用此代码准备数据:

        var pngData:ByteArray = PNGEncoder.encode(image.bitmapData);
        var strForStart:String = "\r\n--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"photo\"; filename=\"file1.png\"\r\n" + "Content-Type: image/png\r\n\r\n" + "";
        var strForEnd:String = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"Upload\"\r\n\r\n" + "Submit Query\r\n" + "--" + boundary + "--";

        var beginBA:ByteArray = new ByteArray();
        beginBA.writeMultiByte(strForStart, "ascii");
        var endBA:ByteArray  = new ByteArray();
        endBA.writeMultiByte(strForEnd, "ascii");

        var resultBytes:ByteArray = new ByteArray();
        resultBytes.writeBytes(beginBA, 0, beginBA.length);
        resultBytes.writeBytes(pngData, 0, pngData.length);
        resultBytes.writeBytes(endBA, 0, endBA.length);