Hack Forums
[Shell] Powershell examples to upload to Google Drive? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Shell] Powershell examples to upload to Google Drive? (/showthread.php?tid=6055052)



Powershell examples to upload to Google Drive? - gurlhackerclub - Mar 6, 2020

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
--