Skip to content

Instantly share code, notes, and snippets.

@throughnothing
Created September 15, 2012 08:01
Show Gist options
  • Save throughnothing/3726907 to your computer and use it in GitHub Desktop.
Save throughnothing/3726907 to your computer and use it in GitHub Desktop.

Revisions

  1. throughnothing revised this gist Sep 15, 2012. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions goauth.pl
    Original file line number Diff line number Diff line change
    @@ -9,18 +9,18 @@
    # Run this sample app with:
    # morbo goauth.pl --listen http://*:5555


    my $config = plugin Config => { default => {
    # Google OAuth API Key Values
    # Get yours from: https://code.google.com/apis/console#access
    client_id => '',
    secret => '',

    # Google Contacts Scope
    scope => 'https://www.google.com/m8/feeds/',
    # Google OAuth base uri
    oauth_base => 'https://accounts.google.com/o/oauth2',
    contacts_full =>'https://www.google.com/m8/feeds/contacts/default/full/' .
    '?alt=json&max-results=3000',
    contacts_full =>'contacts/default/full/?alt=json&max-results=3000',

    # Application callback (url escaped)
    cb => url_escape( 'http://localhost:5555/cb' ),
    }};
    @@ -64,7 +64,8 @@

    # Get the contacts
    my $c_res = $self->app->ua->get(
    $config->{contacts_full}, { Authorization => "Bearer $a_token" }
    "$config->{scope}$config->{contacts_full}",
    { Authorization => "Bearer $a_token" }
    )->res;

    die 'Error' unless $c_res->is_status_class(200);
    @@ -87,4 +88,4 @@
    <% } %>
    </div>
    <% } %>
    </body></html>
    </body></html>
  2. throughnothing revised this gist Sep 15, 2012. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion goauth.pl
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,11 @@
    use Mojo::Util qw(url_escape);

    # Sample Google OAuth contacts app using Mojolicious
    # Run with:
    # For more info see:
    # https://developers.google.com/google-apps/contacts/v3/
    # https://developers.google.com/oauthplayground/
    #
    # Run this sample app with:
    # morbo goauth.pl --listen http://*:5555


  3. throughnothing revised this gist Sep 15, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions goauth.pl
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,9 @@
    use Mojo::Util qw(url_escape);

    # Sample Google OAuth contacts app using Mojolicious
    # Run with:
    # morbo goauth.pl --listen http://*:5555


    my $config = plugin Config => { default => {
    # Google OAuth API Key Values
  4. throughnothing revised this gist Sep 15, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions goauth.pl
    Original file line number Diff line number Diff line change
    @@ -8,14 +8,12 @@
    # Get yours from: https://code.google.com/apis/console#access
    client_id => '',
    secret => '',

    # Google Contacts Scope
    scope => 'https://www.google.com/m8/feeds/',
    # Google OAuth base uri
    oauth_base => 'https://accounts.google.com/o/oauth2',
    contacts_full =>'https://www.google.com/m8/feeds/contacts/default/full/' .
    '?alt=json&max-results=3000',

    # Application callback (url escaped)
    cb => url_escape( 'http://localhost:5555/cb' ),
    }};
  5. throughnothing revised this gist Sep 15, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions goauth.pl
    Original file line number Diff line number Diff line change
    @@ -77,8 +77,8 @@
    <% for ( @$contacts ) { %>
    <div>
    <% if ( $_->{'gd$email'}[0] ) { %>
    <%= $_->{title}{'$t'} %>,
    <%= $_->{'gd$email'}[0]{'address'} %>
    <%= $_->{title}{'$t'} %>
    &lt;<%= $_->{'gd$email'}[0]{'address'} %>&gt;
    <% } %>
    </div>
    <% } %>
  6. throughnothing created this gist Sep 15, 2012.
    85 changes: 85 additions & 0 deletions goauth.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    use Mojolicious::Lite;
    use Mojo::Util qw(url_escape);

    # Sample Google OAuth contacts app using Mojolicious

    my $config = plugin Config => { default => {
    # Google OAuth API Key Values
    # Get yours from: https://code.google.com/apis/console#access
    client_id => '',
    secret => '',

    # Google Contacts Scope
    scope => 'https://www.google.com/m8/feeds/',
    # Google OAuth base uri
    oauth_base => 'https://accounts.google.com/o/oauth2',
    contacts_full =>'https://www.google.com/m8/feeds/contacts/default/full/' .
    '?alt=json&max-results=3000',

    # Application callback (url escaped)
    cb => url_escape( 'http://localhost:5555/cb' ),
    }};

    get '/' => sub { shift->render( 'home' ) };

    get '/auth' => sub {
    # Redirect user to Googl OAuth Login page
    shift->redirect_to("$config->{oauth_base}/auth" .
    "?client_id=$config->{client_id}&response_type=code" .
    "&scope=$config->{scope}&redirect_uri=$config->{cb}"
    );
    };

    # OAuth 2 callback from google
    get '/cb' => sub {
    my ($self) = @_;

    #Get tokens from auth code
    my $res = $self->app->ua->post_form("$config->{oauth_base}/token", {
    code => $self->param('code'),
    redirect_uri => $config->{cb},
    client_id => $config->{client_id},
    client_secret => $config->{secret},
    scope => $config->{scope},
    grant_type => 'authorization_code',
    })->res;

    die "Error getting tokens" unless $res->is_status_class(200);

    # Save access token to session
    $self->session->{access_token} = $res->json->{access_token};
    $self->redirect_to('/contacts');
    };

    get '/contacts' => sub {
    my ($self) = @_;

    # Read access token from session
    my $a_token = $self->session->{access_token} or die "No access token!";

    # Get the contacts
    my $c_res = $self->app->ua->get(
    $config->{contacts_full}, { Authorization => "Bearer $a_token" }
    )->res;

    die 'Error' unless $c_res->is_status_class(200);
    $self->stash( contacts => $c_res->json->{feed}{entry} );
    };

    app->start;

    __DATA__
    @@ home.html.ep
    <a href='/auth'>Click here</a> to authenticate with Google OAuth.
    @@ contacts.html.ep
    <html><body>
    <% for ( @$contacts ) { %>
    <div>
    <% if ( $_->{'gd$email'}[0] ) { %>
    <%= $_->{title}{'$t'} %>,
    <%= $_->{'gd$email'}[0]{'address'} %>
    <% } %>
    </div>
    <% } %>
    </body></html>