Giter Site home page Giter Site logo

api-google's Introduction

NAME

API::Google - Perl library for easy access to Google services via their API

VERSION

version 0.12

SYNOPSIS

use API::Google;
my $gapi = API::Google->new({ tokensfile => 'config.json' });

$gapi->refresh_access_token_silent('[email protected]');

$gapi->api_query({ 
  method => 'post', 
  route => 'https://www.googleapis.com/calendar/v3/calendars/'.$calendar_id.'/events',
  user => '[email protected]'
}, $json_payload_if_post);

CONFIGURATION

config.json must be structured like:

{ "gapi":
  {
    "client_id": "001122334455-abcdefghijklmnopqrstuvwxyz012345.apps.googleusercontent.com",
    "client_secret": "1ayL76NlEKjj85eZOipFZkyM",
    "tokens": {
        "[email protected]": {
            "refresh_token": "1/cI5jWSVnsUyCbasCQpDmz8uhQyfnWWphxvb1ST3oTHE",
            "access_token": "ya29.Ci-KA8aJYEAyZoxkMsYbbU9H_zj2t9-7u1aKUtrOtak3pDhJvCEPIdkW-xg2lRQdrA"
        },
        "[email protected]": {
            "access_token": "ya29.Ci-KAzT9JpaPriZ-ugON4FnANBXZexTZOz-E6U4M-hjplbIcMYpTbo0AmGV__tV5FA",
            "refresh_token": "1/_37lsRFSRaUJkAAAuJIRXRUueft5eLWaIsJ0lkJmEMU"
        }
    }
  }
}

You can easily generate such config with goauth CLI tool inluded in this package

SUBROUTINES/METHODS

refresh_access_token_silent

Get new access token for user from Google API server and store it in jsonfile

build_headers

Keep access_token in headers always actual

$gapi->build_http_transactio($user);

build_http_transaction

$gapi->build_http_transaction({ 
  user => '[email protected]',
  method => 'post',
  route => 'https://www.googleapis.com/calendar/users/me/calendarList',
  payload => { key => value }
})

api_query

Low-level method that can make API query to any Google service

Required params: method, route, user

Examples of usage:

$gapi->api_query({ 
    method => 'get', 
    route => 'https://www.googleapis.com/calendar/users/me/calendarList'',
    user => '[email protected]'
  });

$gapi->api_query({ 
    method => 'post', 
    route => 'https://www.googleapis.com/calendar/v3/calendars/'.$calendar_id.'/events',
    user => '[email protected]'
}, $json_payload_if_post);

AUTHOR

Pavel Serikov [email protected]

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Pavel Serikov.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

api-google's People

Contributors

pavelsr avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

api-google's Issues

Add to API::Google universal query() method

Make the library without limitations.

In particular, in current implementation there is no way to pass to Events.list() timeMax and timeMin params that is inconvenient.

More universal method would be:

$gcal_api->query({
  resource => 'Events'
  method => 'delete'
})

Also it will be great if object oriented way would be implemented (see method chaining)

$gcal_api->Events->delete($event_id)

As examples of good API implementation see source code of MojoX::CloudFlare::Simple, WWW::Telegram::BotAPI and Mojo::Cloudflare

Correct passing of tokensfile parameter to API::Google::Server

Now code will not work if

  • there is more than one json file in current directory
  • file with tokens has another extension that .json

Need to implement passing command-line parameters to Mojolicioius::Lite app (API::Google::Server package)

Full story:

API::Google::Server reads and write token information from json file with help of Config::JSON module

my $config = Config::JSON->new($f);

Now $f is just a first file with .json extension:

sub return_json_filename {
  use Cwd;
  my $cwd = getcwd;
  opendir my $dir, $cwd or die "Cannot open directory: $!";
  my @files = readdir $dir;
  my @j = grep { $_ =~ /\w+.json/ } @files;
  return $j[0];
}

Mojolicious::Lite app is calling from goauth CLI tool like this:

