Google Api授权在控制台运行的应用程序



我想手动授权在没有浏览器可用的控制台中运行的应用程序。

基本上我需要相同的代码,因为他们在这个网站上使用,但c#等效。

我似乎找不到c#库中允许我打印到控制台的链接而不是打开浏览器的部分。

我目前的实现,我刚刚打开浏览器,抛出一个错误,如果它不能。

GoogleClientSecrets? fromStream = await GoogleClientSecrets.FromStreamAsync( stream , taskCancellationToken );
UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( fromStream.Secrets
       , scopes
       , user
       , taskCancellationToken
       , new FileDataStore( nameof(Authorize) ) );

GoogleWebAuthorizationBroker.AuthorizeAsync方法是为已安装的应用程序设计的。已安装的应用程序将在运行代码的机器上打开浏览器。

下面的例子是为web应用程序设计的。该应用程序需要在web服务器上运行。

public void ConfigureServices(IServiceCollection services)
{
...
// This configures Google.Apis.Auth.AspNetCore3 for use in this app.
services
.AddAuthentication(o =>
{
// This forces challenge results to be handled by Google OpenID Handler, so there's no
// need to add an AccountController that emits challenges for Login.
o.DefaultChallengeScheme = GoogleOpenIdConnectDefaults.AuthenticationScheme;
// This forces forbid results to be handled by Google OpenID Handler, which checks if
// extra scopes are required and does automatic incremental auth.
o.DefaultForbidScheme = GoogleOpenIdConnectDefaults.AuthenticationScheme;
// Default scheme that will handle everything else.
// Once a user is authenticated, the OAuth2 token info is stored in cookies.
o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogleOpenIdConnect(options =>
{
options.ClientId = {YOUR_CLIENT_ID};
options.ClientSecret = {YOUR_CLIENT_SECRET};
});
}

这是您的两个选择,除非您能够使用服务帐户授权。服务帐户授权用于应用程序与开发人员控制的帐户之间的服务器到服务器通信。它不能与所有的google api一起工作。

<标题>

选项考虑在本地运行一次代码,然后将由FileDataStore创建的凭据文件与代码一起复制到服务器上。Google .net - FileDatastore揭秘

<标题>

打印链接以前有一种方法可以打印授权链接,但是明天我必须在图书馆里找它。

相关内容

最新更新