Lewati ke konten

Access Token (B2B)

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 timestamp
const timestamp = new Date().toISOString().replace('Z', '+07:00');
// 2. Build string to sign
const stringToSign = `${clientId}|${timestamp}`;
// 3. Sign with RSA-SHA256
const signature = crypto
.sign('sha256', Buffer.from(stringToSign), privateKey)
.toString('base64');
// 4. Request access token
const 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);
import base64
from datetime import datetime
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding
import 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)
# Timestamp
timestamp = datetime.now().astimezone().isoformat()
# String to sign
string_to_sign = f"{client_id}|{timestamp}"
# RSA-SHA256 signature
signature = base64.b64encode(
private_key.sign(
string_to_sign.encode(),
padding.PKCS1v15(),
hashes.SHA256()
)
).decode()
# Request token
auth = 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}")
  • Expires in: 900 seconds (15 menit)
  • Refresh: Request token baru sebelum expired
  • Error code: 18 (Token Expired), 17 (Invalid Token)