如何使用 Powershell 将图片上传到 Google Photos

How to upload a picture to Google Photos using Powershell

Internet 上基本上没有关于如何将照片上传到 Google 照片的 Powershell 代码示例。

根据 documentation,上传媒体项目分为两步:

  1. 将原始字节上传到 Google 服务器。这不会导致在用户的 Google 照片帐户中创建任何媒体项目。相反,它 returns 一个标识上传字节的上传令牌。

  2. 使用上传令牌在用户的 Google 照片帐户中创建媒体项目。您可以选择媒体是否也应添加到特定相册。有关详细信息,请参阅创建相册。

至于第 1 点,上传 RAW 字节有点棘手,因为没有 Powershell 样本!

Bytes are uploaded to Google using upload requests. If the upload request is successful, an upload token which is in the form of a raw text string, is returned. To create media items, use these tokens in the batchCreate call.

如何正确设置正文,其中应包含正在上传的图片的二进制数据?

例如

### token auth code intentionally removed ###

$method = "POST"
$requestUri = "https://photoslibrary.googleapis.com/v1/uploads"
$Headers = @{
   Authorization = "Bearer $access_token";
   ContentType = "application/octet-stream";
   "X-Goog-Upload-Content-Type" = "mime-type";
   "X-Goog-Upload-Protocol" = "raw";
}

$body = ?????? <<<<<<<<<<< What here?

try {
   $Response = Invoke-RestMethod -Headers $Headers -Body $body -Uri $requestUri -Method $method
}
catch {
   $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
   $ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
   $streamReader.Close()
}

你的情况,下面的修改怎么样?

发件人:

$body = ?????? <<<<<<<<<<< What here?

try {
   $Response = Invoke-RestMethod -Headers $Headers -Body $body -Uri $requestUri -Method $method
}

收件人:

$body = "SampleFilenameWithPath"

try {
   $Response = Invoke-RestMethod -Headers $Headers -InFile $body -Uri $requestUri -Method $method
}