2

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?

1
  • 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. Commented Sep 30, 2020 at 10:00

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

enter image description here

enter image description here

1 Comment

I tried that, but then the InvokeRESTAPI-Task throws an error that it requires a service connection of type "generic" :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.