【问题标题】:How to get Token with spree API v2 SDK如何使用 spree API v2 SDK 获取 Token
【发布时间】:2019-05-08 13:01:40
【问题描述】:

我有一个 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();

【问题讨论】:

    标签: spree spree-auth-devise


    【解决方案1】:

    我已经通过在调用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
     })
    

    【讨论】:

      猜你喜欢
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多