在Python中不能通过两条腿的OAuth插入到googlefusion表中

2024-06-23 18:46:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用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">

问:有人知道我在googlefusion表中实现插入操作的错误吗?你知道吗

升级版:

发现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。你知道吗

谢谢。你知道吗


Tags: fromhttpsimportcomauthjsonsqlwww
1条回答
网友
1楼 · 发布于 2024-06-23 18:46:58

买的方法是正确的和可行的。原因是我需要添加访问site中表的权限。你知道吗

通过电子邮件添加此权限,您可以从凭证文件中获得此权限。你知道吗

相关问题 更多 >

    热门问题