【发布时间】:2024-04-24 11:35:02
【问题描述】:
我一直在开发一个使用 OAuth2 来识别用户的 python 应用程序。我似乎已经成功实现了 OAuth2 隐式授权的工作流程(通常用于已安装和用户代理应用程序),但在接收令牌的最后一步,似乎出现了问题。
当用户需要进行身份验证时,会生成一个 PyQt QWebView 窗口(基于 webkit),其中显示登录页面。在用户登录并允许我的应用程序的范围权限后,OAuth2 服务器将重定向到预先指定的 redirect_uri。
问题在于,当使用 QWebView 浏览器时,通常出现在 # 之后的令牌字符串似乎已从 URL 中删除:QWebView 返回的 URL 只是基本的 redirect_uri。
如果我复制粘贴 OAuth 授权 URL 并在普通 Web 浏览器(如 Chrome 或 Firefox)中完成登录和授权的相同步骤,我确实会看到包含令牌字符串的 redirect_uri,所以问题不存在于 OAuth2 过程中,但在我这边的实现中一定会出错。
这种行为是 QWebView 或 webkit 实现所固有的吗?我读错了 QUrl?
为了完整起见,这是我的代码:
为开放科学框架生成 OAuth2 URL 的 osf.py 模块。
# Import basics
import sys
import os
# Module for easy OAuth2 usage, based on the requests library,
# which is the easiest way to perform HTTP requests.
# OAuth2Session object
from requests_oauthlib import OAuth2Session
# Mobile application client that does not need a client_secret
from oauthlib.oauth2 import MobileApplicationClient
#%%----------- Main configuration settings ----------------
client_id = "cbc4c47b711a4feab974223b255c81c1"
# TESTED, just redirecting to Google works in normal browsers
# the token string appears in the url of the address bar
redirect_uri = "https://google.nl"
# Generate correct URLs
base_url = "https://test-accounts.osf.io/oauth2/"
auth_url = base_url + "authorize"
token_url = base_url + "token"
#%%--------------------------------------------------------
mobile_app_client = MobileApplicationClient(client_id)
# Create an OAuth2 session for the OSF
osf_auth = OAuth2Session(
client_id,
mobile_app_client,
scope="osf.full_write",
redirect_uri=redirect_uri,
)
def get_authorization_url():
""" Generate the URL with which one can authenticate at the OSF and allow
OpenSesame access to his or her account."""
return osf_auth.authorization_url(auth_url)
def parse_token_from_url(url):
token = osf_auth.token_from_fragment(url)
if token:
return token
else:
return osf_auth.fetch_token(url)
主程序,打开带有登录屏幕的 QWebView 浏览器窗口
# Oauth2 connection to OSF
import off
import sys
from PyQt4 import QtGui, QtCore, QtWebKit
class LoginWindow(QtWebKit.QWebView):
""" A Login window for the OSF """
def __init__(self):
super(LoginWindow, self).__init__()
self.state = None
self.urlChanged.connect(self.check_URL)
def set_state(self,state):
self.state = state
def check_URL(self, url):
#url is a QUrl object, covert it to string for easier usage
url_string = url.toEncoded()
print(url_string)
if url.hasFragment():
print("URL CHANGED: On token page: {}".format(url))
self.token = osf.parse_token_from_url(url_string)
print(self.token)
elif not osf.base_url in url_string:
print("URL CHANGED: Unexpected url")
if __name__ == "__main__":
""" Test if user can connect to OSF. Opens up a browser window in the form
of a QWebView window to do so."""
# Import QT libraries
app = QtGui.QApplication(sys.argv)
browser = LoginWindow()
auth_url, state = osf.get_authorization_url()
print("Generated authorization url: {}".format(auth_url))
browser_url = QtCore.QUrl.fromEncoded(auth_url)
browser.load(browser_url)
browser.set_state(state)
browser.show()
exitcode = app.exec_()
print("App exiting with code {}".format(exitcode))
sys.exit(exitcode)
基本上,当从 OAuth 服务器返回时,QWebView 的 url_changed 事件提供给 check_URL 函数的 url 永远不会包含 OAuth 令牌片段,无论我使用什么 redirect_uri(在此示例中,我只是为了重定向到 google简单)。
谁能帮我解决这个问题?我已经用尽了在哪里寻找解决此问题的方法的选择。
【问题讨论】:
标签: python oauth-2.0 pyqt python-requests qwebview