Search code examples
azuregoogle-cloud-platformazure-devopsgoogle-oauthazure-pipelines-yaml

InvokeRestAPI with Azure Devops


I am continuously struggling to connect with the GCP from Azure Devops InvokeRestAPI task. I have created a service connection with empty credentials. And created a API task in YAML file as below.

When I add the 'Authorization' in header, Devops fails to recognize it. When I add the token w/wo Bearer in 'AuthToken', it fails with a 401 error, saying authentication error. This is the wrror I face everytime, no matter what I do. "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",

Here is the yaml code:

 - job: planing_df1
    pool: server
    steps:  
      - task: InvokeRESTAPI@1
        
        inputs:
          connectionType: 'connectedServiceName'
          serviceConnection: 'GCPServiceConnectionBasic'
          method: 'GET'
          headers: |
            { 
              "PlanUrl": "$(system.CollectionUri)", 
              "ProjectId": "$(system.TeamProjectId)", 
              "HubName": "$(system.HostType)", 
              "PlanId": "$(system.PlanId)", 
              "JobId": "$(system.JobId)", 
              "TimelineId": "$(system.TimelineId)", 
              "TaskInstanceId": "$(system.TaskInstanceId)", 
              "AuthToken": "ya29.a0AeTM1ie8PKbCNb3nnTJ9XFnoVlBUlgiM48XAENJIFAl-dp4gHblablabla"
            }
          urlSuffix: '/myproj/locations/europe-west4/repositories/Dataform'
          waitForCompletion: 'true'

Solution

  • When you run the API in Invoke Rest API task, you need to make sure that the same token can work fine on your local environment.

    Then you can use the following format to set the headers of the Invoke Rest API Task.

    "Authorization": "Bearer  bearertokendeatil "
    

    For example:

    - job: planing_df1
        pool: server
        steps:  
          - task: InvokeRESTAPI@1
            
            inputs:
              connectionType: 'connectedServiceName'
              serviceConnection: 'GCPServiceConnectionBasic'
              method: 'GET'
              headers: |
                { 
                  "PlanUrl": "$(system.CollectionUri)", 
                  "ProjectId": "$(system.TeamProjectId)", 
                  "HubName": "$(system.HostType)", 
                  "PlanId": "$(system.PlanId)", 
                  "JobId": "$(system.JobId)", 
                  "TimelineId": "$(system.TimelineId)", 
                  "TaskInstanceId": "$(system.TaskInstanceId)", 
                  "Authorization": "Bearer  bearertokendeatil"
                }
              urlSuffix: '/myproj/locations/europe-west4/repositories/Dataform'
              waitForCompletion: 'true'