| 1 | package Net::Google::WebmasterTools::OAuth2; | ||
| 2 | { | ||
| 3 | $Net::Google::WebmasterTools::OAuth2::VERSION = '0.01'; | ||
| 4 | } | ||
| 5 | use strict; | ||
| 6 | |||
| 7 | # ABSTRACT: OAuth2 for Google Webmaster Tools API | ||
| 8 | |||
| 9 | use JSON; | ||
| 10 | use LWP::UserAgent; | ||
| 11 | use URI; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my $class = shift; | ||
| 15 | my $self = { @_ }; | ||
| 16 | |||
| 17 | die("client_id missing") if !$self->{client_id}; | ||
| 18 | die("client_secret missing") if !$self->{client_secret}; | ||
| 19 | |||
| 20 | $self->{redirect_uri} ||= 'urn:ietf:wg:oauth:2.0:oob'; | ||
| 21 | |||
| 22 | return bless($self, $class); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub authorize_url { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | my $uri = URI->new('https://accounts.google.com/o/oauth2/auth'); | ||
| 29 | $uri->query_form( | ||
| 30 | response_type => 'code', | ||
| 31 | client_id => $self->{client_id}, | ||
| 32 | redirect_uri => $self->{redirect_uri}, | ||
| 33 | scope => 'https://www.googleapis.com/auth/webmasters.readonly', | ||
| 34 | ); | ||
| 35 | |||
| 36 | return $uri->as_string; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub get_access_token { | ||
| 40 | my ($self, $code) = @_; | ||
| 41 | |||
| 42 | my $ua = LWP::UserAgent->new; | ||
| 43 | my $res = $ua->post('https://accounts.google.com/o/oauth2/token', [ | ||
| 44 | code => $code, | ||
| 45 | client_id => $self->{client_id}, | ||
| 46 | client_secret => $self->{client_secret}, | ||
| 47 | redirect_uri => $self->{redirect_uri}, | ||
| 48 | grant_type => 'authorization_code', | ||
| 49 | ]); | ||
| 50 | |||
| 51 | die('error getting token: ' . $res->status_line) unless $res->is_success; | ||
| 52 | |||
| 53 | return from_json($res->decoded_content); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub refresh_access_token { | ||
| 57 | my ($self, $refresh_token) = @_; | ||
| 58 | |||
| 59 | my $ua = LWP::UserAgent->new; | ||
| 60 | my $res = $ua->post('https://accounts.google.com/o/oauth2/token', [ | ||
| 61 | refresh_token => $refresh_token, | ||
| 62 | client_id => $self->{client_id}, | ||
| 63 | client_secret => $self->{client_secret}, | ||
| 64 | grant_type => 'refresh_token', | ||
| 65 | ]); | ||
| 66 | |||
| 67 | die('error getting token: ' . $res->status_line) unless $res->is_success; | ||
| 68 | |||
| 69 | return from_json($res->decoded_content); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub interactive { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | my $url = $self->authorize_url; | ||
| 76 | |||
| 77 | print(<<"EOF"); | ||
| 78 | Please visit the following URL, grant access to this application, and enter | ||
| 79 | the code you will be shown: | ||
| 80 | |||
| 81 | $url | ||
| 82 | |||
| 83 | EOF | ||
| 84 | |||
| 85 | print("Enter code: "); | ||
| 86 | my $code = <STDIN>; | ||
| 87 | chomp($code); | ||
| 88 | |||
| 89 | print("\nUsing code: $code\n\n"); | ||
| 90 | |||
| 91 | my $res = $self->get_access_token($code); | ||
| 92 | |||
| 93 | print("Access token: ", $res->{access_token}, "\n"); | ||
| 94 | print("Refresh token: ", $res->{refresh_token}, "\n"); | ||
| 95 | } | ||
| 96 | |||
| 97 | 1; | ||
| 98 | |||
| 99 | __END__ |