Getting 401 error while generating bitbucket connect app jwt token I am using recommended package for js.
code block:
import * as atlassianJwt from 'atlassian-jwt';
import axios from 'axios';
function getAuthToken(
data: { sharedSecret: string; baseUrl: string; clientKey: string },
method,
resource,
) {
const installation = {
sharedSecret: data.sharedSecret,
baseUrl: data.baseUrl,
};
if (!installation) throw new Error('Installation not found');
const jwtToken = atlassianJwt.encodeSymmetric(
{
iss: data.clientKey,
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 180,
qsh: atlassianJwt.createQueryStringHash(
atlassianJwt.fromMethodAndUrl(method, resource),
),
},
installation.sharedSecret,
);
return `JWT ${jwtToken}`;
}
export const fetchBitbucketRepositories = async (data: {
sharedSecret: string;
baseUrl: string;
clientKey: string;
workspace: string;
}) => {
try {
let method = 'GET';
let resource = `/0.2/repositories/${data.workspace}`; // Include /2.0 in the resource for QSH
let token = getAuthToken(data, method, resource);
const response = await axios.get(`${data.baseUrl}${resource}`, {
headers: {
Authorization: token, // Correct header name
},
});
console.log('response: ', response);
return response.data;
} catch (error) {
console.log(error);
throw error; // It's better to throw the actual error for debugging
}
};
executing fun:
async bitbucketRepositories() {
try {
let credentials = {
key: 'hikaflow-app',
publicKey:
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBFq+6Iq5J9AZzTZQfZEba9udHTIJToJnoDvWVHk8jKZIrMrVT1oJoAec84+nBhiO/8neqvbTlD7MeIb5aTDZo8YVhBKmQuEJ5RY56EakoR4x5oILsz/Ki5O4nGWSeTCCG1hj4heVsUi77umkYG5sZyHKNO+P+SwctTH1GEBDwswIDAQAB',
clientKey:
'ari:cloud:bitbucket::app/{********-****-*******-*******-***********}/hikaflow-app',
productType: 'bitbucket',
eventType: 'installed',
sharedSecret:
'ATBCu83viLH4y5LUwJ8LRwURcdjn4vRhw******************************',
actor: {
type: 'user',
uuid: '1d6b015c-cbfe-4fe8-821f-b8fc294efaed',
account_id: '5f06b9240b38b100226bf87e',
},
principal: {
display_name: 'muhammad mudassir',
links: { self: [Object], avatar: [Object], html: [Object] },
created_on: '2021-03-22T12:30:34.628931+00:00',
is_active: true,
type: 'user',
uuid: '{1d6b015c-cbfe-4fe8-821f-b8fc294efaed}',
has_2fa_enabled: null,
properties: {},
is_staff: false,
account_id: '5f06b9240b38b100226bf87e',
nickname: 'muhammad mudassir',
account_status: 'active',
location: null,
zoneinfo: null,
organization: null,
department: null,
job_title: null,
},
user: {
display_name: 'muhammad mudassir',
links: { self: [Object], avatar: [Object], html: [Object] },
created_on: '2021-03-22T12:30:34.628931+00:00',
is_active: true,
type: 'user',
uuid: '{1d6b015c-cbfe-4fe8-821f-b8fc294efaed}',
has_2fa_enabled: null,
properties: {},
is_staff: false,
account_id: '5f06b9240b38b100226bf87e',
nickname: '<work-space>',
account_status: 'active',
location: null,
zoneinfo: null,
organization: null,
department: null,
job_title: null,
},
};
return await fetchBitbucketRepositories({
sharedSecret: credentials.sharedSecret,
baseUrl: credentials.baseApiUrl,
clientKey: credentials.clientKey,
workspace: 'Muhammad-Mudassir',
});
} catch (error) {
console.log(error);
}
}