使用 OAuth 访问 3dCart api

Using OAuth to access 3dCart api

正在尝试在 3dcart

中授权 'app'

api 使用 Swagger 得到了很好的记录,并且他们有此页面和说明 https://apirest.3dcart.com/v1/getting-started/index.html#getting-started

您应该进行第一次调用,这应该会为您提供在后续步骤中使用的令牌

https://apirest.3dcart.com/oauth/authorize?
Client_Id=22613fdfc5a6200bece02a29524XXXXX
&state=12345
&response_type=code
&store_url=www.***********.com
&redirect_uri=www.!!!!!!!!!!.com     <==============  WHAT SHOULD THIS BE

使用邮递员,我可以玩各种参数。

我相信 clientId 应该是与订阅了您的应用程序的每个商店相关联的 guid-ish 字符串。

商店Url - 我知道这个。我应该在字符串中包含 https:// 协议吗?

听起来 STATE 只是您传入的内容,商店会鹦鹉学舌地返回给您。

redirect_uri - 不确定这是/应该是什么

我可以获得状态 200 OK,但我得到的是 HTML 页面。 在浏览器中粘贴相同的 URL,我得到一个登录页面。我以管理员用户身份登录,然后收到 404,因为它会将我重定向到 redirect_uri

???我真的只想登录。我尝试传入空白 uri 并省略参数。两者都产生

Status 400 Error 101  redirect_uri is required
Am I doing this correctly? Is there an example of authorizing so you can use the API?

我已经下载了 GIT 存储库。这有点帮助,但仍然缺少一些东西

蒂亚

使用您所说的 URL 将允许客户授予对您的应用程序的访问权限。您让客户点击的 URL 中的重定向 URI 必须与您在 3dcart Developer 中的应用程序中设置的重定向 URI 相匹配。

之后,通过 5 个步骤获取授权令牌,然后使用它与 3dcart 一起工作API。

重定向 URI 是 3dcart 将发送代码的端点。

第一步:
设置您的重定向 URI:在 3dcart Developers

应用程序的 Oauth 选项卡下

第 2 步: 3dcart 将 post 给这个 URL 一个“代码”。您可以使用 GET

从 URL 获取此信息
$code = $_GET['code'];

第 3 步: 使用 Curl(或其他)通过代码 post 返回 3dcart

$ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://apirest.3dcart.com/oauth/token");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

            $data = array('Code' => $code, 'client_id' => '{your public app key}', 'client_secret' => '{your private app key}', 'grant_type' => 'authorization_code');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'application/x-www-form-urlencoded'
            ));
            $response = curl_exec($ch);

第 4 步: 解析响应以获得 access_token:

$info = json_decode($response, true);
$access_token = $info['access_token'];

第 5 步: 使用访问令牌在 curl

中使用以下参数调用 3dcart
$auth = array(
"Content-Type: application/xml",
"Accept: application/json",
"Authorization: Bearer $access_token",
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://apirest.3dcart.com/3dCartWebAPI/v2/{API END POINT}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $auth);
$response = curl_exec($ch);
curl_close($ch);