frank1 has asked for the wisdom of the Perl Monks concerning the following question:

I need help with my script uploading files to google drive

i have tried to read some modules like LWP::Authen::OAuth2, and many examples on internet but they all pointing out to 'URL for user to go to to start the process in browser', but for me am using this script in server as a background process

Anyone who can help i really appreciate

#!/usr/bin/perl - wT use strict; use warnings; use LWP::UserAgent; use LWP::Authen::OAuth2; my $ua = LWP::UserAgent->new; my $oauth2 = LWP::Authen::OAuth2->new( client_id => 'client_id', client_secret => 'client_secret', site => 'https://accounts.google.com/o/oauth2/token', + # Token endpoint authorize_url => 'https://accounts.google.com/o/oauth2/auth', +# Authorization endpoint redirect_uri => 'my_url_used_in_google_console_when_creating_ +client_id_and_client_secret', ); # Use the access token to make API requests $ua->default_header( 'Authorization' => 'Bearer ' . $oauth2->acces +s_token ); my $url = 'https://www.googleapis.com/upload/drive/v3/files'; my $metadata = { name => '/var/www/.../filename.sql', # Path to file in server mimeType => 'application/txt', }; my $response = $ua->post( $url, 'Authorization' => 'Bearer ' . $oauth2->access_token, 'Content-Type' => 'application/json; charset=UTF-8', 'X-Upload-Content-Type' => 'application/txt', Content => encode_json($metadata), ); if ($response->is_success) { print $res->decoded_content; } else { print $response->decoded_content; print $response->status_line, "n"; }

Replies are listed 'Best First'.
Re: google drive
by ysth (Canon) on Sep 26, 2025 at 00:35 UTC
Re: google drive
by hippo (Archbishop) on Sep 25, 2025 at 21:12 UTC

    If you have problems (whether conceptually or syntactically) with this low-level approach, why not try using a high-level module such as Net::Google::Drive::Simple instead? Hopefully that will save you from having to bother about all the guts of OAuth2 yourself.


    🦛

      i tried this, and saw some errors, actually i tried Net::Google::Drive::Simple:V3, but couldn't work for me i always prefer API in certain things

        There is no access to Google Drive without an OAuth token. The only way to get an OAuth token is human (well, browser, with Javascript and Google login) interaction.