Created
September 15, 2012 08:01
-
-
Save throughnothing/3726907 to your computer and use it in GitHub Desktop.
Revisions
-
throughnothing revised this gist
Sep 15, 2012 . 1 changed file with 6 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 =>'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->{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> -
throughnothing revised this gist
Sep 15, 2012 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 # 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 -
throughnothing revised this gist
Sep 15, 2012 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
throughnothing revised this gist
Sep 15, 2012 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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' ), }}; -
throughnothing revised this gist
Sep 15, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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'} %>> <% } %> </div> <% } %> -
throughnothing created this gist
Sep 15, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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>