0

I have an application that uses Net::Google::Spreadsheets. It began to fail with authentication errors earlier this week. My understanding is that Google has deprecated some authentication methods, and that we are now to use OAuth2.

My application runs on a headless server, so I cannot use the three-legged OAuth2 solution shown in Net::Google::AuthSub login failed with new Google Drive version

I need to use two-legged authentication with a service account. I put together code to build the JWT, and obtain the access_token (as detailed in the Google developer documentation).

What I need some assistance with is the method I need to use to get Net::Google::Spreadsheets to make use of this access_token, or a method to do two-legged OAuth2 that works with this module.

1 Answer 1

3

If you have managed to create the access_token, then you should be able to get it into the right format for Net::Google::Spreadsheets by using it to create a new Net::OAuth2::AccessToken object, and then proceeding more or less as in the example you linked to in the other thread:

use Net::Google::Spreadsheets;
use Net::Google::DataAPI::Auth::OAuth2;
use Net::OAuth2::AccessToken;

my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
    client_id => 'my_client_id.apps.googleusercontent.com',
    client_secret => 'my secret',
    scope => ['http://spreadsheets.google.com/feeds/'],
    );
my $access_token = 'token you generate somehow';

my $token = Net::OAuth2::AccessToken->new(access_token => $access_token,
                  profile => $oauth2->oauth2_webserver,
                  token_type => 'Bearer',
                  );
$oauth2->access_token($token);

my $service = Net::Google::Spreadsheets->new(auth => $oauth2);

and then you can use the service from there. If you want to repeatedly use the same token, you'll need to get a refresh_token as well and include that, as well as setting auto_refresh, but if you're generating a new one each time, this should work.

Sign up to request clarification or add additional context in comments.

1 Comment

ELNJ, Thanks so much for your response. This pretty much did the trick. This took care of the authentication, and the only other thing I had to do was to share my spreadsheet with the google-generated service account email address (instead of my main email address).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.