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() 
相關問題