谷歌照片API添加照片不起作用,上传似乎有效 [英] Google photos api adding photos not working, upload seems to work

查看:76
本文介绍了谷歌照片API添加照片不起作用,上传似乎有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Google Apps脚本和Google Photos API将照片添加到Google Photos中.上载似乎有效/返回了令牌,但随后将照片添加到库中失败.该过程包括两个步骤:1.如此处所述,上传照片数据a>,然后2.按照这里.

第1步对我有用,因为我获得了一个上传令牌,但是第2步(下面带有源代码)抛出错误,但是我的通话中有它需要的一个媒体项.

  {错误": {代码":400,"message":请求必须至少具有一个newMediaItem.",状态":"INVALID_ARGUMENT"}} 

我的代码在执行以下上传步骤之后.我试图对请求正文进行字符串化,并将其传递给有效内容而不是正文,但是没有任何效果.由于错误似乎足够具体,因此我觉得我只是忽略了一件小事,但是呢???谁有一段有效的代码,最好是在我可以看一下的应用脚本中?

  requestHeader = {"authorization":"Bearer" + photos.getAccessToken(),"Content-Type":"application/json"}var requestBody = {"newMediaItems":[{"description":照片描述","simpleMediaItem":{"fileName":fileName,"uploadToken":uploadToken}}]}var options = {"muteHttpExceptions":是的,"method":"post",标题":requestHeader,正文":requestBody};var response = UrlFetchApp.fetch("https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate",选项);Logger.log("raw:" +响应); 

解决方案

  • 您要使用带有Google Apps脚本的Photo API将图像文件添加到相册中.
  • 您已经在API控制台上启用了Google Photo API.您的访问令牌可用于使用mediaItems.batchCreate的方法.

如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

用法:

1.将Cloud Platform专案连结至Google Apps Script专案:

关于此内容,您可以在此处看到详细信息流.

2.添加范围:

在这种情况下,请将 https://www.googleapis.com/auth/photoslibrary 的范围添加到清单文件( appsscript.json )./p>

尽管我认为从您的问题来看,第1步和第2步已经完成,但我添加了它们,因为我认为这可能对其他用户有用.

3.示例脚本:

在您的脚本中,我看不到 uploadToken 的详细信息.但是在您的问题中,我可以确认您已经检索到 uploadToken 的值.因此,当您要使用脚本来检索 uploadToken 时,请将 uploadToken 替换为您的脚本.作为脚本的修改点,1.包括唱片集ID.2. UrlFetchApp没有 body 属性.3.请对有效负载使用 JSON.stringify().

  function getUplaodToken_(imagefileId){var标头= {授权":承载者" + ScriptApp.getOAuthToken(),"X-Goog-Upload-File-Name":"sampleFilename","X-Goog-Upload-Protocol":"raw",};var options = {方法:发布",标头:标头,contentType:应用程序/八位字节流",有效负载:DriveApp.getFileById(imagefileId).getBlob()};var res = UrlFetchApp.fetch("https://photoslibrary.googleapis.com/v1/uploads",选项);返回res.getContentText()}//请运行此程序.函数myFunction(){var imagefileId ="###";//请设置图像文件的文件ID.var albumId ="###";//请设置相册ID.var uploadToken = getUplaodToken_(imagefileId);var requestHeader = {Authorization:"Bearer" + ScriptApp.getOAuthToken()};var requestBody = {"albumId":专辑ID,"newMediaItems":[{"description":照片描述","simpleMediaItem":{"fileName":"sampleName","uploadToken":uploadToken}}]};var options = {"muteHttpExceptions":是的,"method":"post",标题":requestHeader,"contentType":"application/json",有效载荷":JSON.stringify(requestBody)};var response = UrlFetchApp.fetch("https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate",选项);Logger.log(响应);} 

  • 在此脚本中,它假定图像文件已放置在Google云端硬盘中.

注意:

  • 如果出现错误没有向此相册添加媒体项目的权限.,请通过脚本创建相册.官方文件说明如下.

    只能在您的应用创建的相册中创建媒体项目.

    • 在这种情况下,请通过以下脚本创建新相册,然后获取相册ID.

        function createNewAlbum(){var options = {标头:{Authorization:"Bearer" + ScriptApp.getOAuthToken()},有效负载:JSON.stringify({相册:{title:样本标题"}}),contentType:"application/json",方法:发布"};var res = UrlFetchApp.fetch("https://photoslibrary.googleapis.com/v1/albums",选项);Logger.log(res);} 

参考文献:

如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

Trying to use Google Apps Script and Google Photos API to add photos to Google Photos. Upload seems to work / returns a token, but then adding the photo to the library fails. The process consists of two steps: 1. Upload the photo data as described here, then 2. Add the photo to photo library as described here.

Step 1. works for me, as I get an upload token, but step 2 with source code below, throws an error, but my call has the one media item it needs.

{
  "error": {
    "code": 400,
    "message": "Request must have at least one newMediaItem.",
    "status": "INVALID_ARGUMENT"
  }
}

My code after the upload step below. I have tried to stringify request body and have passed it to payload instead of body, but nothing worked. As the error seems specific enough, I've the feeling I'm just overlooking a tiny thing, but what??? Who has a working piece of code, preferably in apps script that I can have a look at?

    requestHeader = {
      "authorization": "Bearer " + photos.getAccessToken(),
      "Content-Type": "application/json"
    }

    var requestBody = {
      "newMediaItems": [
        {
          "description": "Photo description",
          "simpleMediaItem": {
            "fileName": fileName,
            "uploadToken": uploadToken
          }
        }
      ]
    }

    var options = {
      "muteHttpExceptions": true,
      "method" : "post",
      "headers": requestHeader,
      "body" : requestBody
    };


      var response = UrlFetchApp.fetch("https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", options);

      Logger.log("raw: " + response);

解决方案

  • You want to add an image file to the album using Photo API with Google Apps Script.
  • You have already enabled Google Photo API at API console. And yout access token can be used for using the method of mediaItems.batchCreate.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Usage:

1. Linking Cloud Platform Project to Google Apps Script Project:

About this, you can see the detail flow at here.

2. Add scope:

In this case, please addt the scope of https://www.googleapis.com/auth/photoslibrary to the manifest file (appsscript.json).

Although I think that from your question, above step 1 and 2 have already been done, I added them because I thought that this might be useful for other users.

3. Sample script:

In your script, I cannot see the detail of uploadToken. But in your question, I could confirm that you have alredy retrieved the value of uploadToken. So when you want to use your script for retrieving uploadToken, please replace uploadToken to yours. As the modification point of your script, 1. Include the album ID. 2. There is no body property of UrlFetchApp. 3. Please use JSON.stringify() to the payload.

function getUplaodToken_(imagefileId) {
  var headers = {
    "Authorization": "Bearer " + ScriptApp.getOAuthToken(),
    "X-Goog-Upload-File-Name": "sampleFilename",
    "X-Goog-Upload-Protocol": "raw",
  };
  var options = {
    method: "post",
    headers: headers,
    contentType: "application/octet-stream",
    payload: DriveApp.getFileById(imagefileId).getBlob()
  };
  var res = UrlFetchApp.fetch("https://photoslibrary.googleapis.com/v1/uploads", options);
  return res.getContentText()
}

// Please run this.
function myFunction() {
  var imagefileId = "###";  // Please set the file ID of the image file.
  var albumId = "###";  // Please set the album ID.
  var uploadToken = getUplaodToken_(imagefileId);

  var requestHeader = {Authorization: "Bearer " + ScriptApp.getOAuthToken()};
  var requestBody = {
    "albumId": albumId,
    "newMediaItems": [{
      "description": "Photo description",
      "simpleMediaItem": {
      "fileName": "sampleName",
      "uploadToken": uploadToken
    }}]
  };
  var options = {
    "muteHttpExceptions": true,
    "method" : "post",
    "headers": requestHeader,
    "contentType": "application/json",
    "payload" : JSON.stringify(requestBody)
  };
  var response = UrlFetchApp.fetch("https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", options);
  Logger.log(response);
}

  • In this script, it supposes that the image file is put in Google Drive.

Note:

  • If the error of No permission to add media items to this album. occurs, please create the album by the script. The official document says as follows.

    Media items can be created only within the albums created by your app.

    • In this case, please create new album by the following script, and please retrieve the album ID.

      function createNewAlbum() {
        var options = {
          headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
          payload: JSON.stringify({album: {title: "sample title"}}),
          contentType: "application/json",
          method: "post"
        };
        var res = UrlFetchApp.fetch("https://photoslibrary.googleapis.com/v1/albums", options);
        Logger.log(res);
      }
      

References:

If I misunderstood your question and this was not the direction you want, I apologize.

这篇关于谷歌照片API添加照片不起作用,上传似乎有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