不能在Python
问题描述:
INSERT到谷歌的Fusion Tables通过双向OAuth我尝试用谷歌的融合表使用OAuth 2.0的下一个代码工作的服务器到服务器(两回合):不能在Python
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()
一切都OK了,当我从表中读取,但是当我尝试插入一些数据:
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中时做错了?
UPD:
研究发现,oauth2client已过时,试图通过google.auth和请求,因为它是在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,
})
随着预览,我得到了相同的结果:成功选择和403上INSERT。
谢谢。