Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

Powershell examples to upload to Google Drive?

Powershell examples to upload to Google Drive?

I tried the following code I found on a website, but it did not work.

Code
$refreshToken = "1//048b9QfYxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    $ClientID = "xxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"
    $ClientSecret = "iqbxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    $grantType = "refresh_token"
    $requestUri = "https://accounts.google.com/o/oauth2/token"
    $GAuthBody = "refresh_token=$refreshToken&client_id=$ClientID&client_secret=$ClientSecret&grant_type=$grantType"
    $GAuthResponse = Invoke-RestMethod -Method Post -Uri $requestUri -ContentType "application/x-www-form-urlencoded" -Body $GAuthBody
    $accessToken = $GAuthResponse.access_token

$GUploadURI  = "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart"
$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


$title = "My Document Title"
$description = "Stuff about file"
$parentfolder = "mefolder" #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 = "my text here"
#Request headers
$Guploadheaders = @{"Authorization" = "Bearer $accessToken"
                    "Content-type" = 'multipart/related; boundary="BOUNDARY"'
                    "Content-Length" = "$GBodySize"}
##The body must be formatted EXACTLY as below. Any extra linebreaks within the boundaries will cause Google to return a 400 error
$GUploadBody= @"
--BOUNDARY
Content-Type: application/json; charset=UTF-8

{
  "title": "$title",
  "parents": [{
    "id":"$parentfolder"
    }],
  "mimeType": "$inputmime",
  "description": "$description"


}
--BOUNDARY
Content-Type: text/plain

$togoogle
--BOUNDARY--

"@
## End HTTP Body

$uploadrequest = Invoke-WebRequest -method Post -uri $GUploadURI -body $GUploadBody -Headers $Guploadheaders
$uploadrequest
--


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by samir5000Mar 13, 2025 01:32 PM 3 324 Mar 13, 2025 01:32 PM
Last Post: Bastille
[Shell] by PELockDec 9, 2024 09:42 AM 3 657 Dec 9, 2024 09:42 AM
Last Post: PELock
[Source] by QismonNov 27, 2024 04:29 AM 2 307 Nov 27, 2024 04:29 AM
Last Post: mikeyp