Search code examples
pythongoogle-fusion-tables

Permissions and the Fusion Table API


Is it possible to change permissions for a fusion table using google's python api?

I can successfully create the table using a service account but the table is created as private under the client-email ([email protected]) rather than my personal gmail account.

If not, Should I not be using ServiceAccounts for authorization? The job needs to run without human interaction (ie no pasting oauth2 links into a browser).

For completeness, I have created tables in two ways

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({...}),
    )

ANSWER DETAILS (thanks to @RodMcChesney below)

As suggested in the accepted answer below, I used the Drive API. In particular I used the drive client library.

NOTE: before creating the table or service account make sure you have enabled drive api in you google console

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

Solution

  • You can use the Drive Permissions API to change your table's permissions once you've created it.