2
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。
謝謝。