Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

google drive

by frank1 (Monk)
on Sep 25, 2025 at 20:25 UTC ( [id://11166368]=perlquestion: print w/replies, xml ) Need Help??

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.

Re: google drive
by Veltro (Hermit) on Oct 10, 2025 at 08:30 UTC
    I've shared an example here a few years back to interact with gmail. Wrote my own Authen\SASL\Perl\XOUTH2.pm to handle the Bearer. The Perl program was able to get the access token on the command line and it facilitates refreshing as well. I have not looked at your code in detail, but it looks similar, maybe the example is usefull to you: Send email with OAuth 2 through Gmail SMTP or API

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11166368]
Approved by choroba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2025-11-13 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (69 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.