Python Oauth重定向URL不是动态的
我的要求是,我需要授权谷歌用户在每次请求时动态使用OAuth2,并以个性化的方式响应 以下是我遵循的流程##标题## 在谷歌控制台中创建项目 以以下方式创建Oauth客户端ID 应用程序类型:web 授权的JavaScript来源:http://localhost 授权重定向URI:http://localhost:4000 下载为client_request.json 下面是运行下面的python代码后生成的URL 我们面临的问题是 生成的URL在所有用户中都是相同的,因此我们无法单独处理每个请求 仅供参考:以下是python代码片段 '''Python Oauth重定向URL不是动态的,python,google-api,google-oauth,gmail-api,google-api-python-client,Python,Google Api,Google Oauth,Gmail Api,Google Api Python Client,我的要求是,我需要授权谷歌用户在每次请求时动态使用OAuth2,并以个性化的方式响应 以下是我遵循的流程##标题## 在谷歌控制台中创建项目 以以下方式创建Oauth客户端ID 应用程序类型:web 授权的JavaScript来源:http://localhost 授权重定向URI:http://localhost:4000 下载为client_request.json 下面是运行下面的python代码后生成的URL 我们面临的问题是 生成的URL在所有用户中都是相同的,因此我们无法单独处理每
来自apiclient导入发现
从GoogleAppClient.discovery导入生成
从GoogleAppClient导入发现
从httplib2导入Http
从oauth2client导入文件、客户端、工具
从oauth2client.tools导入argparser,运行\u流
args=argparser.parse_args()
从oauth2client.file导入存储
范围https://www.googleapis.com/auth/gmail.settings.basic'
存储=存储('credentials.STORAGE')
凭据=存储。获取()
args.noauth_local_webserver=True
http=httplib2.http()
如果没有信用或信用无效:
flow=client.flow\u from\u clientsecrets('client\u secret.json',SCOPES)
creds=tools.run\u流(流、存储、http=http)
GMAIL=discovery.build('GMAIL','v1',http=creds.authorize(http()))
地址=GMAIL.users().settings().sendAs().list(userId='me',
fields='sendAs(isPrimary,sendAsEmail').execute().get('sendAs')
地址中的地址:
if address.get('isPrimary'):
打破
签名={'signature':''}
rsp=GMAIL.users().settings().sendAs().patch(userId='me',
sendAsEmail=地址['sendAsEmail'],正文=签名)。执行()
打印(“签名更改为'%s'%rsp['Signature'])”
我想您可能没有理解Oauth的工作原理
https://accounts.google.com/o/oauth2/auth?client_id=123456789123-Lkhipfkk6g224bnf7n9sfdsdfsdss.apps.googleusercontent.com&redirect_uri=http%3A%2F%2localhost%3A4000%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.settings.basic&access_type=offline&response_type=code
- 客户端id向google标识您的应用程序
- 重定向要将授权数据返回到的文件的位置
- 范围您希望访问的授权范围
- access\u type offline返回刷新令牌
- response_type这是一个oauth的东西,基本上告诉auth服务器您期望的响应
def main():
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
谢谢Daimto编辑我的问题我不是100%清楚你的问题是什么,或者你想做什么。什么是
授权谷歌用户在每次请求时动态使用OAuth2,并以个性化的方式响应。
的意思是什么?@Datmto让我们不要进入定义。。。让我们努力解决这个问题。使用当前仅限于授权的Oauth方法,可能需要一些外部组件/api来处理会话。我想问题是,所有用户生成的URL都是相同的,我们无法单独处理每个请求。正如我所解释的那样,你想要什么样的解决方案。您无法更改授权服务器的工作方式,尤其是您不拥有的Google授权服务器。
def main():
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)