Home > Mobile >  How to upload a picture to Google Photos using Powershell
How to upload a picture to Google Photos using Powershell

Time:12-25

there are basically no Powershell code samples over the Internet concerning how to upload photos to Google Photos.

According to the documentation, uploading media items is a two-step process:

  1. Upload the raw bytes to a Google Server. This doesn't result in any media items being created in the user’s Google Photos account. Instead, it returns an upload token which identifies the uploaded bytes.

  2. Use the upload token to create the media item in the user's Google Photos account. You can choose whether the media should be also added to a specific album. For more information, see Create albums.

As for point #1 , uploading RAW bytes is a bit tricky, as there are no Powershell samples!

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.

How do you correctly set the body, which should contain the binary data of the image being uploaded?

For instance

### 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()
}

CodePudding user response:

In your situation, how about the following modification?

From:

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

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

To:

$body = "SampleFilenameWithPath"

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