简体   繁体   English

不打开浏览器Python打开授权网址

[英]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.

相关问题 从python打开php url而不打开浏览器 - Opening a php url from python without opening the browser 在浏览器中使用python打开URL(并非无头) - opening URL in browser with python (not headless) 如果没有先在浏览器中打开它,就无法在 Python 中打开它 - Can't open an URL in Python without opened it in browser first 如何在不使用浏览器的情况下使用Python打开URL? - How do you open a URL with Python without using a browser? 如何在不关闭和打开每个元素的浏览器的情况下单击并打开 python selenium 元素中的对象集合 - How do I click and open a collection of objects in an element in python selenium without closing and opening the browser for each element 打开 web 浏览器并在 python 中获取 url 历史记录 - opening a web browser and get url histories in python 在不打开浏览器的情况下抓取网站数据(python) - Scrape website data without opening the browser (python) 使用 Gmail API 而不在 Python 中打开浏览器 - Using Gmail API without opening browser in Python 如何在不实际打开浏览器的情况下使用Python向服务器发送URL请求(“不使用webbrowser模块”)? - How can I send a URL request to the server with Python without actually opening my browser (“without using the webbrowser module”)? python 3打开并读取没有URL名称的URL - python 3 open and read url without url name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM