Search code examples
pythonpostgetgoogle-cloud-platform

how to pass authentication/credentials to the google cloud api post that I'm running from python


I don't know how to pass authentication/credentials to the google cloud api post that I'm running from my python program

import requests

api_endpoint = "https://dataflow.googleapis.com/v1b3/projects/projectid/templates"


data = {
  "jobName": "scriptjob2",
  "parameters": {
    "inputFilePattern": "gs://bucket/files/*.json",
    "outputTopic": "projects/project6/topics/data"
  },
  "environment": {
    "tempLocation": "gs://bucket/tmp",
    "zone": "us-central1-f"
  },
  "gcsPath": "gs://dataflow-templates/latest/GCS_Text_to_Cloud_PubSub",
  "location": "us-central1"
}

r = requests.post(url = api_endpoint, data = data)

result = r.text
print("Result:%s"%result)

I know that I'm supposed to pass a google service account authentication but I just can't figure out how to do this. This is the error message that I'm getting.

"error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other v

alid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

Solution

  • My suggestion is to use the sdk for python for dataflow.

    However, if you insist on using requests, it's as simple as:

    "https://dataflow.googleapis.com/v1b3/projects/projectid/templates&key={yourapikey}"
    

    here is a link to this in the browser for you to test with.