我一直在努力从 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/