是否可以使用google的python api更改融合表的权限?
我可以使用服务帐户成功创建表,但该表在 client-email (xxxx@gce-play.iam.gserviceaccount.com)下创建为私有,而不是我的个人Gmail帐户。
如果没有,我是否应该使用ServiceAccounts进行授权?这项工作需要在没有人工干预的情况下运行(即没有将oauth2链接粘贴到浏览器中)。
为了完整起见,我以两种方式创建了表格
Fusion Table Python Client Library
def create_service(service='fusiontables',version='v2'):
credentials = ServiceAccountCredentials.from_json_keyfile_name(keyfile, scopes)
http = httplib2.Http()
credentials.authorize(http)
return discovery.build(service, version, http=http)
service = create_service()
service.table().insert(body={...}).execute()
http = httplib2.Http()
http = credentials.authorize(http)
http.request(
uri='https://www.googleapis.com/fusiontables/v2/tables',
method='POST',
headers={'Content-Type': 'application/json; charset=UTF-8'},
body=json.dumps({...}),
)
回答详情(感谢下面的@RodMcChesney)
正如下面接受的答案所示,我使用了Drive API。特别是我使用了drive client library。
注意:在创建表格或服务帐户之前,请确保您已在Google控制台中启用了驱动器API
ft_scope = 'https://www.googleapis.com/auth/fusiontables'
drive_scope = 'https://www.googleapis.com/auth/drive'
scopes = [ft_scope,drive_scope]
keyfile='sa_key.json'
# create drive and fusion table service (using method from above)
ft=create_service()
drive = create_service('drive')
# create table
ft.table().insert(body={...}).execute()
# get table id
id=drive.files().list().execute()['items'][0]['id']
# add permissions for 'anyone'
permission={'type': 'anyone','role': 'reader'}
drive.permissions().insert(fileId=id,body=permission).execute()