Top Banner
Google API Perl Client Granada Perl Workshop 2014
19

Granada_Perl_Workshop_2014_Google_API_Client

Aug 19, 2014

Download

Engineering

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Granada_Perl_Workshop_2014_Google_API_Client

Google API Perl

Client

Granada Perl Workshop 2014

Page 2: Granada_Perl_Workshop_2014_Google_API_Client

Who are you?Alberto Jesús

Gutiérrez Juanes

GDG Granada GDG Spain@aljesus_gj

Granada Perl Workshop 2014

Page 3: Granada_Perl_Workshop_2014_Google_API_Client

Where?

Repository GitHubhttp://github.com/comewalk/google-api-perl-client

Granada Perl Workshop 2014

Page 4: Granada_Perl_Workshop_2014_Google_API_Client

HELP

Granada Perl Workshop 2014

VERSIÓN 0.13

07/2011

Contribute to the project

Page 5: Granada_Perl_Workshop_2014_Google_API_Client

Granada Perl Workshop 2014

Perl?

google-api-

dotnetgojavaobjectivecphppythonruby

-client

Page 6: Granada_Perl_Workshop_2014_Google_API_Client

Granada Perl Workshop 2014

What?AdSense Management API

Analytics

Blogger

Calendar

Plus

URL Shortener

WebFonts

Page 7: Granada_Perl_Workshop_2014_Google_API_Client

Granada Perl Workshop 2014

How?

Page 8: Granada_Perl_Workshop_2014_Google_API_Client

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.

Page 9: Granada_Perl_Workshop_2014_Google_API_Client

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

● ...

Page 10: Granada_Perl_Workshop_2014_Google_API_Client

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);

Page 11: Granada_Perl_Workshop_2014_Google_API_Client

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};

Page 12: Granada_Perl_Workshop_2014_Google_API_Client

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);

Page 13: Granada_Perl_Workshop_2014_Google_API_Client

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};

Page 14: Granada_Perl_Workshop_2014_Google_API_Client

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 });

Page 15: Granada_Perl_Workshop_2014_Google_API_Client

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.getmy $res = $service->people->get(body => {userId => 'me‘})->execute({ auth_driver => $auth_driver });

Page 16: Granada_Perl_Workshop_2014_Google_API_Client

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__

Page 17: Granada_Perl_Workshop_2014_Google_API_Client

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__

WITH OAuth2

Page 18: Granada_Perl_Workshop_2014_Google_API_Client

What’s next?Getting Started with Google APIs Clientcode.google.com/p/google-api-perl-client/

Page 19: Granada_Perl_Workshop_2014_Google_API_Client

What’s next?Getting Started with Google APIs Clientcode.google.com/p/google-api-perl-client/