如何使用Spree API V2 SDK获得令牌



我有一个Spree 3.7应用程序,并且正在尝试使用店面API V2获取令牌。我被建议使用他们的SDK,但是我不了解如何使用SDK

下面的示例:

识别客人用户的购物车和订单。

const response = await client.cart.create()
const orderToken: string = response.data.attributes.token

尚未清楚地说明response.data.attributes.token来自何处或如何获得它。

有人有一个例子如何使用SDK获取API令牌吗?目前无法这样做。有人说,使用/购物车可以得到一个访客令牌按钮,试图以404

结束响应

const orderToken: string = response.data.attributes.token将数据返回为未知的数据。我从哪里获取数据值?

  import { makeClient } from '@spree/storefront-api-v2-sdk/dist/client';

async function asyncCall() {
  console.log('calling');
  // When using the SDK in a <script> tag or as part of a Webpack bundle
  // targeted for the browser, instead use:
  // import { makeClient } from '@spree/storefront-api-v2-sdk/dist/client'
  const client = makeClient({
    host: 'https://stern-telecom-react-salman15.c9users.io/'
  });
  console.log(client)
  const createCart = await client.cart.create()

  const orderToken = response.data.attributes.token
  const addToCart = await client.cart.addItem({ orderToken }, {
    variant_id: '1',
    quantity: 1
  })
  console.log('orderToken',orderToken,'createCart',createCart);
  // expected output: 'resolved'
}
asyncCall();

我设法通过添加.succes()在调用data

之前使用SDK获取API令牌
const client = makeClient({
   host: 'yourwebsite or localhost:3000'
 });
 console.log('cliet',client);
const cartCreateResponse = await client.cart.create()
 console.log('cartCreateResponse',cartCreateResponse.success().data);

const orderToken = cartCreateResponse.success().data.attributes.token
 console.log('orderToken', orderToken);
 const addToCart = await client.cart.addItem({ orderToken }, {
   variant_id: '1',
   quantity: 1
 })

最新更新