简体   繁体   English

“失败:尝试创建此媒体项目时出错。”

[英]“Failed: There was an error while trying to create this media item.”

I'm trying to upload a photo on Google Photos, with google api, but i always get this error:我正在尝试使用 google api 在 Google Photos 上上传照片,但我总是收到此错误:

"status":{"code":3,"message":"Failed: There was an error while trying to create this media item."}}

I think that the problem is in the way i take the binary data of photo.我认为问题在于我获取照片二进制数据的方式。

In this code i follow google photo api developers documentation, and it says, after take authorization, to: 1- Upload the raw bytes to a Google Server: to take upload-token 2- Use the upload token to create the media item.在此代码中,我遵循谷歌照片 api 开发人员文档,它说,在获得授权后,要: 1- 将原始字节上传到 Google 服务器:获取上传令牌 2- 使用上传令牌创建媒体项目。

This my node js code:这是我的节点 js 代码:

const photo = fs.readFileSync("fbimages/"+req.session.id_client+"/Prague/2020-05-30T17:29:14+0000_0.png", {
            'encoding': 'binary',
            'flag' : 'r'
        });


 var url= 'https://photoslibrary.googleapis.com/v1/uploads';
 var headers= {
     'Authorization': 'Bearer '+token,
     'Content-type': 'application/octet-stream',
     'X-Goog-Upload-Content-Type': 'image/png',
     'X-Goog-Upload-Protocol': 'raw',
     'X-Goog-Upload-File-Name': "2020-05-30T17:29:14+0000_0.png",
 };
 var body= photo

 request({
      url: url,
      method:'POST',
      headers: headers,
      rejectUnauthorized: false,
      body: JSON.stringify(body)
  }, function(error, response, body1){
      if(error)
      {
        console.log(error);
      }
      else
      {
         var upToken = body1.toString();

         console.log(upToken);

         var url= 'https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate';
         var headers= {
             'Authorization' : 'Bearer '+token,
             'Content-type': 'application/json',
         };
         var body= {
             'newMediaItems': [
                 {
                   'description': 'Prague',
                   'simpleMediaItem': {
                         'fileName': 'prova',
                         'uploadToken': upToken,
                    }
                 }
             ]
         };
         request({
             url: url,
             method: 'POST',
             headers: headers,
             rejectUnauthorized: false,
             body: JSON.stringify(body),

           }, function(error, response, body){
              if(error)
              {
                 console.log(error);
               }
               else
               {
                  res.send(JSON.parse(body));
                }
          });
   }});

Please help me, if anyone has any idea!!请帮助我,如果有人有任何想法!

How about this modification?这个改装怎么样?

Modification points:修改点:

  • I think that encoding: "binary" is not required.我认为encoding: "binary"不是必需的。
    • In this case, ``const photo = fs.readFileSync("fig1.png", { flag: "r" });在这种情况下,`const photo = fs.readFileSync("fig1.png", { flag: "r" }); and const photo = fs.readFileSync("fig1.png");` can be used. and可以使用 const photo = fs.readFileSync("fig1.png");`。
  • Please modify body: JSON.stringify(body) to photo of const photo = fs.readFileSync("fig1.png", { flag: "r" });请将body: JSON.stringify(body)修改为 const photo const photo = fs.readFileSync("fig1.png", { flag: "r" }); . .

When above points are reflected to your script, it becomes as follows.当以上几点反映到您的脚本时,它变成如下。

Modified script:修改后的脚本:

From: 从:
 const photo = fs.readFileSync("fbimages/"+req.session.id_client+"/Prague/2020-05-30T17:29:14+0000_0.png", { 'encoding': 'binary', 'flag': 'r' }); var url= 'https://photoslibrary.googleapis.com/v1/uploads'; var headers= { 'Authorization': 'Bearer '+token, 'Content-type': 'application/octet-stream', 'X-Goog-Upload-Content-Type': 'image/png', 'X-Goog-Upload-Protocol': 'raw', 'X-Goog-Upload-File-Name': "2020-05-30T17:29:14+0000_0.png", }; var body= { 'media-binary-data': photo } request({ url: url, method:'POST', headers: headers, rejectUnauthorized: false, body: JSON.stringify(body) }, function(error, response, body1){
To: 至:
 const photo = fs.readFileSync("fbimages/"+req.session.id_client+"/Prague/2020-05-30T17:29:14+0000_0.png", { flag: "r" }); var url = "https://photoslibrary.googleapis.com/v1/uploads"; var headers = { Authorization: "Bearer " + token, "Content-type": "application/octet-stream", "X-Goog-Upload-Content-Type": "image/png", "X-Goog-Upload-Protocol": "raw", "X-Goog-Upload-File-Name": "2020-05-30T17:29:14+0000_0.png", }; request( { url: url, method: "POST", headers: headers, rejectUnauthorized: false, body: photo, }, function (error, response, body1) {

Note:笔记:

  • In this modification, it supposes that your token can be used for this API.在此修改中,假设您的token可用于此 API。 Please be careful this.请注意这一点。
  • When you want to directly put the uploaded image to an alubmn, please include albumId for requesting to https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate .当您想将上传的图片直接放入相册时,请在请求https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate albumId

Reference:参考:

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

相关问题 创建 FlatList 项。 但它不适用于本机反应 - create FlatList item. but it doesn't work on react native 您不能为此项目创建库存明细。 在套件脚本中 - You cannot create an inventory detail for this item. in suitescript 我试图让 ionic 2 应用程序的用户删除列表项。 使用 Angular 使用 Firebase 构建 - I am trying to let a user of an ionic 2 application delete a list item. Built using Firebase using Angular 尝试进行JSON调用时出现415(不支持的媒体类型)错误 - 415 (Unsupported Media Type) error while trying to make a JSON call Dojo - 尝试删除并添加项目到存储时出错(在ItemFileWriteStore中断言失败) - Dojo - error trying remove and add item to store (assertion failed in ItemFileWriteStore) 如何删除无法创建图表:与Ruby on Rails结合使用时,无法从给定项目错误获取上下文? - How to remove Failed to create chart: can't acquire context from the given item error while using it with Ruby on Rails? 尝试创建新元素时出错 - Error while trying to create new element 尝试放置新项目时出现 DynamoDB InvalidParameterType 错误 - DynamoDB InvalidParameterType Error While Trying To Put A New Item jQuery自动选择菜单项。 - Jquery Auto select Menu Item. 要突出显示鼠标悬停在相应项目上的图像。 - Want to highlight an image on mouseover on corresponding item.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM