如何使用spree API v2 SDK获取Token

How to get Token with spree API v2 SDK

我有一个 Spree 3.7 应用程序,正在尝试使用 storefront API v2 to get a token. I've been recommended to use their SDK,但我不明白如何使用 SDK

示例如下:

Identifies a guest user's cart and order.

const response = await client.cart.create()

const orderToken: string = response.data.attributes.token

没有明确说明 response.data.attributes.token 的来源或获取方式。

有没有人有如何使用 SDK 获取 API 令牌的示例?目前无法这样做。据说使用 /cart 我可以获得一个访客令牌按钮,试图获得以 404

结尾的响应

const orderToken: string = response.data.attributes.token returns 数据未知。我从哪里获取数据值?

  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();

我通过在调用 data

之前添加 .succes() 设法使用 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
 })