-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_example.php
More file actions
90 lines (73 loc) · 2.47 KB
/
Copy pathadvanced_example.php
File metadata and controls
90 lines (73 loc) · 2.47 KB
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
89
90
<?php
/* advanced_example
* This shows off a few extra features of the Trackvia API. In this example, we connect to trackvia and save the
* token to the database for future use.
*
* @copyright Copyright (c) 2013, TrackVia Inc.
*/
require 'Trackvia/Api.php';
require 'Trackvia/Log.php';
use Trackvia\Api;
use Trackvia\Log;
define('CLIENT_ID', '');
define('CLIENT_SECRET', '');
define('USERNAME', '');
define('PASSWORD', '');
if(CLIENT_ID == '' || CLIENT_SECRET == '' || USERNAME == '' || PASSWORD == ''){
die("Please setup your access credentials");
}
// Create a TrackviaApi object with your clientId and secret.
// The client_id and secret are only used when you need to request a new access token.
$tv = new Api(array(
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
));
// load the saved token for this user
function load_saved_token_data()
{
//===========
// code to load the token from your database
//===========
// return array(
// 'access_token' => 'user_access_token',
// 'refresh_token' => 'user_refresh_token',
// 'expires_at' => 'expires_timestamp'
// );
}
$savedToken = load_saved_token_data();
// If there is saved token data to use, set it now
if (!empty($savedToken) && isset($savedToken['access_token'])) {
$tv->setTokenData(array(
'access_token' => $savedToken['access_token'],
'refresh_token' => $savedToken['refresh_token'],
'expires_at' => $savedToken['expires_at']
));
} else {
// No token data.
// So you need to get the user credentials for authentication.
$tv->setUserCredentials(USERNAME, PASSWORD);
}
// extra parameters to pass in for the "new_token" event callback
$extraParams = array('extra_params' => 'this can be whatever you want');
// attach a listener function for when a new token is generated so you can save it to a database
$tv->on('new_token', function ($tokenData, $extraParams) {
$accessToken = $tokenData['access_token'];
$refreshToken = $tokenData['refresh_token'];
// timestamp when the access token expires
$expiresAt = $tokenData['expires_at'];
//============
// code to save token data to your database
//============
}, $extraParams);
/*
//*********************
// Setup the logger for debugging (optional)
//*********************
$authLog = new Log($tv->getAuthentication());
$log = new Log($tv);
*/
/**
* Get a list of your apps
*/
$apps = $tv->getApps();
var_dump($apps);