【问题标题】:Google AppEngine to Fusion Tables with Service AccountsGoogle AppEngine 与服务帐户融合表
【发布时间】:2013-01-24 21:30:01
【问题描述】:

迁移到 /v1 Fusion Table API 的游戏迟到了,但不再拖延了。

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

我发现了另一个问题,该问题向我指出了一些有关将服务帐户与 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

有没有人有使用他们可以共享的服务帐户连接到 AppEngine 上的 Fusion Tables 的示例?或者网上有什么好看的?

谢谢

【问题讨论】:

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


    【解决方案1】:

    这确实有效。重要的部分是您必须授予应用引擎服务帐户访问您的融合表的权限。如果您正在写入,则该帐户需要写入权限。如需帮助,请参阅: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()
    

    【讨论】:

    • 感谢 Ralph 将在几天后进行测试并回复此问题。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      由于 Fusion Tables 的表由各个 Gmail 帐户拥有,而不是由与 API 控制台项目关联的服务帐户拥有,因此 AppAssertionCredentials 可能不起作用。不过,它会提出一个有趣的功能请求:

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

      【讨论】:

      • 谢谢凯瑟琳。您是否会推荐带有刷新令牌的 OAuth2 以允许 web 应用程序进行无用户访问(在初始登录后)?希望允许 cron 作业更新表。
      • 您至少可以授予对单个表的服务帐户的访问权限。虽然不确定 service.table().list()。
      【解决方案4】:

      我找到的帮助将 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

      【讨论】:

        【解决方案5】:

        如果您希望它在 Google App Engine 或 Google Compute Engine 以外的其他主机上工作(例如,从 localhost 进行测试),那么您应该使用从您可以从 service account page 生成和下载的 json 密钥文件创建的 ServiceAccountCredentials .

        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了解说明。

        【讨论】:

        • 另请参阅:Google OAuth2 for python(尤其是嵌入式幻灯片视频)和live video,了解有关在 Google App Engine 上使用 OAuth2 的更多详细信息。
        • 如果您想添加更多有价值的信息,您可以编辑您的答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多