-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.php
61 lines (45 loc) · 2.33 KB
/
index.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
<?php
require("toodledo_oauth2.php");
$toodledo = new Toodledo_OAuth2();
?>
<html>
<body>
client id: <b><?=$toodledo->clientID?></b><br />
<a href="<?= $toodledo->getAuthURL(); ?>">Authorize...</a><br /><br />
<?php
//If we have an authorization code, exchange it for an access token and refresh token
if(!empty($_REQUEST['code'])) {
echo "Exchanging authorization_code for access_token.<br />";
$tokens = $toodledo->getAccessTokenFromAuthCode($_REQUEST['code'],$_REQUEST['state']);
$access_token = $tokens['access_token']; //this will be your short-lived token to use with the API to make requests.
$refresh_token = $tokens['refresh_token']; //this will be your long-lived token to get more access_tokens when they expire
$expiration = $tokens['expires_in']; //this will tell you when the access_token expires
echo "Access Token: <b>".$access_token."</b> expires in ".$expiration." sec<br />";
echo "Refresh Token: <b>".$refresh_token."</b> <a href='?refresh=".$refresh_token."'>Use Refresh Token</a><br />";
}
//if we are using a refresh token
if(!empty($_REQUEST['refresh'])) {
echo "Exchanging refresh_token for access_token.<br />";
$tokens = $toodledo->getAccessTokenFromRefreshToken($_REQUEST['refresh']);
$access_token = $tokens['access_token']; //this will be your short-lived token to use with the API to make requests.
$refresh_token = $tokens['refresh_token']; //this will be your long-lived token to get more access_tokens when they expire
$expiration = $tokens['expires_in']; //this will tell you when the access_token expires
echo "Access Token: <b>".$access_token."</b> expires in ".$expiration." sec<br />";
echo "Refresh Token: <b>".$refresh_token."</b> <a href='?refresh=".$refresh_token."'>Use Refresh Token</a><br />";
}
//if we already have an access token
if(!empty($_REQUEST['access_token'])) {
$access_token = $_REQUEST['access_token'];
echo "Access Token: <b>".$access_token."</b><br />";
}
//if we have a valid access token, make an API call
if(!empty($access_token)) {
echo "<hr />Using access token to request user info.<br />";
$data = $toodledo->getResource("http://api.toodledo.com/3/account/get.php",$access_token);
$user = json_decode($data,true);
echo($user['email']);
echo "<br /><br /><a href='?access_token=".$access_token."'>Request resource again</a>";
}
?>
</body>
</html>