Skip to content
This repository was archived by the owner on Feb 10, 2022. It is now read-only.

Commit 78ed32c

Browse files
committed
Fix refresh token in demo page
1 parent 163b247 commit 78ed32c

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

index.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,29 @@
169169

170170
if($_GET['code'] && $_GET['publish_item']) {
171171

172-
// If the code was in get parameter we authorize
173-
$user = $meli->authorize($_GET['code'], $redirectURI);
172+
// If code exist and session is empty
173+
if($_GET['code'] && !($_SESSION['access_token'])) {
174+
// If the code was in get parameter we authorize
175+
$user = $meli->authorize($_GET['code'], $redirectURI);
174176

175-
// Now we create the sessions with the authenticated user
176-
$_SESSION['access_token'] = $user['body']->access_token;
177-
$_SESSION['expires_in'] = $user['body']->expires_in;
178-
$_SESSION['refresh_token'] = $user['body']->refresh_token;
177+
// Now we create the sessions with the authenticated user
178+
$_SESSION['access_token'] = $user['body']->access_token;
179+
$_SESSION['expires_in'] = time() + $user['body']->expires_in;
180+
$_SESSION['refresh_token'] = $user['body']->refresh_token;
181+
} else {
182+
// We can check if the access token in invalid checking the time
183+
if($_SESSION['expires_in'] < time()) {
184+
try {
185+
// Make the refresh proccess
186+
$refresh = $meli->refreshAccessToken();
179187

180-
// We can check if the access token in invalid checking the time
181-
if($_SESSION['expires_in'] + time() + 1 < time()) {
182-
try {
183-
print_r($meli->refreshAccessToken());
184-
} catch (Exception $e) {
185-
echo "Exception: ", $e->getMessage(), "\n";
188+
// Now we create the sessions with the new parameters
189+
$_SESSION['access_token'] = $refresh['body']->access_token;
190+
$_SESSION['expires_in'] = time() + $refresh['body']->expires_in;
191+
$_SESSION['refresh_token'] = $refresh['body']->refresh_token;
192+
} catch (Exception $e) {
193+
echo "Exception: ", $e->getMessage(), "\n";
194+
}
186195
}
187196
}
188197

0 commit comments

Comments
 (0)