使用Memcach高效地使用Google api特定服务对象

2024-04-20 11:28:21 发布

您现在位置:Python中文网/ 问答频道 /正文

应用程序正试图在服务帐户的帮助下使用googleapps目录API获取用户信息列表(>;200个用户)。你知道吗

if not memcache.get('directory_service'):
    f = file(KEY_FILE, 'r')
    key = f.read()
    f.close()

    credentials = SignedJwtAssertionCredentials(
        service_email,
        key,
        'https://www.googleapis.com/auth/admin.directory.user',
        sub=GoogleAppAdminEmail)
    http_auth = credentials.authorize(Http())

    directory_service = build('admin', 'directory_v1', http=http_auth)
    memcache.set('directory_service', directory_service, 60 * 60 * 2)
else:
    directory_service = memcache.get('directory_service')
user = directory_service.users().get(userKey=username).execute()

我认为最好将directory_service对象保存在memcache中,以减少执行时间,这样第二个请求之后的应用程序将从memcache获取对象。你知道吗

但是我在实现Memcache之后得到了HttpError 401。你知道吗

File "C:\Users\test\appcode\oauth2client\util.py", line 135, in positional_wrapper

    return wrapped(*args, **kwargs)

File "C:\Users\test\appcode\googleapiclient\http.py", line 723, in execute

    raise HttpError(resp, content, uri=self.uri)

HttpError: <HttpError 401 when requesting https://www.googleapis.com/admin/directory/v1/users/username%40mydomain.com?alt=json returned "Login Required"> 

处理这种情况的有效方法是什么?你知道吗


Tags: key用户httpscomauth应用程序httpget
1条回答
网友
1楼 · 发布于 2024-04-20 11:28:21

对存储directory_service对象没有什么兴趣,因为它是在不调用外部api的情况下创建的。你知道吗

您应该存储的是http_auth对象,因为这个对象的生成成本很高。你知道吗

此外,每秒只能请求服务帐户中的令牌一定次数。我不认为确切的限制在某个地方有记录,但是如果您试图同时从同一个服务帐户生成太多的令牌,您将得到一个Rate limit exceeded错误。你知道吗

好的做法是将令牌存储在一些共享存储服务中。它可以是memcache或数据存储。googleapi客户机附带了您应该使用的an App Engine specific ^{} class。它由数据存储支持,并使用Memcache作为可选的缓存层。你知道吗

相关问题 更多 >