我正在使用以下代碼使用 Powershell 將照片和視頻上傳到 Google 相冊。該代碼非常適合圖片,但在上傳視頻時會失敗(當通過瀏覽器上傳視頻時,Google 相冊會接受該視頻)。
嘗試上傳視頻時,原始位元組上傳會成功,但mediaItems.batchCreate會失敗并顯示 status.code3和 status.message"Failed: There was an error while trying to create this media item."
這可能是由于標題中的 mime 型別錯誤嗎?
Function GP-UploadMedia (){
<#
.SYNOPSIS
Uploads a Google Photos media (picture/video)
.Description
Parameters:
- album id
- media path
Returns:
- picture id
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$album_id,
[Parameter(Mandatory=$true)]
[string]$media_path
)
# refresh tokens if required
G-RefreshTokens
# 1st step: upload raw bytes
$requestUri = "https://photoslibrary.googleapis.com/v1/uploads"
$mime_type = [System.Web.MimeMapping]::GetMimeMapping($media_path);
$Headers = @{
Authorization = "Bearer $($Global:Tokens.access_token)";
ContentType = "application/octet-stream";
"X-Goog-Upload-Content-Type" = $mime_type;
"X-Goog-Upload-Protocol" = "raw";
}
$body = $media_path;
try {
$upload_token = Invoke-RestMethod -Headers $Headers -InFile $body -Uri $requestUri -Method POST
}
catch {
# Something went wrong. Investigate!
dieOnError ([System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream()));
}
# refresh tokens if required
G-RefreshTokens
# 2nd step: pair raw bytes to Google Photo media (picture/video)
$requestUri = "https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate"
$Headers = @{
Authorization = "Bearer $($Global:Tokens.access_token)";
}
$body = [PSCustomObject]@{
albumId = $album_id;
newMediaItems = @(
[PSCustomObject]@{
description = "";
simpleMediaItem = [PSCustomObject]@{
fileName = (Split-Path $media_path -leaf);
uploadToken = $upload_token;
}
}
)
}
$myJson = $body|ConvertTo-Json -Depth 4
try {
$Response = Invoke-RestMethod -Headers $Headers -Body ($myJson) -Uri $requestUri -Method POST -ContentType "application/json; charset=utf-8";
}
catch {
# Something went wrong. Investigate!
dieOnError ([System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream()));
}
return $Response.newMediaItemResults.MediaItem.id;
}
uj5u.com熱心網友回復:
雖然我不確定這是否是您問題的直接解決方案,但是當您的腳本進行如下修改時,這是否是您問題的直接解決方案?在這種情況下,請修改“第一步:上傳原始位元組”的請求頭如下。
從:
ContentType = "application/octet-stream";
到:
"Content-Type" = "application/octet-stream";
筆記:
- 當我測驗你的腳本時,我可以和你復制同樣的情況。而且,當我通過上述修改修改您的腳本時,我可以確認問題可以解決。所以,請測驗上面的修改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/394611.html
