Cmdline/Automatic.pm untested

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