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

发布于 2025-01-29 02:42:02 字数 907 浏览 1 评论 0 原文

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

基本上,我需要与它们在但是C#等效。

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

我当前的实现我只是打开浏览器,如果不能打开错误。

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

I want to manually authorize an app that runs in the console where there is no browser available.

Basically I need the same code as they are using on this website but the C# Equivalent.

I just can't seem to find the part in the C# Library that allows me to print a link to the console instead of opening a browser.

My current implementation I have just opens the browser and throws an Error if it cant.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2025-02-05 02:42:02

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 demyssified

曾经是打印链接

的方法打印授权链接,但明天我将不得不在图书馆里挖掘。

The GoogleWebAuthorizationBroker.AuthorizeAsync method is designed for installed applications. Installed applications will open the browser on the machine the code is running on.

The following example is designed for web applications. The application would need to be running on a web server.

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};
        });
}
      

These are your two options unless you are able to use service account authorization. Service account authorization is intended for server to server communication between your application and an account you the developer control. It does not work with all google apis.

options

Consider running your code once locally, then copy credentials file that is created by FileDataStore onto the server along with your code. Google .net – FileDatastore demystified

print link

There used to be a method to print the authorization link but i will have to dig around in the library for it tomorrow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文