Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ranveer5289/001538a20c7a51bd168a0c2fe28137cf to your computer and use it in GitHub Desktop.
Save ranveer5289/001538a20c7a51bd168a0c2fe28137cf to your computer and use it in GitHub Desktop.
#!/bin/bash
# Trusted Agent on Behalf of Demo:
#
# https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-login?meta=getTrustedAgentAuthorizationToken
# SLAS Client requires `sfcc.ta_ext_on_behalf_of` scope.
set -eou pipefail
CODE='kv7kzm78'
ORG='f_ecom_zzrf_001'
CLIENT='8eefe333-cac4-4cbe-ad1c-a7336693acbc'
SITE='RefArch'
REDIRECT='http://localhost:3000/callback'
# Agents cannot shop as themselves!
SHOPPER='[email protected]'
AGENT='[email protected]'
echo '1. Generate Code Verifier / Code Challenge...'
VERIFIER=$(
openssl rand -base64 96 | tr -d '\n' |tr '/+' '_-' | tr -d '=')
CHALLENGE=$(
echo -n $VERIFIER | openssl dgst -binary -sha256 | openssl base64 -A | tr '/' '_' | tr '+' '-' | tr -d '=')
echo '2. Get Trusted Agent Authorization URL...'
BASE="https://$CODE.api.commercecloud.salesforce.com/shopper/auth/v1/organizations/$ORG/oauth2/trusted-agent"
AUTH_URL=$(
curl "$BASE/authorize" \
-sS --get \
--data "client_id=$CLIENT" \
--data "channel_id=$SITE" \
--data "code_challenge=$CHALLENGE" \
--data-urlencode "login_id=$SHOPPER" \
--data "response_type=code" \
--data "redirect_uri=$REDIRECT" \
--data "idp_origin=ecom" \
-D- \
| grep -i 'location' | cut -d' ' -f2 | tr -d '\n\r')
echo -n "3. Opening Authorization URL in browser... come back with the code!"
open "$AUTH_URL"
read CODE
echo '3. Exchange the code for a JWT...'
curl "$BASE/token" \
-sSH "Authorization: Bearer $CODE" \
--data 'grant_type=client_credentials' \
--data "client_id=$CLIENT" \
--data 'idp_origin=ecom' \
--data-urlencode "login_id=$SHOPPER" \
--data-urlencode "agent_id=$AGENT" \
--data "channel_id=$SITE" \
--data "code_verifier=$VERIFIER" | jq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment