已安装应用程序的 OAuth 2.0 刷新令牌
OAuth 2.0 refresh token for installed applications
我正在尝试使用 Oauth 2.0 访问已安装应用程序中的 Google API。我希望用户只授予一次访问权限,然后使用刷新令牌获取新的访问令牌以使用 api.
用户使用此代码向应用程序授予权限:
flow = client.flow_from_clientsecrets(
'client_secrets.json',
scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/',
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
auth_uri = flow.step1_get_authorize_url()
webbrowser.open(auth_uri)
auth_code = raw_input('Introduce the code: ')
credentials = flow.step2_exchange(auth_code)
f = file("Pass.txt", "w")
f.write(credentials.to_json())
f.close()
然后应用程序每天使用其他代码来使用 api:
f = open("Pass.txt", "r")
credentials = client.OAuth2Credentials.from_json(f.read())
f.close()
client = gdata.apps.emailsettings.client.EmailSettingsClient(domain=domain_name)
client.auth_token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
此代码工作了一周,然后停止工作并抛出以下错误:
gdata.client.Unauthorized: Unauthorized - Server responded with: 401, <HTML>
<HEAD>
<TITLE>Token invalid - Invalid token: Token not found</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Token invalid - Invalid token: Token not found</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
现在有人知道这段代码有什么问题吗?
谢谢。
当令牌过期并且服务器 returns 401 时,您应该在凭据对象上使用 refresh
方法来获取新的访问令牌并将其写回存储中,这样您d 运行:
f = open("Pass.txt", "r")
credentials = client.OAuth2Credentials.from_json(f.read())
f.close()
http = httplib2.Http()
credentials.refresh(http)
f = file("Pass.txt", "w")
f.write(credentials.to_json())
f.close()
我正在尝试使用 Oauth 2.0 访问已安装应用程序中的 Google API。我希望用户只授予一次访问权限,然后使用刷新令牌获取新的访问令牌以使用 api.
用户使用此代码向应用程序授予权限:
flow = client.flow_from_clientsecrets(
'client_secrets.json',
scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/',
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
auth_uri = flow.step1_get_authorize_url()
webbrowser.open(auth_uri)
auth_code = raw_input('Introduce the code: ')
credentials = flow.step2_exchange(auth_code)
f = file("Pass.txt", "w")
f.write(credentials.to_json())
f.close()
然后应用程序每天使用其他代码来使用 api:
f = open("Pass.txt", "r")
credentials = client.OAuth2Credentials.from_json(f.read())
f.close()
client = gdata.apps.emailsettings.client.EmailSettingsClient(domain=domain_name)
client.auth_token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
此代码工作了一周,然后停止工作并抛出以下错误:
gdata.client.Unauthorized: Unauthorized - Server responded with: 401, <HTML>
<HEAD>
<TITLE>Token invalid - Invalid token: Token not found</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Token invalid - Invalid token: Token not found</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
现在有人知道这段代码有什么问题吗?
谢谢。
当令牌过期并且服务器 returns 401 时,您应该在凭据对象上使用 refresh
方法来获取新的访问令牌并将其写回存储中,这样您d 运行:
f = open("Pass.txt", "r")
credentials = client.OAuth2Credentials.from_json(f.read())
f.close()
http = httplib2.Http()
credentials.refresh(http)
f = file("Pass.txt", "w")
f.write(credentials.to_json())
f.close()