I try to work with Google Fusion Table using OAuth 2.0 for Server to Server (two-legged) by the next code:
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
scopes = ['https://www.googleapis.com/auth/fusiontables']
credentials = ServiceAccountCredentials\
.from_json_keyfile_name('***file-name***.json', scopes)
fusiontables = build('fusiontables', 'v2', credentials=credentials)
obj = fusiontables.query().sql(sql='select * from ***table-id***').execute()
Everything is OK when I read from the table, but when I try to insert some data:
obj = fusiontables.query().sql(
sql="INSERT INTO ***table-id*** (Location, Number) VALUES('Paris', 1234)")\
.execute()
I got the error:
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/fusiontables/v2/query?alt=json&sql=INSERT+INTO+***table-id***+%28Location%2C+Number%29+VALUES%28%27Paris%27%2C+1234%29 returned "Forbidden">
Question: does someone know what I am doing wrong to implement insertion into Google Fusion Table?
UPD:
Found that oauth2client is deprecated, tried to make a query by google.auth and Requests as it is described in the docs:
from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession
credentials = service_account.Credentials.from_service_account_file('***filename***.json')
if credentials.requires_scopes:
credentials = credentials.with_scopes(scopes=['https://www.googleapis.com/auth/fusiontables'])
authed_session = AuthorizedSession(credentials)
r = authed_session.post('https://www.googleapis.com/fusiontables/v2/query',
data={
"sql": "***My SQL***",
"hdrs": True,
"typed": True,
})
As the preview, I got the same result: success on SELECT and 403 on INSERT.
Thanks.
The bought ways are the right and workable. The reason was that I need to add the permission to access to the table in the site.
Add this permission by email, that you can get from the credential file.