0
|
1 <?php
|
|
2
|
|
3 /**
|
|
4 * Handle expired access tokens
|
|
5 *
|
|
6 * @author Xavier Barbosa
|
|
7 * @since 13 February, 2013
|
|
8 * @link https://developers.facebook.com/blog/post/2011/05/13/how-to--handle-expired-access-tokens/
|
|
9 **/
|
|
10
|
|
11 use Mute\Facebook\App;
|
|
12 use Mute\Facebook\Exception\GraphAPIException;
|
|
13
|
|
14 /**
|
|
15 * Default params
|
|
16 **/
|
|
17
|
|
18 $app_id = 'YOUR_APP_ID';
|
|
19 $app_secret = 'YOUR_APP_SECRET';
|
|
20 $my_url = 'YOUR_POST_LOGIN_URL';
|
|
21
|
|
22 /**
|
|
23 * The process
|
|
24 **/
|
|
25
|
|
26 $app = new App($app_id, $app_secret);
|
|
27
|
|
28 // known valid access token stored in a database
|
|
29 $access_token = "YOUR_STORED_ACCESS_TOKEN";
|
|
30
|
|
31 $code = $_REQUEST["code"];
|
|
32
|
|
33 // If we get a code, it means that we have re-authed the user and can get a valid access_token.
|
|
34 if ($code) {
|
|
35 $params = $app->getOAuth()->getAccessToken($code);
|
|
36 $access_token = $params['access_token'];
|
|
37 }
|
|
38
|
|
39 try {
|
|
40 // Attempt to query the graph:
|
|
41 $decoded_response = $app->get('me', array(
|
|
42 'access_token' => $access_token,
|
|
43 ));
|
|
44
|
|
45 // success
|
|
46 echo "success" . $decoded_response['name'];
|
|
47 echo $access_token;
|
|
48 } catch (GraphAPIException $e) {
|
|
49 if ($e->getType() == "GraphAPIException") {
|
|
50 // Retrieving a valid access token.
|
|
51 $dialog_url = $app->getOAuth()->getCodeURL($my_url);
|
|
52
|
|
53 echo "<script> top.location.href=" . json_encode($dialog_url) . "</script>";
|
|
54
|
|
55 }
|
|
56 else {
|
|
57 echo "other error has happened";
|
|
58 }
|
|
59 }
|
|
60
|