Granada_Perl_Workshop_2014_Google_API_Client

560 views
435 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
560
On SlideShare
0
From Embeds
0
Number of Embeds
37
Actions
Shares
0
Downloads
2
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Granada_Perl_Workshop_2014_Google_API_Client

  1. 1. Google API Perl Client Granada Perl Workshop 2014
  2. 2. Who are you? Alberto Jesús Gutiérrez Juanes GDG Granada GDG Spain @aljesus_gj Granada Perl Workshop 2014
  3. 3. Where? Repository GitHub http://github.com/comewalk/google-api-perl- client Granada Perl Workshop 2014
  4. 4. HELP Granada Perl Workshop 2014 VERSIÓN 0.13 07/2011 Contribute to the project
  5. 5. Granada Perl Workshop 2014 Perl? google-api- dotnet go java objectivec php python ruby -client
  6. 6. Granada Perl Workshop 2014 What? AdSense Management API Analytics Blogger Calendar Plus URL Shortener WebFonts
  7. 7. Granada Perl Workshop 2014 How?
  8. 8. Granada Perl Workshop 2014 Why? OAuth provides client applications a 'secure delegated access' to server resources on behalf of a resource owner. It specifies a process for resource owners to authorize third-party access to their server resources without sharing their credentials.
  9. 9. Granada Perl Workshop 2014 ● Lista Calendarios ● Lista Eventos ● Crear un evento ● Obtener un evento ● Actualizar un evento ● Eliminar un evento ● Mover un evento ● Obtener instancias ● ...
  10. 10. Granada Perl Workshop 2014 use Google::API::Client; use Google::API::OAuth2::Client; use lib 'eg/lib'; use Sample::Utils qw/get_or_restore_token store_token/; use constant MAX_PAGE_SIZE => 50; my $client = Google::API::Client->new; my $service = $client->build('calendar', 'v3'); my $file = "$FindBin::Bin/../client_secrets.json"; my $auth_driver = Google::API::OAuth2::Client->new_from_client_secrets($file, $service->{auth_doc}); my $dat_file = "$FindBin::Bin/token.dat"; my $access_token = get_or_restore_token($dat_file, $auth_driver);
  11. 11. Granada Perl Workshop 2014 # Call calendarlist.list my $res = $service->calendarList->list( body => { maxResults => MAX_PAGE_SIZE, minAccessRole => 'owner', } )->execute({ auth_driver => $auth_driver }); my $calendar_id = $res->{items}->[0]->{id}; my $dest_calendar_id = $res->{items}->[1]->{id};
  12. 12. Granada Perl Workshop 2014 # Call events.list my $page_token; my $count = 1; do { say "=== page $count ==="; my %body = ( calendarId => $calendar_id, ); if ($page_token) { $body{pageToken} = $page_token; } $res = $service->events->list( body => %body, )->execute({ auth_driver => $auth_driver }); $page_token = $res->{nextPageToken}; for my $event (@{$res->{items}}) { say encode_utf8($event->{summary}); } $count++; } until (!$page_token);
  13. 13. Granada Perl Workshop 2014 # Create a new event say '=== Create a new event ==='; my $new_event = { 'summary' => 'Appointment', 'location' => 'Somewhere', 'start' => { 'dateTime' => '2012-08-03T10:00:00+09:00', }, 'end' => { 'dateTime' => '2012-08-03T10:25:00+09:00', }, }; my $added_event = $service->events->insert( calendarId => $calendar_id, body => $new_event, )->execute({ auth_driver => $auth_driver }); say $added_event->{id};
  14. 14. Granada Perl Workshop 2014 # Get an event say '=== Get a event ==='; my $got_event = $service->events->get( body => { calendarId => $calendar_id, eventId => $added_event->{id}, } )->execute({ auth_driver => $auth_driver }); say $got_event->{id}; # Update an event say '=== Update an event ==='; $got_event->{summary} = 'Appointment at Somewhere'; my $updated_event = $service->events->update( calendarId => $calendar_id, eventId => $got_event->{id}, body => $got_event, )->execute({ auth_driver => $auth_driver }); say $updated_event->{updated}; # Delete an event say '=== Delete an event ==='; my $deleted_event = $service->events->delete( calendarId => $calendar_id, eventId => $got_event->{id}, )->execute({ auth_driver => $auth_driver });
  15. 15. Granada Perl Workshop 2014 use Google::API::Client; use OAuth2::Client; my $client = Google::API::Client->new; my $service = $client->build('plus', 'v1'); my $auth_driver = OAuth2::Client->new({ auth_uri => Google::API::Client->AUTH_URI, token_uri => Google::API::Client->TOKEN_URI, client_id => '<YOUR CLIENT ID>', client_secret => '<YOUR CLIENT SECRET>', redirect_uri => 'urn:ietf:wg:oauth:2.0:oob', auth_doc => $service->{auth_doc}, }); # people.get my $res = $service->people->get( body => { userId => 'me‘ })->execute({ auth_driver => $auth_driver });
  16. 16. Granada Perl Workshop 2014 #!/usr/bin/perl use strict; use warnings; use feature qw/say/; use Data::Dumper; use URI; use Google::API::Client; my $service = Google::API::Client->new->build('urlshortener', 'v1'); my $url = $service->url; # Create a shortened URL by inserting the URL into the url collection. my $body = { 'longUrl' => 'http://code.google.com/apis/urlshortener/', }; my $res = $url->insert(body => $body)->execute; say Dumper($res); my $short_url = $res->{'id'}; # Convert the shortened URL back into a long URL $res = $url->get(body => { shortUrl => $short_url })->execute; say Dumper($res); __END__
  17. 17. Granada Perl Workshop 2014 #!/usr/bin/perl use strict; use warnings; use feature qw/say/; use Data::Dumper; use URI; use Google::API::Client; use Google::API::OAuth2::Client; use lib 'eg/lib'; use Sample::Utils qw/get_or_restore_token store_token/; my $service = Google::API::Client->new->build('urlshortener', 'v1'); my $file = "$FindBin::Bin/../client_secrets.json"; my $auth_driver = Google::API::OAuth2::Client->new_from_client_secrets($file, $service->{auth_doc}); my $dat_file = "$FindBin::Bin/token.dat"; my $access_token = get_or_restore_token($dat_file, $auth_driver); my $url = $service->url; # Create a shortened URL by inserting the URL into the url collection. my $body = { 'longUrl' => 'http://code.google.com/apis/urlshortener/', }; my $res = $url->insert(body => $body)->execute; my $res = $url->insert(body => $body)->execute({ auth_driver => $auth_driver }); say Dumper($res); __END__ W ITH OAuth2
  18. 18. What’s next? Getting Started with Google APIs Client code.google.com/p/google-api-perl-client/
  19. 19. What’s next? Getting Started with Google APIs Client code.google.com/p/google-api-perl-client/

×