1
votes

Question

How do i call an external REST-API with oAuth2-Authentication from an Azure DevOps Pipeline?

What i am trying to achieve

So it seems that Azure Pipelines don't report the build-status properly back to Bitbucket, when multiple builds are triggered by the same commit. Thus I tried to call the Bitbucket Cloud API manually from within the Pipeline to display the correct build-status in Bitbucket.

What i tried

The InvokeRESTAPI-Task looked promising, so i went ahead and created the required "generic" service connection (called "Bitbucket API" in the snippet below). However it seems that generic service connections only support basic auth flow?

The following taks sends a request to the correct URL, but fails with 401 - Unauthorized.

- task: InvokeRESTAPI@1
    inputs:
      connectionType: 'connectedServiceName'
      serviceConnection: 'Bitbucket API'
      method: 'POST'
      body: |
        {
          "state": "INPROGRESS",
          "key": "azure-pipeline",
          "name": "CI-Pipeline 1",
          "url": "tbd",
          "description": "Lorem ipsum dolor sit amet"
        }
      urlSuffix: 'commit/$(Build.SourceVersion)/statuses/build'
      waitForCompletion: 'false'

Is there an alternative that i missed or am i actually required to implement the oAuth-flow myself in python or powershell?

2
Hi, how about the issue? Does the answer below resolve your question, If yes, you could Accept it as an Answer , so it could help other community members who get the same issues and we could archive this thread, thanks. - LoLance

2 Answers

0
votes

Instead of Generic try to use BitBucket Cloud service connection type. It supports oAuth as well.

enter image description here

enter image description here

0
votes

Instead of using OAuth, you could also consider using App passwords.

Then you can use simple bash task to call the API via curl command:

curl -X POST -is -u <USER_NAME>:<APP_PASSWORD> \
-H 'Content-Type: application/json' \
https://api.bitbucket.org/2.0/repositories/<USER_NAME>/<REPOSITORY_SLUG>/commit/<COMMIT>/statuses/build \
-d '{
     "state": "SUCCESSFUL",
     "name": "Build key - description",
     "url": "https://<AZURE_URL_REFERENCE>",
     "description": "A general description"
    }'

For more details you can check this similar issue.