-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy path01-guest-shopper-auth-token.ts
More file actions
57 lines (51 loc) · 1.92 KB
/
01-guest-shopper-auth-token.ts
File metadata and controls
57 lines (51 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
/**
* Get authorization token for a guest user
* Usage: ts-node examples/01-guest-shopper-auth-token.ts
* For more information, see (Shopper Login and API Access Service)[https://developer.commercecloud.com/s/api-details/a003k00000VWfNDAA1/commerce-cloud-developer-centershopperloginandapiaccessservice].
*/
import { ClientConfig, Customer } from "commerce-sdk";
// demo client credentials, if you have access to your own please replace them below.
// do not store client secret as plaintext. Store it in a secure location.
const CLIENT_ID = "da422690-7800-41d1-8ee4-3ce983961078";
const CLIENT_SECRET = "D*HHUrgO2%qADp2JTIUi";
const ORG_ID = "f_ecom_zzte_053";
const SHORT_CODE = "kv7kzm78";
const SITE_ID = "RefArch";
// client configuration parameters
const clientConfig: ClientConfig = {
parameters: {
clientId: CLIENT_ID,
organizationId: ORG_ID,
shortCode: SHORT_CODE,
siteId: SITE_ID,
},
};
/**
* Get the shopper or guest JWT/access token, along with a refresh token, using client credentials
* @returns guest user authorization token
*/
async function getGuestUserAuthToken(): Promise<Customer.ShopperLogin.TokenResponse> {
const credentials = `${CLIENT_ID}:${CLIENT_SECRET}`;
const base64data = Buffer.from(credentials).toString("base64");
const headers = { Authorization: `Basic ${base64data}` };
const client = new Customer.ShopperLogin(clientConfig);
return await client.getAccessToken({
headers,
body: {
grant_type: "client_credentials",
},
});
}
getGuestUserAuthToken()
.then((shopperToken) =>
console.log(`Authorization token: ${shopperToken.access_token}`)
)
.catch((error) => {
console.log(`Error fetching token for the guest user: ${error}`);
});