1

我正在写融合表的python包装。 我决定使用Google服务帐户访问该服务。我的代码是:使用Google服务帐户时的授权问题。未经验证的使用每日限额超过。继续使用需要注册

import httplib2 
from googleapiclient.discovery import build 
from oauth2client.service_account import ServiceAccountCredentials 

scopes = ['https://www.googleapis.com/auth/fusiontables'] 
credentials = ServiceAccountCredentials.from_json_keyfile_name('__Remak APIS-37c11e21531ad.json', scopes) 

http = httplib2.Http() 

if not credentials.access_token: 
    credentials.refresh(http) 

service = build('fusiontables', 'v2', http=http) 


def test(): 
    table_id = '1esH-YayZegZH69VsiVBq0YK9hxgP-JWTCljRuQUZy' 

    print(service.query().sql(sql='SELECT * FROM {}'.format(table_id)).execute(http=http)) 


if __name__ == '__main__': 
    test() 

输出为:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/fusiontables/v2/query?alt=json&sql=SELECT+%2A+FROM+1esH-YayrtegZH6VsiVBq0YK9hxgP-JWTCljRuQUZy returned "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."> 

我刚启用这个API,这就是为什么涨停是肯定没有达到。我也尝试在类似的主题中找到答案,但我没有成功。

预先感谢您

回答

0

最后使工作: 有一部分错过: credentials.authorize(http) 现在看来OK

的代码是:

import httplib2 

from googleapiclient.discovery import build 
from oauth2client.service_account import ServiceAccountCredentials 

scopes = ['https://www.googleapis.com/auth/fusiontables'] 
KEY = '__Remak APIS-a8cd56ca2b4e.json' 
credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY,  scopes) 
http = httplib2.Http() 

credentials.authorize(http) 

if not credentials.access_token: 
    credentials.refresh(http) 

fusiontables = build('fusiontables', 'v2', http=http) 


def test(): 
    table_id = '1esH-YayZegZH97VsiVBq0YK9hxgP-JWTCljRuQUZy' 

    print(fusiontables.query().sql(sql='SELECT * FROM  {}'.format(table_id)).execute()) 


if __name__ == '__main__': 
    test() 
相关问题