如何 运行 REST API 在 google 云构建中构建触发器

How to run REST API to build trigger in google cloud build

我在我的本地机器上写了一个 python 脚本并尝试 运行 它并遇到以下错误:

错误

{'error': {'code': 400,
           'details': [{'@type': 'type.googleapis.com/google.rpc.Help',
                        'links': [{'description': 'Google developer console '
                                                  'API key',
                                   'url': 'https://console.developers.google.com/project/[project_id]/apiui/credential'}]}],
           'message': 'The API Key and the authentication credential are from '
                      'different projects.',
           'status': 'INVALID_ARGUMENT'}}

python 构建触发器的脚本

bashCommand = "gcloud auth print-access-token"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
if error:
    print(error)


headers = {
    'Authorization' : 'Bearer '+str(output)[2:-3],
    'Accept' : 'application/json',
    'Content-Type' : 'application/json'
}


cloudbuild = {"build":
                {"source":
                    {"repoSource":
                        {"projectId":"[PROJECT_ID]",
                         "repoName":"[repoName]",
                         "branchName":".*"
                         }
                    }
                },
              "description":"API TRigger for all branch",
              "name":"[TRIGGER NAME]"
              }

data = json.dumps(cloudbuild)

response = requests.post('https://cloudbuild.googleapis.com/v1/projects/[PROJECT_ID]/triggers?key=[API KEY]', headers=headers, data=data)
results_output = response.json()
pprint(results_output)

我也是在本地机器设置项目

gcloud config set project [project-name]

请为此提供一些解决方案。 提前致谢。

我从请求中删除了 API 密钥 访问令牌足以运行上面的python脚本