Search code examples
perloauth-2.0google-authenticationmojolicious

Mojolicious app using google authentication, plugin OAuth2


I am using Mojolicious::Plugin::OAuth2 to build a simple app where you can log in using your google+ credentials, and I am having trouble with the syntax.

My code is very close to the example they give:

use Mojolicious::Lite;
use Mojolicious::Plugin::OAuth2;

plugin 'OAuth2' => {
  google => {
    key => 'xxxxxx.apps.googleusercontent.com',
    secret => 'xxxxxxxx',
  },
};

get "/auth" => sub {
  my $self = shift;
  $self->delay(
    sub {
      my $delay = shift;
      $self->get_token(google => $delay->begin, scope->profile)
    },
    sub {
      my($delay, $token, $tx) = @_;
      return $self->render(text => $tx->res->error) unless $token;
      $self->session(token => $token);
      $self->render(text => $token);
    },
  );
};

 app->start;

The problem area being the scope->profile. Without specifying the scope, I get an error from google saying "400: Error: invalid_request Missing required parameter: scope" but I can't quite get it right and now receive syntax errors.

The documentation says to do it like this $token = $c->get_token($provider_name => \%args); So how do I write that args hash so that it makes sense?


Solution

  • This syntax is what worked for me

      $self->get_token('google', scope => 'profile', $delay->begin);
    

    I had to put $delay->begin at the end and google in quotes.