Perl example Method1 Client Authentication + devicelist

The Netatmo API is a set of webservices allowing developers and third parties to access Netatmo device's data.
Post Reply
gnotoix
Posts: 1
Joined: 09 Aug 2015, 17:13

Perl example Method1 Client Authentication + devicelist

Post by gnotoix » 09 Aug 2015, 17:51

Herewith I'd like to present another basic example for Method1 of Oauth2 Client Authentication written in Perl, in addition to the given PHP example shown in Netatmo's API documentation /doc/authentication/usercred
It doesn't say "Hello", but prints out the decoded data structure of the response from the device list.

Prerequisites: the following 3 Perl-modules and their dependencies need to be installed:

LWP::JSON::Tiny
OAuth::Lite2
Data::Dumper

Please google for cpan.org/modules/INSTALL.html if you don't know how
to accomplish that and in particular if Perl keeps complaining about missing modules like this:
Can't locate .... in @INC (you may need to install the module)
Netatmo-API-id, secret, username and password are hardwired in the code and thus need to be changed prior to execution !

Code: Select all

#!/usr/bin/perl

 use LWP::JSON::Tiny;
 use OAuth::Lite2::Client::UsernameAndPassword;
 use Data::Dumper;
#use OAuth::Lite2::Agent::Dump;						#uncomment this for debugging

 my $client_Obj = OAuth::Lite2::Client::UsernameAndPassword->new(
 id               => '123456789012345678901234',			#change this to your App-ID !
 secret           => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',			#change this, too !
#agent		  => OAuth::Lite2::Agent::Dump->new,			#uncomment this for debugging
 access_token_uri => q{https://api.netatmo.net/oauth2/token},   );
 
 my $access_token_Obj = $client_Obj->get_access_token(
    username => 'John_Doe',						#enter your credentials here !
    password => '********',						
    scope    => 'read_station'			     ) or die $client_Obj->errstr;

 my $user_agent = LWP::UserAgent::JSON->new;
 my $request = HTTP::Request::JSON->new (GET => "https://api.netatmo.net/api/devicelist?access_token=" .
						$access_token_Obj->access_token	  #concatenate strings
					);
 my $response = $user_agent->request($request);

 if ($response->isa('HTTP::Response::JSON')) {
	$Data::Dumper::Indent = 3;			#these two lines influence the format of
	$Data::Dumper::Terse = 1;			#the printout of the data structure
	print Dumper($response->json_content);
 }

frank_s
Posts: 11
Joined: 02 Mar 2015, 08:57
Location: Germany, Hessen
Contact:

Re: Perl example Method1 Client Authentication + devicelist

Post by frank_s » 10 Aug 2015, 21:03

Thanks gnotoix ,
very helpful for me.
Today the use of perl is not usual for many (web) developers anymore. But it is still a good choice not only for batch processes.

Post Reply

Return to “Netatmo API”