在javascript facebook api上发布图像事件

时间:2014-02-17 20:41:12

标签: javascript facebook facebook-graph-api

好的我被卡住了,我想在facebook上创建一个活动,使用javascript facebook api,我想在活动封面上加载图片。我无法弄清楚我是怎么做到的。

我在画布中创建了一个图像,我可以使用sendAsBinnary函数上传到Facebook 来自:https://stackoverflow.com/a/5303242/945521 而这个功能

Facebook.prototype.postImageToFacebook = function(authToken, filename, mimeType, imageData, message){
    try {
        showMessage("Creating post...");
        // this is the multipart/form-data boundary we'll use
        var boundary = '----------RaNdOm_crAPP' + getBoundary();
        // let's encode our image file, which is contained in the var
        var formData = '--' + boundary + '\r\n';
        formData += 'Content-Disposition: form-data; name="source"; filename="' + filename + '"\r\n';
        formData += 'Content-Type: ' + mimeType + '\r\n\r\n';
        for (var i = 0; i < imageData.length; ++i)
        {
            formData += String.fromCharCode(imageData[ i ] & 0xff);
        }
        formData += '\r\n';
        formData += '--' + boundary + '\r\n';
        formData += 'Content-Disposition: form-data; name="message"\r\n\r\n';
        formData += message + '\r\n';
        formData += '--' + boundary + '--\r\n';
        //var xhr = new XMLHttpRequest();
        var xhr = null;
        if (window.XDomainRequest) {
            xhr = new XDomainRequest();
        }
        else if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }
        else {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhr.onload = function() {
            window.alert("The photo was published successfully!!");
        };
        showMessage("Sending request...");
        xhr.open("POST", "https://graph.facebook.com/me/photos?access_token=" + authToken, true);
        if (xhr.setRequestHeader)
            xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
        xhr.sendAsBinary(formData);

    } catch (ex) {
        stopWaitingSpin();
        debugLog(ex);
    }

};

并使用此

 FB.api("/me/events", "POST", {
        "name": name,
        "start_time": start_time,
        //"end_time":end_time,
        "description": description,
        "location": location,
        //"location_id":location_id,
        "privacy_type": privacy_type
    },
    function(response) { 
      if (response && !response.error) {
         var event_id = response.id;
         console.log(response);
      }
    });

我可以创建一个活动。

但是需要调用什么来将图像发送到封面上的事件???

感谢所有人

1 个答案:

答案 0 :(得分:0)

实际上它尚未在文档中提及,但要发布封面照片,您需要使用参数\POST /{event-id}致电cover_url

您创建活动时可以获得

{event-id}

FB.api("/{event-id}", "POST", {
   cover_url: {image-url}
},
function(response) { 
   console.log(response);  // you'll get the response as 'true' or 'false'
});