<?php session_start(); require '../MercadoLivre/meli.php'; $meli = new Meli('APP_ID', 'SECRET_KEY'); if ($_GET['code']) { // If the code was in get parameter we authorize $user = $meli->authorize($_GET['code'], 'http://localhost/PHPSDK/examples/example_login.php'); // Now we create the sessions with the authenticated user $_SESSION['access_token'] = $user['body']->access_token; $_SESSION['expires_in'] = $user['body']->expires_in; $_SESSION['refrsh_token'] = $user['body']->refresh_token; // We can check if the access token in invalid checking the time if ($_SESSION['expires_in'] + time() + 1 < time()) { try { print_r($meli->refreshAccessToken()); } catch (Exception $e) { echo "Exception: ", $e->getMessage(), "\n"; } } // We construct the item to POST $item = array("title" => "Rayban Gloss Black", "subtitle" => "Some subtitle here", "category_id" => "MLB1227", "price" => 10, "currency_id" => "BRL", "available_quantity" => 1, "buying_mode" => "buy_it_now", "listing_type_id" => "bronze", "condition" => "new", "description" => "Item:, <strong> Ray-Ban WAYFARER Gloss Black RB2140 901 </strong> Model: RB2140. Size: 50mm. Name: WAYFARER. Color: Gloss Black. Includes Ray-Ban Carrying Case and Cleaning Cloth. New in Box", "video_id" => "RXWn6kftTHY", "warranty" => "12 month by Ray Ban", "pictures" => array(array("source" => "http://upload.wikimedia.org/wikipedia/commons/f/fd/Ray_Ban_Original_Wayfarer.jpg"), array("source" => "http://en.wikipedia.org/wiki/File:Teashades.gif"))); // We call the post request to list a item echo '<pre>'; print_r($meli->post('/items', $item, array('access_token' => $_SESSION['access_token']))); echo '</pre>'; } else { echo '<a href="' . $meli->getAuthUrl('http://localhost/PHPSDK/examples/example_login.php') . '">Login using MercadoLibre oAuth 2.0</a>'; }
/** * Get Acces Token Page */ public function callback(Request $request) { $error = $request->input('error'); $error_description = $request->input('error_description'); $code = $request->input('code'); $access_token = session('access_token'); if ($error) { return view('mercadolibre::login')->with('auth', ['error' => $error, 'description' => $error_description, 'url' => \Meli::getAuthUrl(env('ML_AUTHENTICATION_URL', ''))]); } if ($code || $access_token) { if ($code && !$access_token) { $user = \Meli::authorize($code, env('ML_AUTHENTICATION_URL')); session(['access_token' => $user['body']->access_token]); session(['expires_in' => time() + $user['body']->expires_in]); session(['refresh_token' => $user['body']->refresh_token]); $me = \Meli::get('/users/me', ['access_token' => session('access_token')]); session(['profile' => $me['body']]); } else { if (session('expires_in') < time()) { try { $refresh = \Meli::refreshAccessToken(session('refresh_token')); session(['access_token' => $refresh['body']->access_token]); session(['expires_in' => time() + $refresh['body']->expires_in]); session(['refresh_token' => $refresh['body']->refresh_token]); } catch (Exception $e) { echo "Exception: " . $e->getMessage() . PHP_EOL; } } } return \Redirect::to('/meli/admin'); } else { $auth_url = \Meli::getAuthUrl(env('ML_AUTHENTICATION_URL', '')); return view('mercadolibre::login')->with('auth_url', $auth_url); } }
$meli = new Meli('APP_ID', 'SECRET_KEY', $_SESSION['access_token'], $_SESSION['refresh_token']); if ($_GET['code'] || $_SESSION['access_token']) { // If code exist and session is empty if ($_GET['code'] && !$_SESSION['access_token']) { // If the code was in get parameter we authorize $user = $meli->authorize($_GET['code'], 'http://localhost/PHPSDK/examples/example_login.php'); // Now we create the sessions with the authenticated user $_SESSION['access_token'] = $user['body']->access_token; $_SESSION['expires_in'] = time() + $user['body']->expires_in; $_SESSION['refresh_token'] = $user['body']->refresh_token; } else { // We can check if the access token in invalid checking the time if ($_SESSION['expires_in'] < time()) { try { // Make the refresh proccess $refresh = $meli->refreshAccessToken(); // Now we create the sessions with the new parameters $_SESSION['access_token'] = $refresh['body']->access_token; $_SESSION['expires_in'] = time() + $refresh['body']->expires_in; $_SESSION['refresh_token'] = $refresh['body']->refresh_token; } catch (Exception $e) { echo "Exception: ", $e->getMessage(), "\n"; } } } echo '<pre>'; print_r($_SESSION); echo '</pre>'; } else { echo '<a href="' . $meli->getAuthUrl('http://localhost/PHPSDK/examples/example_login.php') . '">Login using MercadoLibre oAuth 2.0</a>'; }
$_SESSION['expires_in'] = $UserData[0]['expires_in']; if (!$_SESSION['code']) { header("Location: " . $Meli->getAuthUrl($URL, Meli::$AUTH_URL['MLA'])); die; } else { if (!$_SESSION['access_token']) { // If the code was in get parameter we authorize $UserML = $Meli->authorize($_SESSION['code'], $URL); // Now we create the sessions with the authenticated user $_SESSION['access_token'] = $UserML['body']->access_token; $_SESSION['expires_in'] = time() + $UserML['body']->expires_in; $_SESSION['refresh_token'] = $UserML['body']->refresh_token; $DB->execQuery("UPDATE", "admin_user", "access_token='" . $_SESSION['access_token'] . "', refresh_token='" . $_SESSION['refresh_token'] . "', expires_in=" . $_SESSION['expires_in'], "admin_id=8"); //echo "AT".$DB->lastQuery(); } else { if ($_SESSION['expires_in'] < time()) { try { // Make the refresh proccess $Refresh = $Meli->refreshAccessToken(); // Now we create the sessions with the new parameters $_SESSION['access_token'] = $Refresh['body']->access_token; $_SESSION['expires_in'] = time() + $Refresh['body']->expires_in; $_SESSION['refresh_token'] = $Refresh['body']->refresh_token; $DB->execQuery("UPDATE", "admin_user", "access_token='" . $_SESSION['access_token'] . "', refresh_token='" . $_SESSION['refresh_token'] . "', expires_in=" . $_SESSION['expires_in'], "admin_id=8"); //echo "RT".$DB->lastQuery(); } catch (Exception $e) { echo "Exception: ", $e->getMessage(), "\n"; } } } }