0

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.

2
  • My hypothesis is that either the instance of the Cloud SDK running the gcloud auth print-access-token does not have the correct project configured or the API_KEY you are using is not from the same project. Do you have any restrictions with using a service account? If not, let me know and I can provide an example of how to authenticate this call with token generated from a Service Account. Commented Dec 18, 2019 at 13:14
  • stackoverflow.com/questions/50410596/… got solution from the link Commented Dec 18, 2019 at 14:15

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.