Issue with Azure DevOps API Call using OAuth Token
Hello Community,
I'm facing an issue with an API call to Azure DevOps and would appreciate any insights or suggestions. Background: I have set up an OAuth process to authenticate with Azure DevOps. The process seems to work fine, and I am able to retrieve an access token. Here's the relevant part of my code for acquiring the token:
// MSAL Configuration and Setup
// ...
router.get("/callback", async (req, res) => {
const tokenRequest = {
code: req.query.code,
scopes: ["https://app.vssps.visualstudio.com/user_impersonation"],
redirectUri: ${process.env.VITE_API_URL}/callback
,
codeVerifier: adoCache.get(req.query.state)?.codeVerifier
};
const response = await cca.acquireTokenByCode(tokenRequest);
adoCache.set(req.query.state, {
accessToken: response.accessToken
});
});
The Issue: When I use the retrieved access token to make a call to the Azure DevOps API, it seems to be failing. Here's how I'm making the call:
const orgUrl = https://dev.azure.com/testxxx
;
const axiosInstance = axios.create({
baseURL: orgUrl,
headers: {
'Authorization': Bearer ${adoData.accessToken}
,
'Content-Type': 'application/json'
}
});
const wiql = {
query: "SELECT [System.Id], [System.Title], [System.State] FROM WorkItems"
};
try {
const response = await axiosInstance.post('/_apis/wit/wiql?api-version=7.2-preview.2', wiql);
// Further processing...
} catch (error) {
// Error handling...
}
Despite the token seemingly being valid, the API call is not successful. Instead of the expected JSON response, I seem to be getting an HTML response, suggesting that the call is being redirected to a login page.
Attempts to Resolve:
- I've verified that the token is indeed being retrieved and passed in the header.
- The scopes and request format seem to be correct as per the Azure DevOps documentation.
- I've tried different variations of the API endpoint and checked the token scopes.
I'm at a bit of a loss as to why this might be happening. Any guidance or advice on what might be going wrong or what I could try next would be greatly appreciated. Thank you!