GCP Python - Google TeamDrive API - 权限

时间:2018-03-21 14:51:01

标签: python google-api google-cloud-platform google-directory-api google-drive-team-drive

在使用Google Team Drive API的权限部分时,我真的很感激一些建议: https://developers.google.com/drive/v3/reference/permissions

我目前正在编写一段代码,用于创建团队驱动,通过ID为teamdrive创建一些文件(mimetype文件夹),然后将用户作为一个组添加到Team Drive。

代码为使用API​​的每个人成功创建了一个团队驱动器和文件夹,但是,当我向团队驱动器添加用户时,我的响应完全不同,例如:

def build_google_teamdrive(drive_name):
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http)

team_drive_metadata = {'name': drive_name, "colorRgb": "#004e37"}
request_id = str(uuid.uuid4())

response = service.teamdrives().create(body=team_drive_metadata, requestId=request_id).execute()
logger.info('Creating Team Drive for: {}'.format(drive_name))
print(response)
return response

这会返回预期的响应:

{u'kind': u'drive#teamDrive', u'id': u'0AFlsLbuvuChJUk9PVA', u'name': u'TeamDriveName'}

这确认代码已经运行,与.files()相同。

现在,当我尝试将一个成员(组)添加到Team Drive时,我得到了一些完全不同的东西:

def test_insert():
viewer_group = 'some_user@test.domain.co.uk'
drive_unique_id = '0BKk5OjdX66ooUk9PVA'
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http)

resource = {
    "role": "reader",
    "type": "group",
    "emailAddress": viewer_group
}

response = service.permissions().create(fileId=drive_unique_id, body=resource, supportsTeamDrives=True, sendNotificationEmail=False)
print(response)
return response

返回响应:

<googleapiclient.http.HttpRequest object at 0x104e741d0>

使用权限插入API时:我期待的响应是:

{"kind": "drive#permission","id": "00723864391275245674","type": "group","role": "reader"}

如果我能对这个问题得到任何帮助,我会非常感激,因为我真的很困惑它是如何工作的。

谢谢, 的 PyJordan

1 个答案:

答案 0 :(得分:1)

查看代码,很清楚为什么响应没有按预期返回,请看这里:

response = service.teamdrives().create(body=team_drive_metadata, requestId=request_id).execute()

正如您在回复结束时所看到的,您写道:

.execute()

修复很简单,在你编写的代码中写道:

response = service.permissions().create(fileId=drive_unique_id, body=resource, supportsTeamDrives=True, sendNotificationEmail=False)

我现在将为你解决这个问题:

response = service.permissions().create(fileId=drive_unique_id, body=resource, supportsTeamDrives=True, sendNotificationEmail=False).execute()

希望这会有所帮助和好运。

VS