【问题标题】:Access Google spreadsheet from Google Appengine with service account : working once per hour使用服务帐户从 Google Appengine 访问 Google 电子表格:每小时工作一次
【发布时间】:2016-07-05 01:24:54
【问题描述】:

我已经根据文档在下面实现了 python 代码,以便访问可通过公共链接访问的电子表格。 它每小时工作一次。如果我在成功后执行几秒钟,我会收到一个错误:

Error opening spreadsheet no element found: line 1, column 0

假设: 访问令牌的有效期为 1 小时。因此,appengine 将在一个小时后继续进行令牌刷新,从而重置整体。

问题: 此代码为每个请求请求一个新令牌。所以我该怎么做 ?保存令牌?当我尝试使用 token_to_blob 以保存令牌时,出现错误: 范围未定义

提前感谢您的帮助!

    try :
        credentials = AppAssertionCredentials(scope=('https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds','https://docs.google.com/feeds'))
        logging.info("credentials")
        http_auth = credentials.authorize(httplib2.Http())
        authclient = build('oauth2','v2',http=http_auth)
        auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
    except Exception as details:
        logging.error("Error Google credentials %s"%details)
        return "Error"

    try :
        gd_client = gdata.spreadsheets.client.SpreadsheetsClient()
        gd_client = auth2token.authorize(gd_client)
        feed = gd_client.GetListFeed(<spreadsheetKey>,1)
    except Exception as details:
        logging.error("Error opening spreadsheet %s"%details)
        return "Error"

【问题讨论】:

  • 看看 gspread,它可以很容易地使用服务帐户访问电子表格。
  • 您能先确定错误“未找到元素”的来源吗?尝试查看this 看看是否有帮助
  • 谢谢 - 由于错误的身份验证参数,没有找到来自空答案的元素,即使电子表格是公开的

标签: python-2.7 google-app-engine google-spreadsheet-api google-oauth service-accounts


【解决方案1】:

我最终将凭据和令牌声明为全局。 在这种情况下,它可以处理多个后续请求,但 1 小时后,令牌无效。

我用 access_token_expired 方法进行了测试,但是这个方法总是返回 false。

所以,我终于系统地执行了刷新并且它起作用了。不优雅但实用。另一种选择是存储下一次刷新的时间,仅在 1 小时后刷新。

欢迎您的 cmets 选择优雅的替代品。

我没有尝试 gspread,因为其余代码已经可以用于 gdata.spreadsheets,但也许我应该尝试。

from oauth2client.contrib.appengine import AppAssertionCredentials
from oauth2client.client import Credentials
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
import httplib2

global credentials
global auth2token
try :
    credentials = AppAssertionCredentials(scope=('https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds','https://docs.google.com/feeds'))
    http_auth = credentials.authorize(httplib2.Http())
    authclient = build('oauth2','v2',http=http_auth)
    auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
except Exception as details:
    logging.error("Error Google credentials %s"%details)

class importFromSpreadsheet(webapp2.RequestHandler):
    def __importFromSpreadsheet(self,u):
        try :
            credentials._refresh(httplib2.Http())
        except Exception as details:
            logging.error("Error refreshing Google credentials %s"%details)
...
        try :
            gd_client = gdata.spreadsheets.client.SpreadsheetsClient()
            gd_client = auth2token.authorize(gd_client)
            feed = gd_client.GetListFeed(u,1)
        except Exception as details:
            logging.error("Error opening 1st spreadsheet %s"%details)
            return "Error"

【讨论】:

    最近更新 更多