Python-Bigquery客户端库。持久化客户端对象以进行多个查询

时间:2018-02-15 12:17:24

标签: python google-bigquery google-oauth

我正在使用BigQuery客户端库在python中创建一个BigQuery客户端对象,通过在Google许可屏幕上进行身份验证。为了避免让用户自己授权两次,我想重新利用客户端对象进行两次查询。我试过以下:

1)使用pickle库的Pickle客户端对象。 :

Error
  'Clients have non-trivial state that is local and unpickleable.',
_pickle.PicklingError: Pickling client objects is explicitly not supported.
Clients have non-trivial state that is local and unpickleable.

2)使用dill库腌制客户端对象。

Error
Clients have non-trivial state that is local and unpickleable.',
_pickle.PicklingError: Pickling client objects is explicitly not supported.
Clients have non-trivial state that is local and unpickleable.

3)创建了一个烧瓶会话并将客户端对象存储为会话变量。在这种情况下,对象将转换为带有错误消息的字典

'AttributeError: 'dict' object has no attribute 'query''

1 个答案:

答案 0 :(得分:1)

您无需使用oauth 2.0两次授权用户。创建令牌后,可以重复使用并刷新,更多here

此外,如果您使用flask,文档中的example将显示如何在flask会话中存储凭据并在必要时多次访问它们:

  # Store credentials in the session.
  # ACTION ITEM: In a production app, you likely want to save these
  #              credentials in a persistent database instead.
  credentials = flow.credentials
  flask.session['credentials'] = credentials_to_dict(credentials)

  # Load credentials from the session.
  credentials = google.oauth2.credentials.Credentials(
      **flask.session['credentials'])
相关问题