【问题标题】:how to pass authentication/credentials to the google cloud api post that I'm running from python如何将身份验证/凭据传递给我从 python 运行的谷歌云 api 帖子
【发布时间】:2018-05-30 06:35:03
【问题描述】:

我不知道如何将身份验证/凭据传递给我从我的 python 程序运行的谷歌云 api 帖子

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)

我知道我应该通过 google 服务帐户身份验证,但我就是不知道该怎么做。这是我收到的错误消息。

"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"
  }
}

【问题讨论】:

    标签: python post get google-cloud-platform


    【解决方案1】:

    这就是我能够让它工作的方式:

    from googleapiclient.discovery import build
    from oauth2client.client import GoogleCredentials
    
    credentials = GoogleCredentials.get_application_default()
    service = build('dataflow', 'v1b3', credentials=credentials)
    
    
    PROJECT = 'projectid'
    BUCKET = 'dataflow-templates'
    TEMPLATE = 'GCS_Text_to_Cloud_PubSub'
    GCSPATH="gs://{bucket}/latest/{template}".format(bucket=BUCKET, template=TEMPLATE)
    BODY = {
       "jobName": "job1",
       "parameters": {
           "inputFilePattern": "gs://bucket/files/*.json",
           "outputTopic": "projects/project6/topics/data"
       },
       "environment": {
           "tempLocation": "gs://bucket/tmp",
           "zone": "us-central1-f"
       }
    }
    
    
    request = service.projects().templates().launch(projectId=PROJECT, gcsPath=GCSPATH, body=BODY)
    response = request.execute()
    
    print(response)
    

    【讨论】:

      【解决方案2】:

      我的建议是使用 sdk for python 进行数据流。

      但是,如果你坚持使用请求,那很简单:

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

      here 是浏览器中的链接,供您测试。

      【讨论】:

      • 谢谢丹尼尔。您的建议为我指明了正确的方向。我能够通过 python sdk 完成这项工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 2021-12-20
      • 2010-10-16
      相关资源
      最近更新 更多