权限和融合表API

2024-06-23 19:24:22 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以使用google的pythonapi更改fusion表的权限?你知道吗

我可以使用服务帐户成功地创建表,但该表是在客户端电子邮件下创建为私有的(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()

Fusion Table API

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控制台中启用了drive 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()

Tags: comidjsonhttpexecutecreateservicetable

热门问题