Mojolicious::Commands->start_app('API::Google::Server', 'daemon', '-l', 'http://*:'.$port, 'file', $filename);

As I understood, to pass parameter to Mojolicious::Lite app it's needed to implement custom commmand (example can be Mojolicious::Command::get and extend commands namespace like

my $commands = app->commands;
push @{$commands->namespaces}, 'API::Google::Server::Command::j;

where API::Google::Server::Command::j is a package with code of command handler. Like

# Lowercase command name
package Mojolicious::Command::mycommand;
use Mojo::Base 'Mojolicious::Command';
 
# Short description
has description => 'My first Mojo command';
 
# Short usage message
has usage => <<EOF;
Usage: APPLICATION mycommand [OPTIONS]
 
Options:
  -s, --something   Does something
EOF
 
sub run {
  my ($self, @args) = @_;
 
  # Magic here! :)
}

(taken from Mojolicious::Command)

Missing dependency: Mojolicious::Plugin::OAuth2

While trying to run goauth, the following problem occured:

...
OAuth details was updated!
Starting web server. Before authorization don't forget to set redirect_uri to http://127.0.0.1:3001/
Plugin "OAuth2" missing, maybe you need to install it?
Compilation failed in require at (eval 30) line 1.

It looks like API::Google::Server is missing Mojolicious::Plugin::OAuth2 as a dependency. After installing it, it worked properly ๐Ÿ‘

Add list of authorized scopes to tokens object

Old:

"tokens": {
        "[email protected]": {
            "refresh_token": "1/cI5jWSVnsUyCbasCQpDmz8uhQyfnWWphxvb1ST3oTHE",
            "access_token": "ya29.Ci-KA8aJYEAyZoxkMsYbbU9H_zj2t9-7u1aKUtrOtak3pDhJvCEPIdkW-xg2lRQdrA"
        },
        "[email protected]": {
            "access_token": "ya29.Ci-KAzT9JpaPriZ-ugON4FnANBXZexTZOz-E6U4M-hjplbIcMYpTbo0AmGV__tV5FA",
            "refresh_token": "1/_37lsRFSRaUJkAAAuJIRXRUueft5eLWaIsJ0lkJmEMU"
        }
    }

New:

"tokens": {
        "[email protected]": {
            "refresh_token": "1/cI5jWSVnsUyCbasCQpDmz8uhQyfnWWphxvb1ST3oTHE",
            "access_token": "ya29.Ci-KA8aJYEAyZoxkMsYbbU9H_zj2t9-7u1aKUtrOtak3pDhJvCEPIdkW-xg2lRQdrA",
           "authorized_scopes":  [ "email", "profile", "https://www.googleapis.com/auth/calendar" ]
        },
        "[email protected]": {
            "access_token": "ya29.Ci-KAzT9JpaPriZ-ugON4FnANBXZexTZOz-E6U4M-hjplbIcMYpTbo0AmGV__tV5FA",
            "refresh_token": "1/_37lsRFSRaUJkAAAuJIRXRUueft5eLWaIsJ0lkJmEMU",
            "authorized_scopes":  [ "email"  ]
        }
    }

JWS: decode failed at Crypt::JWT

https://github.com/DCIT/perl-Crypt-JWT/blob/master/lib/Crypt/JWT.pm#L602

Checked with JWT debugger - it says me that invalid signature

In our case Google returns JWS alg header = RS256. So key value can be:

  • public RSA key
  • perl HASH ref with JWK key structure
  • a reference to SCALAR string with PEM or DER or JSON/JWK data,
  • object: Crypt::PK::RSA, Crypt::OpenSSL::RSA, Crypt::X509 or Crypt::OpenSSL::X509

In our case it's perl HASH ref with JWK key structure

https://www.googleapis.com/oauth2/v3/certs

Issue: extract header from JWT id_token and select key from here with correct kid

header:

{
  "alg": "RS256",
  "kid": "abc47f0cea3d1f9bf7072e53d4e1d1adb010642d"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.