1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Example of retrieving an authentication token of the FitBit service. |
||
5 | * |
||
6 | * PHP version 5.4 |
||
7 | * |
||
8 | * @author David Desberg <[email protected]> |
||
9 | * @author Pieter Hordijk <[email protected]> |
||
10 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
11 | */ |
||
12 | |||
13 | use OAuth\Common\Consumer\Credentials; |
||
14 | use OAuth\Common\Storage\Session; |
||
15 | use OAuth\OAuth1\Service\FitBit; |
||
16 | |||
17 | /** |
||
18 | * Bootstrap the example. |
||
19 | */ |
||
20 | require_once __DIR__ . '/bootstrap.php'; |
||
21 | |||
22 | // Session storage |
||
23 | $storage = new Session(); |
||
24 | |||
25 | // Setup the credentials for the requests |
||
26 | $credentials = new Credentials( |
||
27 | $servicesCredentials['fitbit']['key'], |
||
28 | $servicesCredentials['fitbit']['secret'], |
||
29 | $currentUri->getAbsoluteUri() |
||
30 | ); |
||
31 | |||
32 | // Instantiate the FitBit service using the credentials, http client and storage mechanism for the token |
||
33 | /** @var FitBit $fitbitService */ |
||
34 | $fitbitService = $serviceFactory->createService('FitBit', $credentials, $storage); |
||
35 | |||
36 | if (!empty($_GET['oauth_token'])) { |
||
37 | $token = $storage->retrieveAccessToken('FitBit'); |
||
38 | |||
39 | // This was a callback request from fitbit, get the token |
||
40 | $fitbitService->requestAccessToken( |
||
41 | $_GET['oauth_token'], |
||
42 | $_GET['oauth_verifier'], |
||
43 | $token->getRequestTokenSecret() |
||
44 | ); |
||
45 | |||
46 | // Send a request now that we have access token |
||
47 | $result = json_decode($fitbitService->request('user/-/profile.json')); |
||
48 | |||
49 | echo 'result: <pre>' . print_r($result, true) . '</pre>'; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
50 | } elseif (!empty($_GET['go']) && $_GET['go'] === 'go') { |
||
51 | // extra request needed for oauth1 to request a request token :-) |
||
52 | $token = $fitbitService->requestRequestToken(); |
||
53 | |||
54 | $url = $fitbitService->getAuthorizationUri(['oauth_token' => $token->getRequestToken()]); |
||
55 | header('Location: ' . $url); |
||
56 | } else { |
||
57 | $url = $currentUri->getRelativeUri() . '?go=go'; |
||
58 | echo "<a href='$url'>Login with FitBit!</a>"; |
||
59 | } |
||
60 |