Python Google API类设计中的AccessTokenRefresh错误
我试图为我的GoogleAPI调用的基本锅炉板初始化创建一个基类。通过阅读在线教程,我认为http传输对象应该在每次调用时重新使用,而不是重新初始化 一些基本信息: 平台:谷歌的AppEngine-Python2.7,支持线程 错误: 我一定是误解了什么,或者我的设计有缺陷。当我为我想要与BigQuery&Storage接口的每个API指定一个单独的类时,它工作得非常好。我只是想在类之间共享构造函数 这种方法在本地运行得非常好,尽管它使用的是不同的凭证机制 此类的实现示例:Python Google API类设计中的AccessTokenRefresh错误,python,google-app-engine,google-api-python-client,Python,Google App Engine,Google Api Python Client,我试图为我的GoogleAPI调用的基本锅炉板初始化创建一个基类。通过阅读在线教程,我认为http传输对象应该在每次调用时重新使用,而不是重新初始化 一些基本信息: 平台:谷歌的AppEngine-Python2.7,支持线程 错误: 我一定是误解了什么,或者我的设计有缺陷。当我为我想要与BigQuery&Storage接口的每个API指定一个单独的类时,它工作得非常好。我只是想在类之间共享构造函数 这种方法在本地运行得非常好,尽管它使用的是不同的凭证机制 此类的实现示例: class BigQ
class BigQueryClient(BaseGoogleAPIClient):
@classmethod
def get_scope(cls):
return 'https://www.googleapis.com/auth/bigquery'
@property
def serviceName(self):
return 'bigquery'
@property
def version(self):
return 'v2'
#METHODS REMOVED
我有没有做错什么?我的设计有缺陷吗?有没有更好的方法来实现我想做的事情?我想我不必每次都构建服务接口,但如果不构建,它会是线程安全的吗?这个方法是线程安全的吗
任何帮助都将不胜感激 试着在不穿这些衣服的情况下让它发挥作用。刷新错误可能只是意味着您没有有效的刷新令牌。在我尝试创建基类之前,它正在工作
import os, httplib2, abc, logging
from apiclient.discovery import build
from oauth2client.appengine import AppAssertionCredentials
from oauth2client.client import Storage, Credentials
from google.appengine.api import memcache
http = None
class FileStorage(Storage):
def __init__(self, filepath):
self._filepath = filepath
def locked_get(self):
with open(self._filepath, 'r') as f:
json = f.read()
credentials = Credentials.new_from_json(json)
return credentials
class BaseGoogleAPIClient(object):
__metaclass__ = abc.ABCMeta
"""The following is not ideal, would prefer abstract class properties"""
@classmethod
def get_scope(cls):
raise NotImplementedError("Must define the authorization scope required for this class (multiple scopes should be seperated by a space)")
@abc.abstractproperty
def serviceName(self):
"""Define the Service this class will interface with"""
return
@abc.abstractproperty
def version(self):
"""Define the Version of the API we will interface with"""
return
def __init__(self):
global http
if not http:
if not os.environ['SERVER_SOFTWARE'].startswith('Development'):
scopes = []
for sc in BaseGoogleAPIClient.__subclasses__():
for scope in sc.get_scope().split(' '):
if not scope in scopes:
scopes.append(scope)
logging.debug("Scopes {scopes}".format(scopes=scopes))
credentials = AppAssertionCredentials(scope=scopes)
else:
"""USED FOR LOCAL TESTING"""
filepath = #PATH REMOVED
storage = FileStorage(filepath)
credentials = storage.get()
http = credentials.authorize(httplib2.Http(memcache))
self.service = build(self.serviceName, self.version, http=http)
class BigQueryClient(BaseGoogleAPIClient):
@classmethod
def get_scope(cls):
return 'https://www.googleapis.com/auth/bigquery'
@property
def serviceName(self):
return 'bigquery'
@property
def version(self):
return 'v2'
#METHODS REMOVED