gpt4 book ai didi

rest - Magento REST API 错误 500

转载 作者:行者123 更新时间:2023-12-05 01:11:01 25 4
gpt4 key购买 nike

我正在尝试通过 Magento REST API 获取产品信息;但导致错误开始“[message:protected] => Invalid auth/bad request (got a 500, expected HTTP/1.1 20X or a redirect)
……………………”
不知道为什么会发生这种情况;有人可以帮我吗?
我的代码如下:

$callbackUrl = "http://myhostcom/current_script.php";
$temporaryCredentialsRequestUrl = "http://myhostcom/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://myhostcom/oauth/authorize';
$accessTokenRequestUrl = 'http://myhostcom/oauth/token';
$apiUrl = 'http://myhostcom/api/rest';
$consumerKey = 'xxxxxx';
$consumerSecret = 'xxxxxxxxxxx';
echo '<pre/>';
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();
$oauthClient->disableSSLChecks();


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/465";
$oauthClient->fetch($resourceUrl);
$productsList = json_decode($oauthClient->getLastResponse());
print_r($productsList);
}
} catch (OAuthException $e) {
print_r($e);
}

谢谢。

最佳答案

尝试下面的代码它的工作

$callbackUrl = "http://myhostcom/current_script.php";
$temporaryCredentialsRequestUrl = "http://myhostcom/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://myhostcom/oauth/authorize';
$accessTokenRequestUrl = 'http://myhostcom/oauth/token';
$apiUrl = 'http://myhostcom/api/rest';
$consumerKey = 'xxxxxx';
$consumerSecret = 'xxxxxxxxxxx';
echo '<pre/>';
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();
$oauthClient->disableSSLChecks();


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/465";
$oauthClient->fetch($resourceUrl, array(), 'GET', array("Content-Type" => "application/json","Accept" => "*/*"));
//$oauthClient->fetch($resourceUrl);
$productsList = json_decode($oauthClient->getLastResponse());
print_r($productsList);
}
} catch (OAuthException $e) {
print_r($e);
}

希望对你有帮助

关于rest - Magento REST API 错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18483875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com