如何使用php获取magento的oauth访问令牌和秘密

时间:2013-08-21 05:49:27

标签: php magento oauth

我有消费者密钥和秘密。我试图使用消费者密钥和秘密来获取oauth请求令牌,但我的错误是我收到错误

  

未捕获的异常'OAuthException',消息'无效的身份验证/错误   请求(得到404,预期HTTP / 1.1 20X或重定向)'

我已尝试使用以下代码获取请求令牌,但它无法正常工作

$callbackUrl = "http://www.myhost.com/oauth_admin.php";
$temporaryCredentialsRequestUrl = "http://www.myhost.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://www.myhost.com/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://www.myhost.com/oauth/token';
$apiUrl = 'http://www.myhost.com/api/rest';
$consumerKey = 'my consumerKey';
$consumerSecret = 'my consumerSecret';

session_start();

if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
    $_SESSION['state'] = 0;
}

$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']) && !isset($_SESSION['state'])) {
    $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
   echo  $_SESSION['secret'] = $requestToken['oauth_token_secret'];
    $_SESSION['state'] = 1;
    header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
    exit;
} else if (isset($_SESSION['state']) && $_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;
}
print_r($_SESSION);//get this by next page

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

我通过更改服务器配置来解决这个问题,以采用.htaccess规则。试着检查一下。