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

Hello Perl Community !

I wrote a script that allows me to access google spreadsheet, access content coming from users that have filed a form.

It's been working flawlessly for almost a year until now...

The script cannot access the spreasheet anymore. The code returned by google is 404 and some lines about the capabilities of gdrive but without any hint.
To troubleshoot the issue, I created a new project and reused the code I previously used to setup the access.
(This code was found on stackoverflow)
The process consists in executing a first script to obtain the access token and refresh token and save this information into a session file.
Then your script uses this file to access google spreasheets.
The 1str step works fine and I can see the access and refresh tokens in the generated file.
Here is the code for the 1st step.

#!/usr/bin/perl # Code to get a web-based token that can be stored # and used later to authorize our spreadsheet access. # Based on code from https://gist.github.com/hexaddikt/6738162 #------------------------------------------------------------------- # To use this code: # 1. Edit the lines below to put in your own # client_id and client_secret from Google. # 2. Run this script and follow the directions on # the screen, which will give step you # through the following steps: # 3. Copy the URL printed out, and paste # the URL in a browser to load the page. # 4. On the resulting page, click OK (possibly # after being asked to log in to your Google # account). # 5. You will be redirected to a page that provides # a code that you should copy and paste back into the # terminal window, so this script can exchange it for # an access token from Google, and store the token. # That will be the token the other spreadsheet access # code can use. use Net::Google::DataAPI::Auth::OAuth2; use Net::Google::Spreadsheets; use Storable; #to save and restore token for future use use Term::Prompt; # Provide the filename in which we will store the access # token. This file will also need to be readable by the # other script that accesses the spreadsheet and parses # the contents. my $session_filename = "stored_google_access.session"; # Code for accessing your Google account. The required client_id # and client_secret can be found in your Google Developer's console # page, as described in the detailed instruction document. This # block of code will also need to appear in the other script that # accesses the spreadsheet. # Be sure to edit the lines below to fill in your correct client # id and client secret! my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new( client_id => '*********.apps.googleusercontent.com', client_secret => '********', scope => ['http://spreadsheets.google.com/feeds/'], redirect_uri => 'https://developers.google.com/oauthplayground', ); # We need to set these parameters this way in order to ensure # that we get not only an access token, but also a refresh token # that can be used to update it as needed. my $url = $oauth2->authorize_url(access_type => 'offline', approval_prompt => 'force'); # Give the user instructions on what to do: print <<END The following URL can be used to obtain an access token from Google. 1. Copy the URL and paste it into a browser. 2. You may be asked to log into your Google account if you were not logged in already in that browser. If so, go ahead and log in to whatever account you want to have access to the Google doc. 3. On the next page, click "Accept" when asked to grant access. 4. You will then be redirected to a page with a box in the left-hand column labeled "Authorization code". Copy the code in that box and come back here. Here is the URL to paste in your browser to get the code: $url END ; # Here is where we get the code from the user: my $code = prompt('x', 'Paste the code obtained at the above URL here: + ', '', ''); # Exchange the code for an access token: my $token = $oauth2->get_access_token($code) or die; # If we get to here, it worked! Report success: print "\nToken obtained successfully!\n"; print "Here are the token contents (just FYI):\n\n"; print $token->to_string, "\n"; # Save the token for future use: my $session = $token->session_freeze; store($session, $session_filename); print <<END2 Token successfully stored in file $session_filename. Use that filename in your spreadsheet-access script to load the token as needed for access to the spreadsheet data. END2 ;

As written before, this previous step seems to work fine.
Now I execute a second script to validate the access and this is failing.
use Net::Google::Spreadsheets; use Net::Google::DataAPI::Auth::OAuth2; use Net::OAuth2::AccessToken; use Storable; # Authentication code based on example from gist at # https://gist.github.com/hexaddikt/6738247 # Get the token that we saved previously in order to authenticate: my $session_filename = "stored_google_access.session"; # Be sure to edit the lines below to fill in your correct client # id and client secret! my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new( client_id => '****.apps.googleusercontent.com', client_secret => '***', scope => ['http://spreadsheets.google.com/feeds/'], redirect_uri => 'https://developers.google.com/oauthplayground', ); # Deserialize the file so we can thaw the session and reuse the refres +h token my $session = retrieve($session_filename); if($session->error) { print $session->error_description; } my $restored_token = Net::OAuth2::AccessToken->session_thaw($session, auto_refresh => 1, profile => $oauth2->oauth2_webserver, ); $oauth2->access_token($restored_token); # Now we can use this token to access the spreadsheets # in our account: my $service = Net::Google::Spreadsheets->new( auth => $oauth2); my $spreadsheet_by_title = $service->spreadsheet( { title => 'aaa' } );

The code fails with the following error :
request for 'https://spreadsheets.google.com/feeds/spreadsheets/privat +e/full?title=aaa' failed: 404 Not Found <!DOCTYPE html><
It seems that the url used doesn't work anymore.
If I use the same url in a browser in which I am have access to the gdrive content, the sheet cannot be accessed.
Has anyone faced the same issue?
Thanks.
X.

Replies are listed 'Best First'.
Re: Accessing Google sheets using perl
by Marshall (Canon) on Aug 20, 2021 at 18:48 UTC
    I noticed that there is Net-Google-Spreadsheets-V4-0.002. Its from 2017 but appears to be passing tests currently. I think Net-Google-Spreadsheets means a version from 2012. It could be that Google deprecated something that used to work. I haven't kept up with GDrive recent changes.

    Update:
    I have no experience with Perl and accessing a Google Drive. However, this subject does interest me for future work. At my Windows 10 command line, with Active State Perl (v5.24.3 built on Dec 11 2017 11:09:26), I did "cpan Net-Google-Drive-Simple". The installation reported success. CPAN reports this version as of Jan 28, 2021.

    I am in the midst of another project right now and don't really have much time to mess with this at the moment. However, my perusal of the documentation indicates that this is a very promising avenue of investigation. If this recent module does what you need, it could be better than what you have now? I don't know. Let us know if it is?

    PS: The installation spewed out 1,600 lines of stuff to my terminal. I don't think any of that matters. At the end of the day, it said that the installation "worked".

      The documentation of the Net-Google-Spreadsheets-V4 doesn't explain how to read cell values from google sheet. I could not managed to read data from my google sheet yet.
Re: Accessing Google sheets using perl
by Bod (Parson) on Aug 20, 2021 at 08:26 UTC

    There is a CPAN module available for accessing Google Sheets - Google::Spreadsheets.
    It may be worth creating some test code around this module to see if it too fails in the same way.

    I've not used this module so cannot say how well it works but it might just help in your diagnosis.

      When recommending a module which you aren't sure of, best to check open issues. This was last updated in 2012 and seems like it no longer functions.