2

在遷移到/ v1 Fusion Table API但沒有再等待的時候,遊戲已經到了最後。谷歌AppEngine到服務帳戶的Fusion Tables

我使用AppEngine上的Python,並試圖連接到與谷歌服務帳戶(OAuth2用戶對服務器端的應用程序更復雜的表妹使用JSON網絡令牌)

谷歌的Fusion Tables我發現指出了另一個問題我想了解一些使用Google預測API使用服務帳戶的文檔。 Fusion Table and Google Service Accounts

到目前爲止,我已經得到了

import httplib2 
from oauth2client.appengine import AppAssertionCredentials 
from apiclient.discovery import build 

credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/fusiontables') 
http = credentials.authorize(httplib2.Http(memcache)) #Http(memcache) 
service = build("fusiontables", "v1", http=http) 

# list the tables 
tables = service.table().list().execute() # <-- ERROR 401 invalid credentials here 

沒有人有使用服務帳戶,他們可能可以共享連接到Fusion Tables AppEngine上的例子嗎?或者網上有什麼好的?

感謝

回答

0

由於融合表表是由個人Gmail帳戶,而不是一個API控制檯項目相關聯的服務帳戶擁有的AppAssertionCredentials可能不會工作。它將使一個有趣的功能要求,但:

http://code.google.com/p/fusion-tables/issues/list

+0

謝謝凱瑟琳。你會推薦帶有刷新令牌的OAuth2允許webapp進行無用戶訪問(在初始登錄後)嗎?希望允許cron作業更新表格。 – base2john

+0

您至少可以授予對各個表的服務帳戶的訪問權限。不知道有關service.table()。列表()雖然。 – abeyer

3

這實際上做的工作。重要的部分是你必須讓應用程序引擎服務帳戶訪問你的融合表。如果您正在編寫該帳戶,則需要寫入權限。如需幫助請參見:https://developers.google.com/api-client-library/python/start/installation(找入門:快速入門)

你的App Engine服務帳戶將會像[email protected]

還必須使App Engine服務帳戶api控制檯中的一名團隊成員,並授予它「可編輯」權限。

SCOPE='https://www.googleapis.com/auth/fusiontables' 
PROJECT_NUMBER = 'XXXXXXXX' # REPLACE WITH YOUR Project ID 

# Create a new API service for interacting with Fusion Tables 
credentials = AppAssertionCredentials(scope=SCOPE) 
http = credentials.authorize(httplib2.Http()) 
logging.info('QQQ: accountname: %s' % app_identity.get_service_account_name()) 
service = build('fusiontables', 'v1', http=http, developerKey='YOUR KEY HERE FROM API CONSOLE') 

def log(value1,value2=None): 
    tableid='YOUR TABLE ID FROM FUSION TABLES' 
    now = strftime("%Y-%m-%d %H:%M:%S", gmtime()) 
    service.query().sql(sql="INSERT INTO %s (Temperature,Date) values(%s,'%s')" % (tableid,value1,now)).execute() 
+0

謝謝拉爾夫會在幾天內測試並回到這個問題。 – base2john

0

最好的在線資源,我發現爲Python的AppEngine連接至Fusion Tables API使用的oauth2幫助 Google APIs Client Library for Python

的幻燈片演示有助於理解在線樣本,爲什麼使用裝飾。

也是理解是否使用應用程序的服務Acount或用戶帳戶進行身份驗證有用的是: Using OAuth 2.0 to Access Google APIs

考慮從範圍安裝Google APIs Client Library for Python

除此之外,該的OAuth2就是都或多或少常見Google API不僅僅是融合表格。

一旦的oauth2工作,看到Google Fusion Tables API

1

澄清拉爾夫Yozzo的回答是:你需要從當您創建service_account憑證(您加載相同的文件,你下載的JSON文件添加「client_email」的價值使用ServiceAccountCredentials.from_json_keyfile_name('service_acct.json')當新oauth2client庫),你的表的共享對話框屏幕(單擊1,然後在2輸入電子郵件地址)

click Share then enter the email address

0

如果你希望它從另一臺主機比谷歌App Engine的工作或Google Compute En gine(例如從localhost進行測試),那麼您應該使用從json密鑰文件創建的ServiceAccountCredentials,您可以從service account page生成並下載該文件。

scopes = ['https://www.googleapis.com/auth/fusiontables'] 
keyfile = 'PATH TO YOUR SERVICE ACCOUNT KEY FILE' 
FTID = 'FUSION TABLE ID' 

credentials = ServiceAccountCredentials.from_json_keyfile_name(keyfile, scopes) 
http_auth = credentials.authorize(Http(memcache)) 
service = build('fusiontables', 'v2', http=http_auth) 

def insert(title, description): 
    sqlInsert = "INSERT INTO {0} (Title,Description) values('{1}','{2}')".format(FTID, title, description) 
    service.query().sql(sql=sqlInsert).execute() 

請參閱Google's page on service accounts的解釋。

+0

另請參閱: [Google OAuth2 for python](https://developers.google.com/api-client-library/python/guide/aaa_oauth)(尤其是嵌入式幻燈片視頻)和 [實時視頻](https ://www.youtube.com/watch?v = HoUdWBzUZ-M) 瞭解有關在Google App Engine上使用OAuth2的更多詳細信息。 –

+1

如果你想添加更多有價值的信息,你可以編輯你的答案。 – zajonc