首页 > 解决方案 > 使用集成的 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;

它说用户凭据不正确,但凭据正确,请您帮助我提前谢谢...。

标签: apicurloauth-2.0shopware

解决方案


我使用 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 集成设置中启用了写入权限。


推荐阅读