简体   繁体   English

来自桌面应用程序的 OAuth2 身份验证代码

[英]OAuth2 authentication code from desktop app

I'm having some trouble with my desktop app and connecting to the Blizzard API.我的桌面应用程序和连接到暴雪 API 时遇到问题。 For OAuth2, I know I need to redirect the user to a website where they provide authorization for the application to access data on their behalf.对于 OAuth2,我知道我需要将用户重定向到他们为应用程序提供授权以代表他们访问数据的网站。 From there, there's a redirect URL that contains the authorization code that must be exchanged for an access token.从那里,有一个重定向 URL 包含必须交换访问令牌的授权代码。

My app is for desktop, so I'm unsure of how to obtain the authorization code from that redirect URL.我的应用程序是用于桌面的,所以我不确定如何从该重定向 URL 获取授权码。 Here's some of my code that I'm using for testing:这是我用于测试的一些代码:

if self._check_connection():
        self.client_id = os.getenv("BLIZZ_CLIENT_ID")
        self.client_secret = os.getenv("BLIZZ_CLIENT_SECRET")
        self.region = "us"
        state = "abcd1234"

        re = requests.get(f"{BLIZZ_AUTH_URL}?client_id={self.client_id}&response_type=code&redirect_uri={REPO_URL}&locale={self.region}&scope={SC2_SCOPE}&state={state}")

        re_url = re.url
        print(re_url)

The fuller product would use webbrowser to get the user to authorize my app, but then I need to get that code that will be in the redirect URL after clicking "Allow".更完整的产品将使用 webbrowser 让用户授权我的应用程序,但随后我需要在单击“允许”后获取将在重定向 URL 中的代码。 So I'm trying to get the URL at that last bit there, but when I print it to the console to check it, it doesn't contain the authorization code.因此,我试图在最后一位获得 URL,但是当我将其打印到控制台进行检查时,它不包含授权码。 However, if I click it, it opens the correct page in the browser, which contains the auth code.但是,如果我单击它,它会在浏览器中打开正确的页面,其中包含身份验证代码。

Thank you for taking the time to read, I've looked at all sorts of posts on the internet but most of them are about web apps or they say something vague like "get the authorization code and exchange it" without elaborating on how that actually works.感谢您花时间阅读,我查看了互联网上的各种帖子,但其中大多数是关于 web 应用程序的,或者他们说的是“获取授权码并交换它”之类的模糊内容,但没有详细说明实际情况如何作品。

Is there any clean way to do this that doesn't require the user to basically copy the code from the address bar and paste it into my app?有没有什么干净的方法不需要用户基本上从地址栏中复制代码并将其粘贴到我的应用程序中? Ideally, I'd like to just grab it programmatically and continue from there.理想情况下,我想以编程方式抓取它并从那里继续。

Since you are asking the user to authorize your application via an OAuth2 provider, you need to pass a proper redirect uri to the OAuth2 server.由于您要求用户通过 OAuth2 提供程序授权您的应用程序,因此您需要将正确的重定向 uri 传递给 OAuth2 服务器。

Usually it is a valid domain pointing to a web server that will handle the request.通常它是一个有效域,指向将处理请求的 web 服务器。 For a desktop application (or mobile for that matter) you won't have a web server, so you need a custom protocol to handle that response and redirect it to your application.对于桌面应用程序(或移动应用程序),您将没有 web 服务器,因此您需要一个自定义协议来处理该响应并将其重定向到您的应用程序。

For instance when you click an email address hyperlink mailto:email@server.com the protocol mailto: tells the browser to redirect the call to the host OS and open up the default mail application.例如,当您单击 email 地址超链接mailto:email@server.com时,协议mailto:告诉浏览器将呼叫重定向到主机操作系统并打开默认邮件应用程序。 The same works for most apps that interact with a browser like Skype, Discord, Twitch, etc对于大多数与 Skype、Discord、Twitch 等浏览器交互的应用程序也是如此

For windows apps you can read this topic for guidance: How do I register a custom URL protocol in Windows?对于 windows 应用程序,您可以阅读此主题以获取指导: 如何在 Windows 中注册自定义 URL 协议?

The idea is to open up the browser with the battle.net login, but the redirect url will point to your custom protocol, that way your application will receive a message containing the full URL including the authorization_code you need.想法是使用战网登录打开浏览器,但重定向 url 将指向您的自定义协议,这样您的应用程序将收到一条包含完整 URL 的消息,其中包括您需要的authorization_code码。

Note that by doing that the OS will launch a new instance of your app, you'll have to use some sort of messaging system/pipes/sockets to redirect data from the new instance to the currently running one.请注意,通过这样做,操作系统将启动您的应用程序的新实例,您必须使用某种消息传递系统/管道/套接字将数据从新实例重定向到当前运行的实例。 To detect a previously running app you can simply use a mutex.要检测以前运行的应用程序,您可以简单地使用互斥锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM