|
169 | 169 |
|
170 | 170 | if($_GET['code'] && $_GET['publish_item']) {
|
171 | 171 |
|
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); |
174 | 176 |
|
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(); |
179 | 187 |
|
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 | + } |
186 | 195 | }
|
187 | 196 | }
|
188 | 197 |
|
|
0 commit comments