无法通过 Python 中的两条腿 OAuth 插入 Google 融合表

Can't INSERT into Google Fusion Tables by two-legged OAuth in Python

我尝试与 Google Fusion Table 一起工作,使用 OAuth 2.0 进行服务器到服务器(双足),下一个代码:

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

当我从 table 读取时一切正常,但是当我尝试插入一些数据时:

obj = fusiontables.query().sql(
    sql="INSERT INTO ***table-id*** (Location, Number) VALUES('Paris', 1234)")\
    .execute()

我收到错误:

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">

问题:有人知道我在执行插入 Google Fusion Table 时做错了什么吗?

更新:

发现oauth2client已弃用,尝试通过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,
})

作为预览,我得到了相同的结果:SELECT 上的成功和 INSERT.[=17 上的 403 =]

谢谢。

买来的方法是对的,也是可行的。原因是我需要在site中添加访问table的权限。

通过电子邮件添加此权限,您可以从凭据文件中获取。