Google AppEngine 使用服务帐号接入 Fusion Tables
我现在才开始迁移到/v1 Fusion Table API,但再也等不下去了。
我在AppEngine上使用Python,想通过Google服务账户连接到Google Fusion Tables(这是一种比OAuth2更复杂的方式,适合服务器端应用,使用的是JSON Web Tokens)。
我找到另一个问题,里面提到了一些关于如何使用服务账户与Google预测API的文档。Fusion Table和Google服务账户
到目前为止,我已经得到了
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
因为Fusion Tables的表格是由个人的Gmail账户拥有的,而不是和API控制台项目关联的服务账户,所以AppAssertionCredentials可能无法使用。不过,这倒是一个有趣的功能请求:
3
这个确实是可行的。关键是你需要给应用引擎的服务账号访问你的融合表的权限。如果你要写入数据,那么这个账号需要有写入的权限。想要获取更多帮助,可以查看这个链接:https://developers.google.com/api-client-library/python/start/installation(可以找一下“入门:快速开始”部分)
你的应用引擎服务账号看起来会像这样:your-app-id@appspot.gserviceaccount.com
你还需要在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()