2017-10-04 84 views
0

嗨我想使用谷歌API服务来创建服务帐户。googleapi.discovery iam创建服务帐户

这里是我当前的代码:

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() 

但是,这并不工作,我不知道如何找到预期或需要什么样的参数。

回答

0

对于任何正在挣扎的人。

检出api explorer以获取请求的格式。

例如,如果端点iam.projects.serviceAccounts.get

,你需要提供姓名=“projects/project/serviceAccounts/[email protected]

那么您的通话将看起来像:

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/[email protected]") 

希望这可以帮助别人。

1

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

+1

您应该将实际的答案代码/测试放在这里,因为该链接需要更改或移动 – James

+1

虽然此链接可能回答问题,但最好在此处包含答案的重要部分并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/18989490) – jpp