duan6161 2015-10-29 02:13
浏览 89

如何连接到我自己的站点的Magento REST API

I have a new Magento site where I do not have direct control over the server but do have admin access to the site. I would very much appreciate if someone can explain what I'm doing wrong and/or the simplest way to connect to my own site.

I have configured all Web Service settings as per http://devdocs.magento.com/guides/m1x/api/rest/authentication/oauth_authentication.html#OAuthAuthentication-PHPExamples

I have the consumer key and consumer secret. For convenience I'm pasting the sample PHP login code provided by Magento. I'm using it exactly as provided other than subbing in my domain and credentials.

<?php
/**
 * 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 "&lt;br/&gt;";
    print_r($e->lastResponse);
} 

The error I receive is

making the request failed (Couldn't resolve host name)&lt;br/&gt;gt;

It's failing at this line:

$requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);

Apparently because the $callbackUrl I defined is invalid. Well, what should be here? Can it be any URL? Must it sit on the same server as the site?

  • 写回答

1条回答 默认 最新

  • du9843 2016-11-28 08:00
    关注
    /**
     * 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
    
    $baseUrl = 'http://yourhost.abc';
    $scriptName = $_SERVER['SCRIPT_NAME'];
    $callbackUrl = 'http://scripthost.xyz' . $scriptName;
    $temporaryCredentialsRequestUrl = $baseUrl."/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
    $adminAuthorizationUrl = $baseUrl.'/admin/oauth_authorize';
    $customerAuthorizationUrl = $baseUrl.'/oauth/authorize';
    $accessTokenRequestUrl = $baseUrl.'/oauth/token';
    $apiUrl = $baseUrl.'/api/rest';
    $consumerKey = 'Your API consumer key';
    $consumerSecret = 'Your API consumer key';
    
    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: ' . $customerAuthorizationUrl . '?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(json_encode($oauthClient->getLastResponse()), FALSE);
    
            echo $productsList;
        }
    } catch (OAuthException $e) {
        print_r($e->getMessage());
        echo "&lt;br/&gt;";
        print_r($e->lastResponse);
    }
    
    ?>
    

    /** Also Callback URL is same as your PHP calling script. In other words you have to redirect to this script. Also your callback script and your server, both should on live server or on local server. */

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况