| 1 | ########################################### | ||||||||||
| 2 | package OAuth::Cmdline::Smartthings; | ||||||||||
| 3 | ########################################### | ||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use MIME::Base64; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | extends "OAuth::Cmdline"; | |||||||||
| 10 | |||||||||||
| 11 | sub BUILD { | ||||||||||
| 12 | 0 | ▶ | my( $self ) = @_; | ||||||||
|
Subroutine
BUILD: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 13 | |||||||||||
| 14 | 0 | ▶ | if( !defined $self->base_uri ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | # default to US | ||||||||||
| 16 | 0 | $self->base_uri( "https://graph-na02-useast1.api.smartthings.com" ); | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 0 | $self->login_uri( $self->base_uri . "/oauth/authorize" ); | |||||||||
| 20 | 0 | $self->token_uri( $self->base_uri . "/oauth/token" ); | |||||||||
| 21 | |||||||||||
| 22 | 0 | $self->client_init_conf_check( $self->base_uri ); | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | ########################################### | ||||||||||
| 26 | sub site { | ||||||||||
| 27 | ########################################### | ||||||||||
| 28 | 0 | ▶ | return "smartthings"; | ||||||||
|
Subroutine
site: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 1 | 1; | |||||||||
| 32 | |||||||||||
| 33 | __END__ | ||||||||||
| 34 | |||||||||||
| 35 | =head1 NAME | ||||||||||
| 36 | |||||||||||
| 37 | OAuth::Cmdline::Smartthings - Smartthings-specific OAuth oddities | ||||||||||
| 38 | |||||||||||
| 39 | =head1 SYNOPSIS | ||||||||||
| 40 | |||||||||||
| 41 | my $oauth = OAuth::Cmdline::Smartthings->new( ); | ||||||||||
| 42 | my $json = | ||||||||||
| 43 | $oauth->http_get( $oauth->base_uri . "/api/smartapps/endpoints" ); | ||||||||||
| 44 | |||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||
| 46 | |||||||||||
| 47 | This class provides the necessary glue to interact with Smartthings | ||||||||||
| 48 | Web API. A short tutorial on how to use the API can be found here: | ||||||||||
| 49 | |||||||||||
| 50 | http://docs.smartthings.com/en/latest/smartapp-web-services-developers-guide/tutorial-part1.html | ||||||||||
| 51 | |||||||||||
| 52 | =head1 REGISTRATION | ||||||||||
| 53 | |||||||||||
| 54 | To register with Smartthings, go to | ||||||||||
| 55 | |||||||||||
| 56 | https://graph-na02-useast1.api.smartthings.com (US) | ||||||||||
| 57 | https://graph-eu01-euwest1.api.smartthings.com (UK) | ||||||||||
| 58 | |||||||||||
| 59 | with your browser and create a new app, enable the "OAuth" section, then | ||||||||||
| 60 | cut-and-paste the client ID and client secret into | ||||||||||
| 61 | your C<~/.smartthings.yml> file: | ||||||||||
| 62 | |||||||||||
| 63 | client_id: xxx | ||||||||||
| 64 | client_secret: yyy | ||||||||||
| 65 | |||||||||||
| 66 | Also use | ||||||||||
| 67 | |||||||||||
| 68 | http://localhost:8082/callback | ||||||||||
| 69 | |||||||||||
| 70 | as a "redirect URI" (not optionial as the label would suggest) and then | ||||||||||
| 71 | run C<eg/smartthings-token-init> (in this distribution) and point your | ||||||||||
| 72 | browser to | ||||||||||
| 73 | |||||||||||
| 74 | http://localhost:8082 | ||||||||||
| 75 | |||||||||||
| 76 | Then click the login link and follow the flow. | ||||||||||
| 77 | |||||||||||
| 78 | =head1 Web Client Use | ||||||||||
| 79 | |||||||||||
| 80 | After registration and initialization (see above), the local | ||||||||||
| 81 | C<~/.smartthings.yml> file should contain an access token. With | ||||||||||
| 82 | this in place, all that is required to obtain the endpoint of | ||||||||||
| 83 | the specific Smartthings app installation and fetch the status of | ||||||||||
| 84 | all switches to which the user has granted access to is the following code: | ||||||||||
| 85 | |||||||||||
| 86 | use OAuth::Cmdline::Smartthings; | ||||||||||
| 87 | use JSON qw( from_json ); | ||||||||||
| 88 | |||||||||||
| 89 | my $oauth = OAuth::Cmdline::Smartthings->new; | ||||||||||
| 90 | |||||||||||
| 91 | my $json = $oauth->http_get( | ||||||||||
| 92 | $oauth->base_uri . "/api/smartapps/endpoints" ); | ||||||||||
| 93 | |||||||||||
| 94 | if( !defined $json ) { | ||||||||||
| 95 | die "Can't get endpoints"; | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | my $uri = from_json( $json )->[ 0 ]->{ uri } . "/switches"; | ||||||||||
| 99 | my $data = $oauth->http_get( $uri ); | ||||||||||
| 100 | print "$data\n"; | ||||||||||
| 101 | |||||||||||
| 102 | will print something like | ||||||||||
| 103 | |||||||||||
| 104 | [{"name":"Outlet","value":"on"}] | ||||||||||
| 105 | |||||||||||
| 106 | =head1 LEGALESE | ||||||||||
| 107 | |||||||||||
| 108 | Copyright 2016 by Mike Schilli, all rights reserved. | ||||||||||
| 109 | This program is free software, you can redistribute it and/or | ||||||||||
| 110 | modify it under the same terms as Perl itself. | ||||||||||
| 111 | |||||||||||
| 112 | =head1 AUTHOR | ||||||||||
| 113 | |||||||||||
| 114 | 2016, Mike Schilli <cpan@perlmeister.com> | ||||||||||