Esempio n. 1
3
<?php

if ($_GET['action'] == "login") {
    session_name('testml');
    session_start();
    require '../../classes/class.meli.php';
    $Meli = new Meli('4853777712698373', 'zhBim3Li6QKCTUht49YLnhpbT66CQwGm', $_SESSION['access_token'], $_SESSION['refresh_token']);
    $URL = 'http://localhost/projects/admin/files/modules/test/test.php';
    if (!$_SESSION['code']) {
        header("Location: " . $Meli->getAuthUrl($URL, Meli::$AUTH_URL['MLA']));
        die;
    }
    if ($_GET['code'] && !$_SESSION['code']) {
        $_SESSION['code'] = $_GET['code'];
    }
    // If code exist and session is empty
    if ($_SESSION['code'] && !$_SESSION['access_token']) {
        // If the code was in get parameter we authorize
        $user = $Meli->authorize($_SESSION['code'], $URL);
        // 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;
    }
    header("Location: api_example.php");
    die;
}
?>
<!DOCTYPE html>
<html>
  <head>
Esempio n. 2
1
<?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);
     }
 }
<?php

require '../../../MercadoLivre/meli.php';
$meli = new Meli(6977659590438028, "Bod7085COHJXx00mXd09kWMoHWr6cDLD");
if ($_GET['code']) {
    $oAuth = $meli->authorize($_GET['code'], 'http://localhost/PHPSDK/examples/example_login.php');
    $_SESSION['access_token'] = $oAuth['body']->access_token;
} else {
    echo '<a href="' . $meli->getAuthUrl('http://CALLBACK_URL') . '">Login with MercadoLibre</a>';
}
//session_start();
//require '../../MercadoLivre/meli.php';
//$meli = new Meli('7591845110154875', 'QiDwHZjfJcPdVg9hYzXWv1gbneq9piY9', $_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;
//	}
//
//	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