-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.php
80 lines (59 loc) · 2.61 KB
/
callback.php
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
<?php
require_once 'config.php';
// Base URL
$baseUrl = 'http://api.fitbit.com';
// Request token path
$req_url = $baseUrl . '/oauth/request_token';
// Authorization path
$authurl = $baseUrl . '/oauth/authorize';
// Access token path
$acc_url = $baseUrl . '/oauth/access_token';
// Fitbit API call (get activities for specified date)
$apiCall = "http://api.fitbit.com/1/user/-/activities/apiSubscriptions/320-activities.xml";
// Start session to store the information between calls
session_start();
// In state=1 the next request should include an oauth_token.
// If it doesn't go back to 0
if ( !isset($_GET['oauth_token']) && $_SESSION['state']==1 ) $_SESSION['state'] = 0;
try
{
// Create OAuth object
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);
// Enable ouath debug (should be disabled in production)
$oauth->enableDebug();
if ( $_SESSION['state'] == 0 )
{
// Getting request token. Callback URL is the Absolute URL to which the server provder will redirect the User back when the obtaining user authorization step is completed.
$request_token_info = $oauth->getRequestToken($req_url, $callbackUrl);
// Storing key and state in a session.
$_SESSION['secret'] = $request_token_info['oauth_token_secret'];
$_SESSION['state'] = 1;
// Redirect to the authorization.
header('Location: '.$authurl.'?oauth_token='.$request_token_info['oauth_token']);
exit;
}
else if ( $_SESSION['state']==1 )
{
// Authorized. Getting access token and secret
$oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($acc_url);
// Storing key and state in a session.
$_SESSION['state'] = 2;
$_SESSION['token'] = $access_token_info['oauth_token'];
$_SESSION['secret'] = $access_token_info['oauth_token_secret'];
}
// Setting asccess token to the OAuth object
$oauth->setToken($_SESSION['token'],$_SESSION['secret']);
// Performing API call
$oauth->fetch($apiCall,null, OAUTH_HTTP_METHOD_POST);
// Getting last response
$response = $oauth->getLastResponse();
// Initializing the simple_xml object using API response
$xml = simplexml_load_string($response);
}
catch( OAuthException $E )
{
print_r($E);
}
echo 'Subscribed.';
?>