我们正在背离数据存储的预定出口机制(谷歌建议),采用通过云调度器调度数据存储备份的方式,这将针对HTTP 云函数。在这里,我们希望使用云函数将数据存储实体导出到特定的存储桶中。这种偏离标准机制的原因是,我们希望避免在所有服务中复制非应用程序特定的代码。
根据文档,托管导出和导入服务只能通过数据存储模式Admin (REST,RPC)可用,并且请求需要OAuth 2.0授权。
在云函数中,要访问数据存储API https://datastore.googleapis.com/v1/projects/<APP ID>:export,我们需要来自作用域https://www.googleapis.com/auth/datastore的access_token。
在标准的GAE应用程序代码中,使用python27运行时,我们可以获得access_token,如下例所示-
from google.appengine import app_identity
access_token, _ = app_identity.get_access_token('https://www.googleapis.com/auth/datastore')但是,云函数有Python37运行时。因此,导入google.appengine会给出error as error: ModuleNotFoundError: No module named 'google.appengine'错误
如何获得所需范围的access_token?(以下任何一个范围)-
请参考Python代码/文档。谢谢。
发布于 2019-03-11 12:23:09
JWT和Access令牌是两件不同的事情。JWT由三个部分组成:标题、声明集和签名。另一方面,只能从授权服务器获得访问令牌。请在这谷歌文档的‘增编’部分。我通过传递服务帐户详细信息来创建云函数,并将其编码到JWT中(带有标题和有效负载)。
signed_jwt = jwt.encode(payload, key, headers=additional_headers, algorithm='RS256')
decoded_signed_jwt = signed_jwt.decode("utf-8")为了获得访问令牌,请参考以下代码片段。
token_endpoint = 'https://www.googleapis.com/oauth2/v4/token'
token_req_data = {
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion': decoded_signed_jwt
}
token_post_data = urllib.parse.urlencode(token_req_data).encode("utf-8")
access_token_req = urllib.request.Request(token_endpoint, token_post_data)
access_token_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
result = urllib.request.urlopen(access_token_req)
json_str = result.read()这个json字符串应该包括访问令牌。在所需API的请求头中使用此访问令牌。请按以下方式更新签名JWT的有效负载,否则可能无法获得有效的JWT。
'scope': 'https://www.googleapis.com/auth/cloud-platform',
'aud': 'https://www.googleapis.com/oauth2/v4/token'希望这有帮助,我感谢谷歌对此部分的支持。
发布于 2019-02-19 10:49:30
Python 3.4或更高版本由google.appengine支持。该错误被发布到在函数运行时没有作为依赖项安装的google-api-python-client。
尝试将其添加到Clound编辑器页面的requeriments.txt中。
您还可以使用谷歌-奥斯 by import google.auth
https://stackoverflow.com/questions/54762687
复制相似问题