如何使用Spree API v2 SDK获取令牌

时间:2019-05-08 13:01:40

标签: spree spree-auth-devise

我有一个Spree 3.7应用程序,正在尝试使用storefront API v2获取令牌。建议我使用他们的SDK,但我不了解如何使用SDK

以下示例:

  

标识来宾用户的购物车和订单。

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返回未知数据。我从哪里获得数据值?

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

1 个答案:

答案 0 :(得分:1)

在调用.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
 })
相关问题