OAuth::Cmdline::Smartthings - Smartthings-specific OAuth oddities
version 0.07
my $oauth = OAuth::Cmdline::Smartthings->new( ); my $json = $oauth->http_get( $oauth->base_uri . "/api/smartapps/endpoints" );
This class provides the necessary glue to interact with Smartthings Web API. A short tutorial on how to use the API can be found here:
http://docs.smartthings.com/en/latest/smartapp-web-services-developers-guide/tutorial-part1.html
To register with Smartthings, go to
https://graph-na02-useast1.api.smartthings.com (US) https://graph-eu01-euwest1.api.smartthings.com (UK)
with your browser and create a new app, enable the "OAuth" section, then cut-and-paste the client ID and client secret into your ~/.smartthings.yml file:
~/.smartthings.yml
client_id: xxx client_secret: yyy
Also use
http://localhost:8082/callback
as a "redirect URI" (not optionial as the label would suggest) and then run eg/smartthings-token-init (in this distribution) and point your browser to
eg/smartthings-token-init
http://localhost:8082
Then click the login link and follow the flow.
After registration and initialization (see above), the local ~/.smartthings.yml file should contain an access token. With this in place, all that is required to obtain the endpoint of the specific Smartthings app installation and fetch the status of all switches to which the user has granted access to is the following code:
use OAuth::Cmdline::Smartthings; use JSON qw( from_json ); my $oauth = OAuth::Cmdline::Smartthings->new; my $json = $oauth->http_get( $oauth->base_uri . "/api/smartapps/endpoints" ); if( !defined $json ) { die "Can't get endpoints"; } my $uri = from_json( $json )->[ 0 ]->{ uri } . "/switches"; my $data = $oauth->http_get( $uri ); print "$data\n";
will print something like
[{"name":"Outlet","value":"on"}]
Mike Schilli <cpan@perlmeister.com>
This software is copyright (c) 2022 by Mike Schilli.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install OAuth::Cmdline, copy and paste the appropriate command in to your terminal.
cpanm
cpanm OAuth::Cmdline
CPAN shell
perl -MCPAN -e shell install OAuth::Cmdline
For more information on module installation, please visit the detailed CPAN module installation guide.