api - 使用集成的 Shopware6 身份验证
问题描述
使用集成的 Shopware6 身份验证有时无法正常工作以下是我的代码
$authData = array('client_id' => $client_id,
'client_secret' => $client_secret,
'scopes' => 'write',
'grant_type' => 'client_credentials',
);
$url = rtrim($url, '/') . '/api/oauth/token';
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($authData));
curl_setopt( $curl,
CURLOPT_HTTPHEADER,
['Content-Type: application/json; charset=utf-8']
);
$response = json_decode(curl_exec($curl));
echo "<pre>";
print_r($response);
exit;
它说用户凭据不正确,但凭据正确,请您帮助我提前谢谢...。
解决方案
我使用 Guzzle Http Client,一切正常。我使用下一个代码
$httpClient = new Client();
$postBody = json_encode([
'grant_type' => 'client_credentials',
'client_id' => $this->sw6AccessKey,
'client_secret' => $this->sw6SecretKey
]);
$response = $httpClient->post('http://test.com/api/oauth/token', [
'headers' => ['Content-Type' => 'application/json'],
'body' => $postBody
]);
一般来说,您的代码看起来不错。
确保凭据正确,并且在 Shopware 集成设置中启用了写入权限。
推荐阅读
- android - 错误 MSB4064:“XamarinDownloadArchives”任务不支持“AllowUnsecureUrls”参数
- mysql - 复合键 - 自动增量问题 / InnoDB MySQL 表
- c# - Azure 服务总线规则名称最大长度
- jquery - Jquery在输入的每两个字符更改时调用一个函数
- javascript - findOneAndUpdate 仅提供部分更新
- c++ - 删除全局隐式函数 - 避免模棱两可的运算符
- angular - Twin 订阅 block forkJoin 订阅
- sql - 如何根据分组将字符串组合在一列中
- rest - 搜索与特定名称值匹配的子项
- openedx - 打开 edx 登录或 SSO 实施问题