[英]Open the authorization URL without opening browser Python
I am using google_auth_oauthlib.flow
in Python to authorize Google Oauth 2 account.我在 Python 中使用
google_auth_oauthlib.flow
来授权 Google Oauth 2 帐户。 My code looks like this:我的代码如下所示:
from google_auth_oauthlib.flow import InstalledAppFlow
flow = InstalledAppFlow.from_client_secrets_file(
"client_secret_929791903032.apps.googleusercontent.com.json",
scopes=['profile', 'email'])
flow.run_local_server(open_browser=False)
session = flow.authorized_session()
profile_info = session.get(
'https://www.googleapis.com/userinfo/v2/me').json()
print(profile_info)
Basing on run_local_server()
document, I tried to set open_browser=False
but then Google provided me an URL to authorize, it looks like this https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=929739191032-hpdm8djidqd8o5nqg2gk366efau34ea6q.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=profile+email&state=oHJmupijpVH2gJEPqTogVVHIEtbVXcr&access_type=offline基于
run_local_server()
文档,我尝试设置open_browser=False
但随后 Google 为我提供了一个URL进行授权,看起来像这样https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=929739191032- hpdm8djidqd8o5nqg2gk366efau34ea6q.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=profile+email&state=oHJmupijpVH2gJEPqTogVVHIEtbVHIEtbVH
After clicking to the provided link, my browser automatically opened with the UI named Sign in with Google , then I have to sign in manually on the browser.单击提供的链接后,我的浏览器会自动打开名为Sign in with Google的 UI,然后我必须在浏览器上手动登录。
So my question is how to open the authorization URL without opening browser?所以我的问题是如何在不打开浏览器的情况下打开授权网址? I want my code automatically authorize without doing manually.
我希望我的代码无需手动操作即可自动授权。
So my question is how to open the authorization URL without opening browser?
所以我的问题是如何在不打开浏览器的情况下打开授权网址? I want my code automatically authorize without doing manually.
我希望我的代码无需手动操作即可自动授权。
If you are using G Suite, you can create a service account and enable Domain Wide Delegation to assume the identity of a G Suite user.如果您使用的是 G Suite,则可以创建一个服务帐号并启用域范围委派以使用 G Suite 用户的身份。 This only works for users that are part of your G Suite domain.
这仅适用于属于您 G Suite 网域的用户。
If you are not using G Suite, you cannot bypass the user authentication screen the first time a user comes to your website.如果您未使用 G Suite,则无法在用户首次访问您的网站时绕过用户身份验证屏幕。 Once a user authenticates with
offline
access, you can save the Refresh Token to use in the future.用户通过
offline
访问进行身份验证后,您可以保存刷新令牌以备将来使用。
Authentication and Authorization is between the client (user) and Google Accounts.身份验证和授权在客户端(用户)和 Google 帐户之间进行。 Your software is not involved in the credentials part (username, password, etc.).
您的软件不涉及凭据部分(用户名、密码等)。 The user must grant permission to Google Accounts to allow your services to access the user's Google Identity.
用户必须向 Google 帐户授予权限,才能允许您的服务访问用户的 Google 身份。
[EDIT 1/22/2019 - after question on how to save the refresh token] [编辑 1/22/2019 - 关于如何保存刷新令牌的问题]
The following code with authorize and save the refresh token:以下代码授权并保存刷新令牌:
# pip install google-auth-oauthlib
from google_auth_oauthlib.flow import InstalledAppFlow
# https://google-auth-oauthlib.readthedocs.io/en/latest/reference/google_auth_oauthlib.flow.html
flow = InstalledAppFlow.from_client_secrets_file(
'client_secrets.json',
scopes=['https://www.googleapis.com/auth/cloud-platform'])
cred = flow.run_local_server(
host='localhost',
port=8088,
authorization_prompt_message='Please visit this URL: {url}',
success_message='The auth flow is complete; you may close this window.',
open_browser=True)
with open('refresh.token', 'w+') as f:
f.write(cred._refresh_token)
print('Refresh Token:', cred._refresh_token)
print('Saved Refresh Token to file: refresh.token')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.