Google AppEngine与服务帐户的Fusion表

时间:2013-01-24 21:30:02

标签: python google-app-engine google-fusion-tables google-api-python-client

在迁移到/ v1 Fusion Table API的游戏后期,但不再延迟。

我在AppEngine上使用Python并尝试使用Google服务帐户连接到Google Fusion Tables(对于使用JSON Web令牌的服务器端应用程序,更复杂的OAuth2表兄弟)

我发现了另一个问题,指出了一些使用Google Preview 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

是否有人使用可能能够共享的服务帐户连接到AppEngine上的Fusion Tables?还是网上好玩的东西?

由于

5 个答案:

答案 0 :(得分:3)

这确实有效。重要的部分是您必须为应用引擎服务帐户授予对融合表的访问权限。如果您正在编写,则该帐户需要写访问权限。如需帮助,请参阅:https://developers.google.com/api-client-library/python/start/installation(查找入门:快速入门)

您的应用引擎服务帐户将类似于your-app-id@appspot.gserviceaccount.com

您还必须使app引擎服务帐户成为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()

答案 1 :(得分:1)

澄清Ralph Yozzo的回答:您需要在创建service_account凭据时使用的json文件添加'client_email'的值(与使用新的oauth2client库时使用ServiceAccountCredentials.from_json_keyfile_name('service_acct.json')时加载的文件相同) ,到您的表的共享对话框屏幕(单击1,然后在2中输入电子邮件地址)

click Share then enter the email address

答案 2 :(得分:0)

由于Fusion Tables的表由个别Gmail帐户拥有,而不是与API控制台项目关联的服务帐户,因此AppAssertionCredentials可能无效。但是,它会产生一个有趣的功能请求:

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

答案 3 :(得分:0)

我找到的最好的在线资源,用于帮助将Python AppEngine与使用Oauth2的Fusion Tables API连接起来 Google APIs Client Library for Python

幻灯片演示有助于理解在线样本,使用装饰器的原因。

了解是否使用应用程序的服务帐户或用户帐户进行身份验证也很有用: Using OAuth 2.0 to Access Google APIs

考虑安装Google APIs Client Library for Python

除了范围之外,Oauth2或多或少都与所有Google API不同,而不仅仅是融合表。

oauth2工作完成后,请参阅Google Fusion Tables API

答案 4 :(得分:0)

如果您希望它可以使用其他主机而不是Google App Engine或Google Compute Engine(例如来自localhost进行测试),那么您应该使用从json密钥文件创建的ServiceAccountCredentials,您可以从{{3 }}

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()

有关说明,请参阅service account page

相关问题