web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / uploading file on goog...
Power Automate
Answered

uploading file on google drive using api - Power shell script PAD

(0) ShareShare
ReportReport
Posted on by 357

Hi Team,

 

I am trying to upload file on google drive using api Powershell script.

file get added on google drive but file name is not comming its come Untitled,

 

also not able to view the upladed file 

 

what is an issue 

Here is my script

# Web API endpoint URL
$apiUrl = %Google%

# Input file path
$inputFilePath = "C:\Users\sanket.shinde\Downloads\Krishna.png"


# API token
$apiToken = "provided"

$fileName = "Demo"

try {
    # Create a WebClient object
    $webClient = New-Object System.Net.WebClient

    # Set the Authorization header with the API token
    $webClient.Headers.Add("Authorization", "Bearer $apiToken")

    # Upload the file using multipart/form-data
    $responseBytes = $webClient.UploadFile($apiUrl, "POST", $inputFilePath)

    Write-Output "file saved successfully."
}
catch {
    Write-Output "An error occurred: $_"
}

I have the same question (0)
  • Verified answer
    kinuasa Profile Picture
    795 Most Valuable Professional on at

    The code in the link below may be a useful reference for how to upload files using the Google Drive API in PowerShell.

     

    *GDrive Upload PowerShell Script - GitHub

    https://gist.github.com/ConnorGriffin/dc804357bb10ff7522d0e21ddfdf9398

     

    Alternatively, if you are using the API Client Library for .NET, the code might look something like the one below.

     

    Add-Type -AssemblyName "System.Web"
    [void][Reflection.Assembly]::LoadFile("C:\System\Lib\Newtonsoft.Json.dll")
    [void][Reflection.Assembly]::LoadFile("C:\System\Lib\Google.Apis.dll")
    [void][Reflection.Assembly]::LoadFile("C:\System\Lib\Google.Apis.Core.dll")
    [void][Reflection.Assembly]::LoadFile("C:\System\Lib\Google.Apis.Auth.dll")
    [void][Reflection.Assembly]::LoadFile("C:\System\Lib\Google.Apis.Drive.v3.dll")
    
    $clientId = "(Client ID)"
    $clientSecret = "(Client Secret)"
    $credentialPath = "C:\wk\GoogleAPI\client_secrets.json"
    $uploadFilePath = "C:\Test\SamplePDF.pdf"
    #[String[]]$parents = @("1*_**********_*****************") #Folder ID
    [String[]]$parents = @("root")
    
    #Get AccessToken
    $clientSecrets = [Google.Apis.Auth.OAuth2.ClientSecrets]::new()
    $clientSecrets.ClientId = $clientId
    $clientSecrets.ClientSecret = $clientSecret
    [String[]]$scopes = @("https://www.googleapis.com/auth/drive")
    $credential = [Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker]::AuthorizeAsync(
     $clientSecrets, 
     $scopes, 
     "user", 
     [System.Threading.CancellationToken]::None, 
     [Google.Apis.Util.Store.FileDataStore]::new($credentialPath, $true)).Result
    
    #Upload file to Google Drive
    $service = [Google.Apis.Drive.v3.DriveService]::new()
    $uploadFileName = [System.IO.Path]::GetFileName($uploadFilePath)
    $mimeType = [System.Web.MimeMapping]::GetMimeMapping($uploadFilePath)
    $stream = [System.IO.FileStream]::new($uploadFilePath, [System.IO.FileMode]::Open)
    $body = [Google.Apis.Drive.v3.Data.File]::new()
    $body.Name = $uploadFileName
    $body.MimeType = $mimeType
    $body.Parents = $parents
    $request = $service.Files.Create($body, $stream, $mimeType)
    $request.AccessToken = $credential.Token.AccessToken
    $request.Upload()
    $stream.Close()

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 519 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 296 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard