Я использую PowerShell для загрузки PDF-файла с локального компьютера на диск Google с помощью API-интерфейса Google Drive.В настоящее время я использую адаптированную версию кода Montel Edwards '.Когда я запускаю свой код, он создает файл на диске Google, как и предполагалось, но вместо того, чтобы следовать инструкциям, указанным в $ GUploadBody, он создает документ без названия, содержимое которого является буквальным текстом этой переменной.Что я должен изменить, чтобы загрузить настоящий pdf.
Вот мой код, надеюсь, он правильно отформатирован, когда я загрузил его
$refresh_body = @{
client_id='redacted';
client_secret='redacted';
refresh_token='redacted';
grant_type="refresh_token";
}
$refresh_token = Invoke-RestMethod -Uri "https://www.googleapis.com/oauth2/v4/token" -Method POST -Body $refresh_body
$access_token = $refresh_token.access_token
$title = "Scan.pdf"
$description = "scan"
$parentfolder = "redacted" #folder ID where to save
$inputmime = "application/vnd.google-apps.document" #
https://developers.google.com/drive/v2/web/mime-types more on mime types here
$togoogle = "S:\Building_Permit_Tool\Working\0\*.pdf"
##The body must be formatted EXACTLY as below. Any extra linebreaks within
the boundaries will cause Google to return a 400 error
$GUploadBody = @"
Content-Type: application/json; charset=UTF-8
{
"name": "$title",
"parents": [{
"id":"$parentfolder"
}],
"mimeType": "$inputmime",
"description": "$description"
}
--BOUNDARY
Content-Type: application/pdf
$togoogle
--BOUNDARY--
"@
## End HTTP Body
$GBodySize = ($GUploadBody | Measure-Object -property length -sum).Sum #Calculates the size in bytes of the HTTP POST body, necessary for Content- Length in the request header
#Request headers
$Guploadheaders = @{"Authorization" = "Bearer $access_token"
"Content-type" = 'text/plain; boundary="BOUNDARY"'
"Content-Length" = "$GBodySize"
}
$GUploadURI = "https://www.googleapis.com/upload/drive/v3/files?uploadType=media"
$uploadrequest = Invoke-WebRequest -method POST -uri $GUploadURI -body $GUploadBody -Headers $Guploadheaders
$uploadrequest