The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

OAuth::Cmdline::Smartthings - Smartthings-specific OAuth oddities

VERSION

version 0.07

SYNOPSIS

    my $oauth = OAuth::Cmdline::Smartthings->new( );
    my $json = 
        $oauth->http_get( $oauth->base_uri . "/api/smartapps/endpoints" );

DESCRIPTION

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

REGISTRATION

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:

    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

    http://localhost:8082

Then click the login link and follow the flow.

Web Client Use

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"}]

AUTHOR

Mike Schilli <cpan@perlmeister.com>

COPYRIGHT AND LICENSE

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.