googleapi.discovery iam创建服务帐户

时间:2017-10-04 07:31:53

标签: python gcp

您好我想使用google api服务创建服务帐户。

这是我目前的代码:

base_url = f"https://iam.googleapis.com/v1/projects/{project}/serviceAccounts"
auth = f"?access_token={access_token}"
data = {"accountId": name,
        "serviceAccount": {
            "displayName": name
        }}
Create a service Account
r = requests.post(base_url + auth, json=data)
try:
    r.raise_for_status()
except requests.HTTPError:
    if r.status_code != 409:
        raise

这样可行,但它使用了请求包。

我想使用googleapiclient

from googleapiclient.discovery import build

credentials = GoogleCredentials.get_application_default()
api = build(service, version, credentials=credentials)

然后,在哪里可以找到有关如何使用此api对象的信息? 我试过了:

api.projects().serviceAccounts.create(name=name).execute()

但这不起作用,我不知道如何找到预期或要求的参数。

2 个答案:

答案 0 :(得分:1)

您可以找到GCP IAM API文档here。 需要的参数和值记录在那里。

答案 1 :(得分:0)

对于任何正在挣扎的人。

查看api explorer以获取请求的格式。

例如,如果端点为from googleapiclient.discovery import build credentials = GoogleCredentials.get_application_default() api = build(service, version, credentials=credentials) sa = api.projects().serviceAccounts().get(name="projects/project/serviceAccounts/sa@gsc.googleserviceaccounts.com")

您需要提供name =“projects/project/serviceAccounts/sa@gsc.googleserviceaccounts.com”

然后你的电话会是:

net/http

希望这有助于某人。