#!/usr/bin/perl -w use LWP::UserAgent; use JSON::PP; use strict; my $ua = LWP::UserAgent->new; sub get_json { my $resp = shift; my $method = shift; #warn "raw response: ".$resp->decoded_content; warn "$method: www: ".$resp->status_line unless $resp->is_success; my $json = decode_json($resp->decoded_content); die "$method: api: method call failed: ".$json->{msg} unless $json->{success}; return $json; } my %auth_request = ( app_id => "fr.itix.munin", app_name => "Munin", app_version => "0.0.1", device_name => "tournedix.itix.fr" ); my $auth_response = $ua->post("http://mafreebox.freebox.fr/api/v1/login/authorize", Content => encode_json(\%auth_request)); my $json_auth_response = get_json($auth_response, "authorize"); my $trackid = $json_auth_response->{result}->{track_id}; die "post: authorize: no trackid in response" unless defined $trackid; my $apptoken = $json_auth_response->{result}->{app_token}; print "APPTOKEN is '",$apptoken,"'\n"; print "\nNow you have to approve that apptoken on the Freebox front display !!!\n\n"; my $count = 1; do { $auth_response = $ua->get("http://mafreebox.freebox.fr/api/v1/login/authorize/$trackid"); $json_auth_response = get_json($auth_response, "polling"); print "N: $count STATUS: ",$json_auth_response->{result}->{status}, "\n"; sleep 2; $count++; } while ($json_auth_response->{result}->{status} eq 'pending'); my $status = $json_auth_response->{result}->{status}; print "Final status is '", $status, "'\n"; if ($status eq 'granted') { print "\nCongratulation ! You have a valid AppToken.\n"; print "\nYou can store the AppToken in /etc/munin/plugin-conf.d/fb\n\n"; print " [fb_*]\n env.FB_AppToken $apptoken\n\n"; print "\nThen, you will have to go on the FreeBox web interface to give the 'settings configuration' privileges to that new app token.\n"; } exit 0;