Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Bitbucket Cloud API and Node.js - Authentication issues

Andy Rouse
Contributor
June 19, 2023

I've asked this on Stack Exchange, as well, so apologies for the double post.

 

I have a fairly simple command to connect to the Bitbucket API which works for me from the command line:
curl -H @{"Authorization"="Bearer {{token}}"} -o file.txt https://api.bitbucket.org/2.0/repositories/{{account name}}/{{repo name}}/downloads/test_run_id.txt

This runs absolutely fine for me and creates the file I expect in the place I expect it in the file system. It contains a string that matches the content in the file in the Bitbucket Downloads section.

However, if I call it from a JS class with node.js, the content of the output file is the following error message:

{"type": "error", "error": {"message": "You may not have access to this repository or it no longer exists in this workspace. If you think this repository exists and you have access, make sure you are authenticated."}}

For now, the class is pretty simple (intentionally so), but I can't see where the error is. I've built node.js classes in the past which call the Bitbucket Cloud APIs without problem, so I can't tell the difference between the command line and the context of the node.js, which should really be the same?

The {{}} segments above and below are hard-coded values for now.

Is this a simple case of the different contexts (command line vs node.js) needing different authentication approaches?

"use strict";
const fs = require('fs');
const path = require('path');
const child_process = require("child_process");

class RetrieveTestResults {

    execute(){

        child_process.execSync('curl -H @{"Authorization"="Bearer {{token}}"} -o file.txt https://api.bitbucket.org/2.0/repositories/{{account name}}/{{repo name}}/downloads/test_run_id.txt');

        const resultsIdFile = path.resolve('','file.txt');

        console.log("resultsIdFile:", resultsIdFile); 
        
        const resultId = fs.readFileSync(resultsIdFile, 'utf8');

        console.log("resultId:", resultId);

    }
}

/**
 * Main entry point for the script
*/
function main() {

    const retrieveTests = new RetrieveTestResults();

    retrieveTests.execute();
}
  
main();

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events