Reputation: 5233
I am currently trying to make this work:
my $ua = Mojo::UserAgent->new;
my $req = Mojo::Message::Request->new;
my $tx = $ua->build_tx(GET => 'https://spreadsheets.google.com/feeds/spreadsheets/private/full');
app->log->info($c->session('token'));
$tx->req->headers->authorization('Bearer ' . $c->session('token'));
where $c->session('token')
is the token I got via Mojolicious::Plugin::OAuth2.
I only get back an empty response. Doing the same (I think) via curl works OK:
curl -v -H "authorization: Bearer the_same_token_as_above" https://spreadsheets.google.com/feeds/spreadsheets/private/full
What am I doing wrong?
Upvotes: 2
Views: 1097
Reputation: 333
The only thing I see that you're missing is a call to start
. Adding the following two lines to your code block works for me (albeit with a different url/token):
$tx = $ua->start($tx);
app->log->info($tx->res->body);
If you have a lot of API calls that need to be authorized, then you might want to try the approach similar to this, shown below:
my $ua = Mojo::UserAgent->new;
$ua->on(start => sub {
my ($ua, $tx) = @_;
$tx->req->headers->authorization('Bearer <your token here>');
});
my $tx = $ua->get('<your first url here>');
app->log->info("Response body:", $tx->res->body);
my $tx = $ua->get('<your second url here>');
app->log->info("Response body:", $tx->res->body);
# etc...
The advantage of this technique is that each time you use the get
method of that UserAgent
instance, it will fire the start event listener and add the authorization header for you.
Upvotes: 5