为 google Oauth2 延长 access_token 的寿命
extending the life of a access_token for google Oauth2
我正在使用以下代码为 google Oauth2 服务获取 access_token。
逻辑很简单。
- 执行代码
- 如果有令牌(保存在文件 token.txt 中),请使用它
- 如果没有令牌,使用基于浏览器的
flow.run_console()
;将访问令牌保存到文件
- return 会话的构建()
我的问题是,有没有办法将令牌的寿命延长到 3600 秒以上?
像一个月?或更好?谢谢
代码:
def get_service(ServiceName, API_VERSION):
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
creds = json.loads(Path(CLIENT_SECRETS_FILE).read_text())
if Path(token).exists():
access_token = Path(token).read_text()
credentials = Credentials(
None,
refresh_token=access_token,
token_uri="https://accounts.google.com/o/oauth2/token",
client_id=creds['installed']['client_id'],
client_secret=creds['installed']['client_secret']
)
else:
credentials = flow.run_console()
with open(token, 'w') as filetowrite:
filetowrite.write(credentials.token)
return build(ServiceName, API_VERSION, credentials = credentials)
没有。这是不可能的。很难证明这是不可能的。但这是推理:
想法是,出于安全原因,令牌的寿命很短。
一个人应该获得一个额外的刷新令牌。一旦 oauth 令牌过期,使用刷新令牌获取新的 oauth 令牌或更好的新令牌对。
这降低了窃听令牌的风险。即使攻击者设法获得了令牌,它也只有很短的有效期。如果服务提供商需要,还可以撤销特定令牌。
见OAuth2 and Google API: Access token expiration time?
不可能,访问令牌通常会在 60 分钟后过期。如果您有刷新令牌,则可以使用刷新令牌获取新的(有效的)访问令牌。
访问令牌和刷新令牌的组合最大限度地提高了安全性和灵活性。 OAuth 2.0 规范推荐此选项,并且一些较大的实现已采用此方法。
此文档解释了如何执行此操作:
https://developers.google.com/identity/protocols/OAuth2WebServer#offline
您不能扩展它(至少不能自己扩展)的主要原因是它的值由令牌服务器设置。如果您要解码访问令牌,更改到期值,再次对其进行编码,它将使令牌无效,因为这些令牌具有嵌入式数字签名。 [ 使用 https://jwt.io 解码并自行查看 ]。
关于扩展它的方法,您必须与令牌服务器交谈 - 在您的情况下 Google 并查看他们是否可以为您破例。正如其他答案所建议的那样,您最好的选择是依赖 refresh_token。
我正在使用以下代码为 google Oauth2 服务获取 access_token。 逻辑很简单。
- 执行代码
- 如果有令牌(保存在文件 token.txt 中),请使用它
- 如果没有令牌,使用基于浏览器的
flow.run_console()
;将访问令牌保存到文件 - return 会话的构建()
我的问题是,有没有办法将令牌的寿命延长到 3600 秒以上? 像一个月?或更好?谢谢
代码:
def get_service(ServiceName, API_VERSION):
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
creds = json.loads(Path(CLIENT_SECRETS_FILE).read_text())
if Path(token).exists():
access_token = Path(token).read_text()
credentials = Credentials(
None,
refresh_token=access_token,
token_uri="https://accounts.google.com/o/oauth2/token",
client_id=creds['installed']['client_id'],
client_secret=creds['installed']['client_secret']
)
else:
credentials = flow.run_console()
with open(token, 'w') as filetowrite:
filetowrite.write(credentials.token)
return build(ServiceName, API_VERSION, credentials = credentials)
没有。这是不可能的。很难证明这是不可能的。但这是推理:
想法是,出于安全原因,令牌的寿命很短。 一个人应该获得一个额外的刷新令牌。一旦 oauth 令牌过期,使用刷新令牌获取新的 oauth 令牌或更好的新令牌对。
这降低了窃听令牌的风险。即使攻击者设法获得了令牌,它也只有很短的有效期。如果服务提供商需要,还可以撤销特定令牌。
见OAuth2 and Google API: Access token expiration time?
不可能,访问令牌通常会在 60 分钟后过期。如果您有刷新令牌,则可以使用刷新令牌获取新的(有效的)访问令牌。
访问令牌和刷新令牌的组合最大限度地提高了安全性和灵活性。 OAuth 2.0 规范推荐此选项,并且一些较大的实现已采用此方法。
此文档解释了如何执行此操作: https://developers.google.com/identity/protocols/OAuth2WebServer#offline
您不能扩展它(至少不能自己扩展)的主要原因是它的值由令牌服务器设置。如果您要解码访问令牌,更改到期值,再次对其进行编码,它将使令牌无效,因为这些令牌具有嵌入式数字签名。 [ 使用 https://jwt.io 解码并自行查看 ]。
关于扩展它的方法,您必须与令牌服务器交谈 - 在您的情况下 Google 并查看他们是否可以为您破例。正如其他答案所建议的那样,您最好的选择是依赖 refresh_token。