通过Azure开发操作调用REST API

i2loujxw  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(198)

我一直在努力从Azure Devops InvokeRestAPI任务连接GCP。我已经使用空凭据创建了一个服务连接。并在YAML文件中创建了一个API任务,如下所示。
当我在标头中添加“授权”时,Devops无法识别它。当我在“授权令牌”中添加不带承载者的令牌时,它失败并出现401错误,说明身份验证错误。这是我每次遇到的错误,无论我做什么。“message”:“请求缺少必需的身份验证凭据。需要OAuth 2访问令牌、登录cookie或其他有效的身份验证凭据。请参阅https://developers.google.com/identity/sign-in/web/devconsole-project."
下面是yaml代码:

- 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'
11dmarpk

11dmarpk1#

当您在Invoke Rest API任务中运行API时,您需要确保相同的令牌可以在您的本地环境中正常工作。
然后,您可以使用以下格式设置Invoke Rest API任务的标头。

"Authorization": "Bearer  bearertokendeatil "

例如:

- 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'

相关问题