简体   繁体   English

将多张照片发布到一个帖子

[英]Posting multiple Photos to one post

I have been trying to create an application which needs multiple photos to be attached to one post. 我一直在尝试创建一个需要将多张照片附加到一个帖子的应用程序。 These are the following attempts i tried, 这些是我尝试过的以下尝试,

First i used facebook-node-sdk which JS SDK to achieve different functionality, but Official Js Sdk does't have option for file to upload, when then i moved to attaching/inserting photo itself to HTTP POST with the help of form-data , with the following code- 首先,我使用facebook-node-sdk来实现JS SDK的不同功能,但是正式的Js Sdk没有上传文件的选项,然后我在form-data的帮助下将照片本身附加/插入到HTTP POST中。 ,使用以下代码-

    var form = new FormData();
    form.append('file', fs.createReadStream(picPaths[0]));
    form.append('message', "Hello"); //Put message
    var ACCESS_TOKEN = "ACCESS_TOKEN";
    var options = {
        method: 'post',
        host: 'graph.facebook.com',
        path:  '{Object-ID}/photos' + '?access_token=' + ACCESS_TOKEN,
        headers: form.getHeaders(),
    }

    var request = https.request(options, function(res) {
        console.log(res, false, null);
    });

    form.pipe(request);

    request.on('error', function(error) {
        console.log(error);
    });

This works with one photo. 只能使用一张照片。

But as you can see in this github.com/Thuzi/facebook-node-sdk/issues/113 which i started, it is not possible to attach more than one photo. 但是,正如您在我开始的github.com/Thuzi/facebook-node-sdk/issues/113中所看到的那样,不可能附加多张照片。

So as mentioned by dantman i stated looking in batch process, which can be found developers.facebook.com/docs/graph-api/making-multiple-requests titled under Uploading binary data . 因此,如dantman所提到的,我说要查找批处理过程,可以在标题为Uploading binary data的下找到developers.facebook.com/docs/graph-api/making-multiple-requests。 The one thing that hits and give me hope is this one statement. 打给我希望的一件事就是这一说法。

The attached_files property can take a comma separated list of attachment names in its value. attach_files属性的值可以采用逗号分隔的附件名称列表。

Note That (batching with photos) also is not possible with this library or JS SDK (Please correct me if i am wrong) 请注意 ,此库或JS SDK也无法(带照片批处理)(如果我输入错了,请纠正我)

You can do post images with curl like this, 您可以像这样卷曲地张贴图像,

curl -F 'access_token=ACCESS_TOKEN' -F 'batch=[{"method":"POST","relative_url":"{Object-Id}/photos","body":"message=Test Post","attached_files":"file1"}]' -F 'file1=@image1' -F 'file2=@image2' https://graph.facebook.com

The above code posts with one image 上面的代码发布了一张图片

So my question is this, it possible to attach multiple images/binary_files to the post with the help of curl, something like ..."attached_files":"file1,file2"... as suggested by docs, please help me with this problem and if you have already done this can you please post the snapshot of your code. 所以我的问题是,有可能在curl的帮助下将多个图像/ binary_files附加到帖子上,就像文档建议的那样..."attached_files":"file1,file2"... ,请帮助我问题,如果您已经完成此操作,则可以发布代码快照。

Thanks, Ravi 谢谢,拉维

I finally figured out how. 我终于想通了。

So first, read the section here titled "Publishing a multi-photo post with uploaded photos": https://developers.facebook.com/docs/graph-api/reference/page/photos/#Creating 因此,首先,请阅读此处标题为“发布包含上载照片的多张照片帖子”的部分: https : //developers.facebook.com/docs/graph-api/reference/page/photos/#Creating

What it says is basically correct, however, it is not in JavaScript. 它说的基本上是正确的,但是在JavaScript中却不是。 Also, they don't emphasize enough an important step: You have to set "published" to "false" for the image you upload, for it to then be attachable to the post that gets created. 此外,他们没有强调足够重要的步骤:您必须将上传的图像的“发布”设置为“假”,然后才能将其附加到创建的帖子中。

So anyway, here is the working code -- in JavaScript, and with "published" correctly set to false: 因此,无论如何,这是工作代码-用JavaScript,并且正确将“ published”设置为false:

async function PostImageToFacebook(token, filename, mimeType, imageDataBlob, message) {
    var fd = new FormData();
    fd.append("access_token", token);
    fd.append("source", imageDataBlob);
    //fd.append("message", "photo message for " + filename);
    fd.append("no_story", "true");
    //fd.append("privacy", "SELF");
    fd.append("published", "false");

    // Upload image to facebook without story(post to feed)
    let uploadPhotoResponse = await $.ajax({
         url: "https://graph.facebook.com/me/photos?access_token=" + token,
         type: "POST",
         data: fd,
         processData: false,
         contentType: false,
         cache: false
    });
    console.log(`Uploaded photo "${filename}": `, uploadPhotoResponse);

    let uploadPhotoResponse2 = await $.ajax({
        url: "https://graph.facebook.com/me/photos?access_token=" + token,
        type: "POST",
        data: fd,
        processData: false,
        contentType: false,
        cache: false
  });
  console.log(`Uploaded photo "${filename}": `, uploadPhotoResponse2);

    let makePostResponse = await $.ajax({
        "async": true,
        "crossDomain": true,
        "url": "https://graph.facebook.com/v2.11/me/feed",
        "method": "POST",
        "headers": {
            "cache-control": "no-cache",
            "content-type": "application/x-www-form-urlencoded"
        },
        "data": {
            "message": "Testing multi-photo post2!",
            "attached_media[0]": `{"media_fbid":${uploadPhotoResponse.id}}`,
            "attached_media[1]": `{"media_fbid":${uploadPhotoResponse2.id}}`,
            "access_token": token
        }
    });
    console.log(`Made post: `, makePostResponse);
}

The code above currently just uploads the same image twice, then attaches both to the new post. 上面的代码当前仅将同一张图片上传两次,然后将两者都附加到新帖子上。 Obviously, in real world usage you would replace the data in the second photo-upload with a different image. 显然,在实际使用中,您将用另一张图像替换第二张照片上传中的数据。

Anyway, to use the function, just call it like so: 无论如何,要使用该函数,只需像这样调用它:

function dataURItoBlob(dataURI) {
    var byteString = atob(dataURI.split(",")[1]);
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
         ia[i] = byteString.charCodeAt(i);
    }
    return new Blob([ab], {type: "image/png"});
}

let imageDataURI = GetImageDataURIFromSomewhere();
let imageBlob = dataURItoBlob(imageDataURI);

PostImageToFacebook(fbToken, "some_filename", "image/png", imageBlob, window.location.href);

this is possible. 这个有可能。

Note: This one is not an efficient way to do this but just for explaining purpose i am doing here, 注意:这不是执行此操作的有效方法,而只是为了解释我在此所做的目的,

The first hint that i got that it may be possible is from this post 我得到的第一个提示是可能来自这篇文章

Steps that i used: 我使用的步骤:

  1. Follow the doc to create custom open graph stories 按照文档创建自定义开放图故事
  2. Let's suppose you four image to attach (pic[1, 2, 3, 4]) 假设您要附加四个图像(pic [1、2、3、4])
  3. First i staged them with the help of new facebook-node-sdk v1.1.0-alpha1 with the code something like this (with batch process). 首先,我在新的facebook-node-sdk v1.1.0-alpha1的帮助下进行了上演,这些代码具有类似这样的代码(具有批处理功能)。

     FB.api( "", "post", { batch: [ { method: "POST", relative_url: "me/staging_resources", attached_files: "file1", type:"image/png" }, { method: "POST", relative_url: "me/staging_resources", attached_files: "file2", type:"image/png" }, { method: "POST", relative_url: "me/staging_resources", attached_files: "file3", type:"image/png" }, { method: "POST", relative_url: "me/staging_resources", attached_files: "file4", type:"image/png" }], file1: fs.createReadStream(picPaths[0]), file2: fs.createReadStream(picPaths[1]), file3: fs.createReadStream(picPaths[2]), file4: fs.createReadStream(picPaths[3]) }, function(response) { console.log(response); }); 
  4. Now from the response part get the url and dis the post with the same library . 现在,从响应部分获取url,并使用相同的发布该帖子。 With the code something like this. 用这样的代码。

     FB.api( "me/objects/{app-namespace}:{custom-object}", "post", { "object": { "og:title": "Sample Post", "og:image[0]": { "url": "fbstaging:{...}", "user_generated": true }, "og:image[1]": { "url": "fbstaging:{...}", "user_generated": true }, "og:image[2]": { "url": "fbstaging:{...}", "user_generated": true }, "og:image[3]": { "url": "fbstaging:{...}", "user_generated": true } } }, function(response) { console.log(response); } ); 

Now, with these two piece of code you will be able to push multiple images/photo to the single post. 现在,使用这两段代码,您就可以将多张图像/照片推送到单个帖子中。

Note: this can make more sense or can be done with the help of named batch process which is being described here . 注意:这可能更有意义,也可以在此处描述的命名批处理的帮助下完成。

Thanks, Ravi 谢谢,拉维

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM