使用控制台应用程序打开浏览器,获取一些数据并将其传回应用程序

问题描述 投票:4回答:1

我正在尝试使用简单的C#控制台应用程序获取Google授权代码。整个过程非常简单:使用包含唯一Client Id的URL向Google Oauth2端点发送请求,登录Google帐户,然后获取授权码:

string url = "https://accounts.google.com/o/oauth2/v2/auth?whatever";
Process.Start("chrome.exe", url); // open up browser so user can log in
// get the auth code from chrome

如何从Google Chrome中获取生成的代码并将其传回我的应用程序?

c# oauth-2.0 console-application google-oauth2
1个回答
0
投票

启动另一个打开Web服务器的进程。该Web服务器将只返回它获取的请求URL代码参数。

在OAuth流程中,将回调URL设置为http://localhost:8080(您还需要在console中设置它)。

因此,在用户同意后,Google将重定向到localhost。您的Web服务器将捕获请求(localhost?code=auth_code)并将auth_code返回到控制台,如described此处

© www.soinside.com 2019 - 2024. All rights reserved.