2015-09-28 72 views
0

上市的產品這是我的劇本我已經從Magento Reference使用(參見:RequestToken檢索的產品清單與OAuth認證Admin用戶)的Magento:從API

我submiting從我的本地主機此腳本。

/** 
* Example of retrieving the products list using Admin account via Magento REST API. OAuth authorization is used 
* Preconditions: 
* 1. Install php oauth extension 
* 2. If you were authorized as a Customer before this step, clear browser cookies for 'yourhost' 
* 3. Create at least one product in Magento 
* 4. Configure resource permissions for Admin REST user for retrieving all product data for Admin 
* 5. Create a Consumer 
*/ 
// $callbackUrl is a path to your file with OAuth authentication example for the Admin user 
$callbackUrl = "http://yourhost/oauth_admin.php"; 
$temporaryCredentialsRequestUrl = "http://yourhost/oauth/initiate?oauth_callback=" . urlencode($callbackUrl); 
$adminAuthorizationUrl = 'http://yourhost/admin/oAuth_authorize'; 
$accessTokenRequestUrl = 'http://yourhost/oauth/token'; 
$apiUrl = 'http://yourhost/api/rest'; 
$consumerKey = 'yourconsumerkey'; 
$consumerSecret = 'yourconsumersecret'; 

session_start(); 
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) { 
    $_SESSION['state'] = 0; 
} 
try { 
    $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI; 
    $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType); 
    $oauthClient->enableDebug(); 

    if (!isset($_GET['oauth_token']) && !$_SESSION['state']) { 
     $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl); 
     $_SESSION['secret'] = $requestToken['oauth_token_secret']; 
     $_SESSION['state'] = 1; 
     header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']); 
     exit; 
    } else if ($_SESSION['state'] == 1) { 
     $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']); 
     $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl); 
     $_SESSION['state'] = 2; 
     $_SESSION['token'] = $accessToken['oauth_token']; 
     $_SESSION['secret'] = $accessToken['oauth_token_secret']; 
     header('Location: ' . $callbackUrl); 
     exit; 
    } else { 
     $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']); 

     $resourceUrl = "$apiUrl/products"; 
     $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json')); 
     $productsList = json_decode($oauthClient->getLastResponse()); 
     print_r($productsList); 
    } 
} catch (OAuthException $e) { 
    print_r($e->getMessage()); 
    echo "<br/>"; 
    print_r($e->lastResponse); 
} 

因此,我得到了「哎呀,我們的壞...」頁面,我期待看到產品清單。 在URL中我看到oauth_token。

http://myUrl/oAuth_authorize?oauth_token=a4315776966dca4fa5d3786f4fghjkab157cf

有人能指出我了下一步該怎麼做?如果您需要任何其他數據,請詢問。

回答

0

沒有在網址中有錯字

$adminAuthorizationUrl = 'http://yourhost/admin/oAuth_authorize'; 

應該

$adminAuthorizationUrl = 'http://yourhost/admin/oauth_authorize';