Hi, This is first time posting in here.
Now our app is using In-app purchase and i'm implementing server logics using app-store-server-api.
But from yesterday, i'm facing 'http status code 401 Unauthorized error' when calling some apis.
I did many trials to fix this error and issue new key for api in app store connect. But I can't resolve this error. Even can't know what is the reason.
I attach my real code for calling 'get transaction info' api. Please give some help for me!!!!
const jwt = require('jsonwebtoken');
const fs = require('fs');
const axios = require('axios');
getApiJWTToken = () => {
// header
const keyId = process.env.APP_STORE_KEY_ID;
const issuerId = process.env.APP_STORE_ISSUER_ID;
const appBundleId = process.env.APP_BUNDLE_ID;
const header = {
alg: 'ES256',
kid: keyId,
typ: 'JWT',
};
// payload
const issuedAt = Number(dayjs().unix());
const payload = {
iss: issuerId,
iat: issuedAt,
aud: 'appstoreconnect-v1',
bid: appBundleId,
};
// sign
const keyString = fs.readFileSync(filePath, 'utf-8');
const token = jwt.sign(payload, keyString, {
algorithm: 'ES256',
header: header,
expiresIn: '30m',
});
return btoa(token);
};
requestSingleTransactionInfo = async ({ transactionId }) => {
try {
// jwt token
const token = this.getApiJWTToken();
const envType = process.env.APP_STORE_SERVER_API_ENV;
const commonUrl = `/inApps/v1/transactions/${transactionId}`;
let baseUrl;
if (envType === 'Sandbox') {
// sandbox
baseUrl = 'https://api.storekit-sandbox.itunes.apple.com';
} else if (envType === 'Production') {
// production
baseUrl = 'https://api.storekit.itunes.apple.com';
}
const url = `${baseUrl}${commonUrl}`;
const response = await axios.get(url, {
headers: {
Authorization: `Bearer ${token}`,
},
});
const { signedTransactionInfo } = response.data;
const transactionInfo = await decodeTransaction(signedTransactionInfo);
console.log(transactionInfo);
return transactionInfo;
} catch (err) {
throw err;
}
};