无法通过 Python 中的两条腿 OAuth 插入到 Google Fusion Tables 中



我尝试使用OAuth 2.0使用Google Fusion Table进行服务器到服务器(两条腿(:

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

当我从表中读取时,一切都很好,但是当我尝试插入一些数据时:

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

问题:有人知道我在实现插入谷歌融合表时做错了什么吗?

上级:

发现 oauth2client 已被弃用,尝试通过 google.auth 和请求进行查询,如文档中所述:

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上成功 403。

谢谢。

购买的方式是正确的和可行的。原因是我需要添加访问网站中表的权限。

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

最新更新