forked from EJTH/SLinkedIn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reusing-tokens.php
52 lines (42 loc) · 1.45 KB
/
reusing-tokens.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
<?php
/**
* This is just for illustrative purposes. Ideally should be a user object and based on a real database
*/
class TokenDB {
static $file = 'token.storage';
public static function getToken(){
return @file_get_contents(TokenDB::$file);
}
public static function setToken($t){
file_put_contents(TokenDB::$file,$t);
}
public static function resetToken(){
file_put_contents(TokenDB::$file,'');
}
}
include '../simplelinkedin.class.php';
//Set up the API
$ln = new SimpleLinkedIn('YOUR_API_KEY', 'YOUR_API_SECRET');
//Set the current consumer token as the one stored in our DB.
$ln->setTokenData(TokenDB::getToken());
if($ln->authorize()){
try {
//Do some OAuth requests...
$user = $ln->fetch('GET', '/v1/people/~:(firstName,lastName)');
print "Hello $user->firstName $user->lastName.";
//Update stored token.
$tokenData = $ln->getTokenData();
TokenDB::setToken($tokenData['access_token']);
} catch(SimpleLinkedInException $e){
//If token was expired or invalid, we reset and reauthorize.
if($e->getLastResponse()->status == 401){
//reset the stored token, so we can go through the authorization process
//again at page refresh
TokenDB::resetToken();
//Reauthorize..
$ln->authorize();
exit;
} else throw $e;
}
}
?>