Shashikumar KL
Shashikumar KL

Reputation: 1085

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

I have written a python script in my local machine and trying to run it and getting below error:

Error

{'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 script to Build trigger

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)

I also set the project in my local machine

gcloud config set project [project-name]

please give some solution for this. Thanks in advance.

Upvotes: 0

Views: 710

Answers (1)

Shashikumar KL
Shashikumar KL

Reputation: 1085

I removed API Key from request Access-token is enough to run the above python script

Upvotes: 1

Related Questions