-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitbit-gettoken
executable file
·88 lines (68 loc) · 2.34 KB
/
fitbit-gettoken
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env perl
#use strict;
#use warnings;
use Data::Dumper;
use Storable;
use LWP::UserAgent;
require LWP::Authen::OAuth;
$Storable::Deparse = 1;
my $ua = LWP::Authen::OAuth->new(
'oauth_consumer_key' => '3f7661c5e85d4e61821d5d4503753bd4',
'oauth_consumer_secret' => 'cbb04dd0b755489f9378e8be12217e41',
);
#$ua->cookie_jar(
# {
# 'file' => "$ENV{HOME}/.cookies.txt"
# }
#);
#$ua->timeout(10);
#$ua->env_proxy;
# Allow posts to redirect.
push @{ $ua->requests_redirectable }, 'POST';
my $request = $ua->post( 'https://api.fitbit.com/oauth/request_token',
);
die $request->as_string if $request->is_error;
# update the token secret from the HTTP response
$ua->oauth_update_from_response( $request );
# Open a browser to get our pin.
# Data is returned as form-encoded
my $uri = URI->new( 'http:' );
$uri->query( $request->content );
my %oauth_data = $uri->query_form;
my $authorize_url='http://www.fitbit.com/oauth/authorize?oauth_token='.$oauth_data{'oauth_token'};
`open $authorize_url`;
print "Please enter the pin you got from the fitbit page.\n";
my $pin = <>;
chomp($pin);
$pin =~ s/\n//;
die('You must enter the pin!') unless $pin;
# turn the 'request' token into an 'access' token with the verifier
# returned by fitbit
$request = $ua->post( 'https://api.fitbit.com/oauth/access_token',
[
# 'oauth_token' => $oauth_data{'oauth_token'},
'oauth_token_secret' => $oauth_data{'oauth_token_secret'},
'oauth_verifier' => $pin,
],
);
print "below should be my token data:\n";
die $request->as_string; # if $request->is_error;
#print "
#\$params = { 'oauth_consumer_key' => '3f7661c5e85d4e61821d5d4503753bd4',
#'oauth_consumer_secret' => 'cbb04dd0b755489f9378e8be12217e41',
#'oauth_token_secret' => '$oauth_data{'oauth_token_secret'}',
#'oauth_access_token' => $oauth_data{'oauth_access_token'},
#'oauth_verifier' => '$pin', }\;
#";
# update the token secret from the HTTP response
$ua->oauth_update_from_response( $request );
#print Dumper(%oauth_data);
#print Dumper($ua);
store $ua, './fitbitagent';
my $response_format = 'json';
my $api_version = '1';
my $api_base_url = "https://api.fitbit.com";
my $info_get_url = "/$api_version/user/-/profile.$response_format";
my $activity_post_url = "/$api_version/user/-/activities.$response_format";
$request = $ua->post( $api_base_url.$info_get_url);
die $request->as_string;