我试图使用OAuth从Google连接到Google,但是授权失败了,出现了一个错误400: invalid_request (见屏幕截图oauth误差)。
我一直在使用代码跟踪google的官方文档:
import httplib2
from apiclient import errors
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
# Copy your credentials from the console
CLIENT_ID = '___'
CLIENT_SECRET = '___'
# Check https://developers.google.com/webmaster-tools/v1/ for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters.readonly'
# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print ('Go to the following link in your browser: ' + authorize_url)
code = input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)还有一个教程,它专门通过colab笔记本建立连接。
!pip install git+https://github.com/joshcarty/google-searchconsole
import searchconsole
account = searchconsole.authenticate(client_config='client_secret_.json',serialize='credentials.json', flow='console')已知工作流/设置的不同之处:
我做错什么了?
发布于 2022-03-18 21:49:41
看来官方文件已经过时了。
根据谷歌开发者博客的说法,OAuth的频带外(oob)正受到反对:
2022年2月28日- OOB流的新OAuth使用将被阻止。
换句话说,出于安全原因,不应再使用从浏览器手动复制auth令牌并将其粘贴到应用程序的功能。
在这个链接上,也有关于如何为不同的客户端(iOS、Android、Chrome和桌面应用程序)具体解决这个问题的建议。我在JavaScript上遇到了这个问题,并通过创建一个侦听本地主机上任意端口的HTTP服务器来解决这个问题,该端口由带有生成令牌的OAuth2身份验证流自动触发。
您需要更改redirect_uri以使其指向此端口。例如:如果您的服务器侦听端口3000,redirect_uri可能是urn:ietf:wg:oauth:2.0:oob.而不是urn:ietf:wg:oauth:2.0:oob.。
https://stackoverflow.com/questions/71500262
复制相似问题