| 1 | ########################################### | ||
| 2 | package OAuth::Cmdline::Automatic; | ||
| 3 | ########################################### | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use MIME::Base64; | ||
| 7 | |||
| 8 | use base qw( OAuth::Cmdline ); | ||
| 9 | |||
| 10 | ########################################### | ||
| 11 | sub site { | ||
| 12 | ########################################### | ||
| 13 | return "automatic"; | ||
| 14 | } | ||
| 15 | |||
| 16 | ########################################### | ||
| 17 | sub redirect_uri { | ||
| 18 | ########################################### | ||
| 19 | my( $self ) = @_; | ||
| 20 | |||
| 21 | return undef; | ||
| 22 | } | ||
| 23 | |||
| 24 | 1; | ||
| 25 | |||
| 26 | __END__ | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | OAuth::Cmdline::Automatic - Automatic.com-specific OAuth oddities | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | my $oauth = OAuth::Cmdline::Automatic->new( ); | ||
| 35 | $oauth->access_token(); | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | This class overrides methods of C<OAuth::Cmdline> because | ||
| 40 | Automatic.com's Web API deviates from standard OAuth. | ||
| 41 | |||
| 42 | Automatic.com deviates from the standard OAuth protocol in two significant | ||
| 43 | ways listed below. | ||
| 44 | |||
| 45 | =head2 No Automatic.com support for redirect URL | ||
| 46 | |||
| 47 | Since Automatic doesn't allow a C<redirect_uri> parameter in the initial | ||
| 48 | token dance move, make sure to set the field C<"OAuth Redirect URL"> | ||
| 49 | in the new app registration form on | ||
| 50 | I<https://developer.automatic.com/my-apps> to | ||
| 51 | |||
| 52 | http://localhost:8082/callback | ||
| 53 | |||
| 54 | which is where C<eg/automatic-token-init> is waiting for the response | ||
| 55 | from the authentication server. Other OAuth consumers let you specify | ||
| 56 | a redirect URL in the request, which is more convenient. | ||
| 57 | |||
| 58 | =head2 No Automatic.com support for a refresh token | ||
| 59 | |||
| 60 | Other OAuth consumers return a refresh_token alongside with the access_token, | ||
| 61 | so if the access_token expires, you can fetch a new one by simply providing | ||
| 62 | the saved refresh token. Automatic.com, however, upon successful | ||
| 63 | user authorization, only sends back an access_token without a refresh_token. | ||
| 64 | Since their access_token has an expiration time of one year, it's not | ||
| 65 | an immediate problem but could prove to be largely inconvenient for the | ||
| 66 | user when that time comes and they have to re-grant access for the app. | ||
| 67 | |||
| 68 | =head1 EXAMPLE | ||
| 69 | |||
| 70 | After registering the app (note the first item above), put the values | ||
| 71 | for client_id and client_secret in YAML format | ||
| 72 | into C<.automatic.yml> in your home directory: | ||
| 73 | |||
| 74 | # ~/.automatic.yml | ||
| 75 | client_id: XXXXX | ||
| 76 | client_secret: YYYY | ||
| 77 | |||
| 78 | Then run the | ||
| 79 | |||
| 80 | eg/automatic-token-init | ||
| 81 | |||
| 82 | script included in this distribution and | ||
| 83 | point your browser to C<http://localhost:8082>. Then follow the | ||
| 84 | authentication flow until redirected back to localhost. By then, | ||
| 85 | the script will have updated the file C<~/.automatic.yml> to | ||
| 86 | contain the access token needed for further authenticated calls. | ||
| 87 | |||
| 88 | After that, any script like the following can access your Automatic | ||
| 89 | data from the server: | ||
| 90 | |||
| 91 | use OAuth::Cmdline::Automatic; | ||
| 92 | use LWP::UserAgent; | ||
| 93 | use JSON qw( from_json ); | ||
| 94 | |||
| 95 | my $oauth = OAuth::Cmdline::Automatic->new(); | ||
| 96 | my $ua = LWP::UserAgent->new(); | ||
| 97 | |||
| 98 | my $resp = $ua->get( "https://api.automatic.com/device/", | ||
| 99 | $oauth->authorization_headers ); | ||
| 100 | |||
| 101 | if( $resp->is_error ) { | ||
| 102 | die $resp->message; | ||
| 103 | } | ||
| 104 | |||
| 105 | my $data = from_json( $resp->decoded_content ); | ||
| 106 | |||
| 107 | for my $device ( @{ $data->{ results } } ) { | ||
| 108 | printf "%s-v%d %s\n", | ||
| 109 | $device->{ id }, | ||
| 110 | $device->{ version }, | ||
| 111 | $device->{ url }; | ||
| 112 | } | ||
| 113 | |||
| 114 | =head1 LEGALESE | ||
| 115 | |||
| 116 | Copyright 2016 by Mike Schilli, all rights reserved. | ||
| 117 | This program is free software, you can redistribute it and/or | ||
| 118 | modify it under the same terms as Perl itself. | ||
| 119 | |||
| 120 | =head1 AUTHOR | ||
| 121 | |||
| 122 | 2016, Mike Schilli <cpan@perlmeister.com> |