【问题标题】:Google API .NET Client - How do I get OAuth2 Access Token and Refresh token for C# ASP.NET Core Web API client to authenticate YouTube Data API v3Google API .NET 客户端 - 如何为 C# ASP.NET Core Web API 客户端获取 OAuth2 访问令牌和刷新令牌以验证 YouTube 数据 API v3
【发布时间】:2021-01-11 10:58:26
【问题描述】:

如何为 C# ASP.NET Core Web API 客户端获取 OAuth2 访问令牌和刷新令牌以验证 YouTube Data API v3

在这种情况下,用户名没有手动输入其用户名和密码,然后接收代码以获取令牌的 UI。不需要 redirect_uri。

如何获取访问令牌和刷新令牌

我曾经用Microsoft Azure AD解决了类似的问题,stackoverflow上的解决方案

我只是找不到有关此场景的 Google Cloud Platform .NET 客户端的任何信息

【问题讨论】:

    标签: c# asp.net-core asp.net-web-api google-api google-api-dotnet-client


    【解决方案1】:

    自 2015 年以来,您不能将客户端登录(用户名和密码)与任何 Google api 一起使用。您需要使用 Oauth2 来验证您的用户。

    您需要先配置库。

    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};
            });
    }
          
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ...
    
        app.UseAuthentication();
        app.UseAuthorization();
    
        ...
    }
    

    然后,您可以随意调用 YouTube API。当点击此端点时,将提示用户同意授权。

    [GoogleScopedAuthorize(YouTubeService.ScopeConstants.Readonly)]
    public async Task<IActionResult> YouTubeCall([FromServices] IGoogleAuthProvider auth)
    {
        GoogleCredential cred = await auth.GetCredentialAsync();
        var service = new YouTubeService(new BaseClientService.Initializer
        {
            HttpClientInitializer = cred
        });
        
        // your call to the youTube service here.
    
    }
    

    我建议您查看Asp .net core 的示例,但它位于谷歌驱动器中,您需要对其进行更改。

    客户端库应该为您处理所有访问令牌和刷新令牌,但如果您真的想访问它们,这里有一些关于如何访问它们的信息#1725

    【讨论】:

    • 在我的场景中,用户不会出现,因为它是 API 到 API 的身份验证。得到 Google Cloud Platform .NET 客户端团队的 amanda-tarafa 和 jskeet 的帮助。不要使用 Web 应用程序 Google API 服务凭据,而是使用桌面凭据和 API 密钥,它可以工作。关于GitHub 的解决方案当您收到令牌后,您可以使用收到的令牌向 YouTube 数据 API 发出请求。
    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 2012-06-25
    • 2012-03-03
    • 2014-11-06
    • 2015-10-30
    • 2014-08-18
    • 1970-01-01
    • 2016-12-08
    相关资源
    最近更新 更多