azure - 使用 Azure DevOps 调用RestAPI

标签 azure google-cloud-platform azure-devops google-oauth azure-pipelines-yaml

我一直在努力从 Azure Devops InvokeRestAPI 任务连接 GCP。 我已使用空凭据创建了服务连接。并在 YAML 文件中创建了一个 API 任务,如下所示。

当我在 header 中添加“授权”时,Devops 无法识别它。 当我在“AuthToken”中添加带有 wo Bearer 的 token 时,它失败并出现 401 错误,表示身份验证错误。 这是我每次都会遇到的错误,无论我做什么。 "message": "请求缺少所需的身份验证凭据。需要 OAuth 2 访问 token 、登录 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'

最佳答案

当您在 Invoke Rest API 任务中运行 API 时,您需要确保相同的 token 可以在您的本地环境中正常工作。

然后您可以使用以下格式设置 Invoke Rest API 任务的 header 。

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

关于azure - 使用 Azure DevOps 调用RestAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74630269/

相关文章:

python - 减少 Google OR-tools python 脚本的运行时间(使用 Google Cloud)

.net - 当某些依赖方可能不基于 .Net 时,Azure 访问控制和 WIF 是否合适

Azure Pipelines 每个拉取请求触发 2 个作业

google-cloud-platform - GCP 从现有 VM 创建实例模板

.net - Azure、.Net、Cobertura - ##[警告]找到多个文件或目录匹配项

amazon-web-services - 使用 Azure Devops 将 .NET 核心代码部署到 AWS lambda - 找不到项目路径

azure - Terraform 为 Linux 容器创建应用程序服务

azure - Windows Azure 是否通过 CUDA 或 openCL 提供高性能计算功能?

asp.net - 通过azure媒体服务.NET SDK将视频上传到azure blob存储

postgresql - 为什么 App Engine 无法连接到 Compute Engine 虚拟机实例?