渐渐边缘商业工具
一个与商务工具HTTP API合作的易用工具包: https://docs.com/api/ 商业工具
请注意: 这个 API 正在逐个建立中 。 如果你想要用一个方法 似乎不存在,请 要么提出一个问题 要么自由做出改变 {\fn黑体\fs22\bord1\shad0\3aHBE\4aH00\fscx67\fscy66\2cHFFFFFF\3cH808080}你自己 举个公关 {\fn黑体\fs22\bord1\shad0\3aHBE\4aH00\fscx67\fscy66\2cHFFFFFF\3cH808080}你
见本文全文: API 文档。 。 。 .
与官方商务工具SDK相比?
我们认为比较容易使用而较少动词. 在翻转的一面 官方商务工具 SDK 实值 更灵活,当然更完整和最新.
安装
简单安装 @gradientedge/commercetools-utils 使用软件包管理器的软件包,例如:
npm install --save @gradientedge/commercetools-utils
代码示例
为了清楚起见,我们用这个词 赠款 描述持有访问符的物体, 刷新标志、范围和过期细节.
创建客户赠款
import { Region, CommercetoolsAuth, CommercetoolsGrant } from '@gradientedge/commercetools-utils'
async function example() {
const auth = new CommercetoolsAuth({
projectKey: 'your-project-key',
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
region: Region.EUROPE_GCP,
clientScopes: ['create_anonymous_token']
})
let grant: CommercetoolsGrant
try {
const grant = await auth.getClientGrant()
console.log('Grant:', grant)
} catch (error) {
// 'error' will likely be an instance of CommercetoolsError.
// See the API for full details.
console.error(error)
throw error
}
}
example()
在客户中登录
import { Region, CommercetoolsAuth, CommercetoolsGrant } from '@gradientedge/commercetools-utils'
async function example() {
const auth = new CommercetoolsAuth({
projectKey: 'your-project-key',
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
region: Region.EUROPE_GCP,
clientScopes: ['create_anonymous_token']
})
let grant: CommercetoolsGrant
try {
const grant = await auth.login({
username: 'myUsername',
password: 'myPassword'
})
console.log('Grant:', grant)
} catch (error) {
console.error(error)
throw error
}
}
example()
注意 getClientGrant 这里的方法可以被交换出来说, login,以登录客户.
没必要打电话 getClientGrant 打电话前 login. 确保有积极的客户赠款
将会由 CommercetoolsAuth 课.
拿个匿名的客户补助金
import { Region, CommercetoolsAuth, CommercetoolsGrant } from '@gradientedge/commercetools-utils'
async function example() {
const auth = new CommercetoolsAuth({
projectKey: 'your-project-key',
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
region: Region.EUROPE_GCP,
clientScopes: ['create_anonymous_token']
})
let grant: CommercetoolsGrant
try {
const grant = await auth.getAnonymousGrant()
console.log('Grant:', grant)
} catch (error) {
console.error(error)
throw error
}
}
example()
刷新现有的客户赠款
下面的代码显示了我们如何称呼 refreshCustomerGrant 使用该方法 refreshToken 一、导 言
现有赠款。 现实地说,这笔补助金可能来自于 某种像JWT的东西 通过
您的 UI 给您的后端服务器 .
import { Region, CommercetoolsAuth, CommercetoolsGrant } from '@gradientedge/commercetools-utils'
async function example() {
const auth = new CommercetoolsAuth({
projectKey: 'your-project-key',
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
region: Region.EUROPE_GCP,
clientScopes: ['create_anonymous_token']
})
let grant: CommercetoolsGrant
try {
const grant = await auth.login({
username: 'myUsername',
password: 'myPassword'
})
console.log('Grant:', grant)
} catch (error) {
console.error(error)
throw error
}
// The 'grant' would have been
const refreshToken = grant.refreshToken
try {
const grant = await auth.refreshCustomerGrant(refreshToken)
console.log('Refreshed grant:', grant)
} catch (error) {
console.error(error)
throw error
}
}
example()