Access Token (B2B)
Node.js Example
Section titled “Node.js Example”import crypto from 'crypto';import fs from 'fs';
const clientId = 'YOUR_CLIENT_ID';const clientSecret = 'YOUR_CLIENT_SECRET';const privateKey = fs.readFileSync('private-key.pem', 'utf8');
// 1. Generate timestampconst timestamp = new Date().toISOString().replace('Z', '+07:00');
// 2. Build string to signconst stringToSign = `${clientId}|${timestamp}`;
// 3. Sign with RSA-SHA256const signature = crypto .sign('sha256', Buffer.from(stringToSign), privateKey) .toString('base64');
// 4. Request access tokenconst response = await fetch('https://api.snapdev.site/bca/v1.0/access-token/b2b', { method: 'POST', headers: { 'Authorization': `Basic ${btoa(`${clientId}:${clientSecret}`)}`, 'X-TIMESTAMP': timestamp, 'X-SIGNATURE': signature, 'CHANNEL-ID': '95161', },});
const { accessToken } = await response.json();console.log('Access Token:', accessToken);Python Example
Section titled “Python Example”import base64from datetime import datetimefrom cryptography.hazmat.primitives import hashes, serializationfrom cryptography.hazmat.primitives.asymmetric import paddingimport requests
client_id = "YOUR_CLIENT_ID"client_secret = "YOUR_CLIENT_SECRET"
with open("private-key.pem", "rb") as f: private_key = serialization.load_pem_private_key(f.read(), password=None)
# Timestamptimestamp = datetime.now().astimezone().isoformat()
# String to signstring_to_sign = f"{client_id}|{timestamp}"
# RSA-SHA256 signaturesignature = base64.b64encode( private_key.sign( string_to_sign.encode(), padding.PKCS1v15(), hashes.SHA256() )).decode()
# Request tokenauth = base64.b64encode(f"{client_id}:{client_secret}".encode()).decode()resp = requests.post( "https://api.snapdev.site/bca/v1.0/access-token/b2b", headers={ "Authorization": f"Basic {auth}", "X-TIMESTAMP": timestamp, "X-SIGNATURE": signature, "CHANNEL-ID": "95161", },)
token = resp.json()["accessToken"]print(f"Access Token: {token}")Token Lifetime
Section titled “Token Lifetime”- Expires in: 900 seconds (15 menit)
- Refresh: Request token baru sebelum expired
- Error code:
18(Token Expired),17(Invalid Token)