简体   繁体   English

Facebook Graph API使用Javascript发布图片

[英]Facebook Graph API publish picture with Javascript

I'm using the Facebook Javascript SDK in my react app and I want to make a post with multiple photos. 我在我的react应用程序中使用Facebook Javascript SDK,并且我想发表多张照片。 I know that that means I have to first post the pictures unpublished and then use the returned ids to make the post. 我知道那意味着我必须先发布未发布的图片,然后使用返回的ID进行发布。

However, I'm having trouble finding good documentation and running into issues figuring out how to allow a user select and post a local picture (not from a url). 但是,我在寻找好的文档时遇到了麻烦,并且在解决如何允许用户选择和发布本地图片(而不是从URL)中遇到麻烦。 The code is a bit difficult to put all here, but here are the steps I'm taking and the errors I'm getting: 将代码放在这里有点困难,但是这里是我要采取的步骤以及我遇到的错误:

  1. Get file the user selected using a file input. 使用文件输入获取用户选择的文件。

  2. Encode the picture as a blob and put that and the access token into FormData to use in the api request. 将图片编码为Blob,然后将其和访问令牌放入FormData中以在api请求中使用。

     var reader = new FileReader(); reader.onload = function(e) { var arrayBuffer = e.target.result; var blob = new Blob([arrayBuffer], { type: photo.type }); var pictureData = new FormData(); pictureData.append('access_token:', this.state.FBaccessToken); pictureData.append('source', blob); return pictureData; }.bind(this) return reader.readAsArrayBuffer(photo); 
  3. Do a post request 进行发布请求

     var encodedRequest = this.encodePhoto(photo); FB.api( "/me/photos?published=false", "POST", encodedRequest, function (response) { if (response && !response.error) { //once successfully gotten the photos add them to the array of photo ids temp.push({"media_fbid": response.id}); console.log(response); } else { alert(response.error.message); } }.bind(this) ); 

The error when I run it this way is that it doesn't seem to recognize the access token, but when I remove the access token from pictureData in step 2, and change the api encodedRequest part to this: 我以这种方式运行它时的错误是它似乎无法识别访问令牌,但是当我在步骤2中从pictureData中删除访问令牌并将api encodeRequest部分更改为此时:

{
    access_token: this.state.FBaccessToken,
    source: encodedRequest,
},

I get the error "(#324) Requires upload file" . 我收到错误“(#324)需要上传文件” I tried adding fileUpload: true, to the SDK init code but that also didn't seem to do anything. 我尝试将fileUpload: true,添加到SDK初始化代码中,但这似乎也无能为力。 Posting simple text only statuses and reading from feed is all working fine. 发布简单的纯文本状态并从提要中读取一切正常。

Sorry for the long post, but I'd be really grateful if anyone has any insight! 抱歉,很长的帖子,但是如果有人有任何见解,我将不胜感激! Thanks. 谢谢。

Is it the extra colon you have after access_token in your first pictureData.append() call? 它是您在第一个pictureData.append()调用中的access_token之后的多余冒号吗?

pictureData.append('access_token:', this.state.FBaccessToken); pictureData.append('access_token:',this.state.FBaccessToken); versus pictureData.append('access_token', this.state.FBaccessToken); 与pictureData.append('access_token',this.state.FBaccessToken);

Edit: Below is output from postman to post images referencing a file on my laptop 编辑:以下是邮递员的输出,用于张贴引用笔记本电脑上文件的图像

var form = new FormData();
form.append("source", "/Users/patricklambe/images/test.jpg");
form.append("access_token", "PAGEACCESSTOKEN");
form.append("caption", "check this photo");

var settings = {
"async": true,
"crossDomain": true,
"url": "https://graph.facebook.com/v2.11/444873272561515/photos",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"postman-token": "1d786fec-c9b1-2494-1b5c-8fd0e2ea5ade"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
  }

 $.ajax(settings).done(function (response) {
  console.log(response);
});

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

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