【问题标题】:OAuth google Api return null .NETOAuth google Api 返回 null .NET
【发布时间】:2020-11-01 09:42:39
【问题描述】:

我使用的是 GoogleWebAuthorizationBroker,但发布后它不起作用,我更改为 GoogleAuthorizationCodeFlow 但我得到空令牌响应

public static Google.Apis.Drive.v3.DriveService GetService()
    {

        var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets
            {
                ClientId = "-----",
                ClientSecret = "----"
            },
            Scopes = new[] { Google.Apis.Drive.v3.DriveService.Scope.DriveReadonly, "email" },
            DataStore = new FileDataStore(Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("/"), "DriveServiceCredentials.json"), true)
        });
        TokenResponse token = flow.LoadTokenAsync("user", CancellationToken.None).Result;       
            
              UserCredential credential = new UserCredential(flow, "user", token);
              
                Google.Apis.Drive.v3.DriveService service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "GoogleDriveRestAPI-v3",
            });   
  return service; 
    }

【问题讨论】:

  • 任何解决方案......?
  • 您的credentials.json 文件是否位于要访问的正确目录中?你有生成的token 文件吗?如果可以,请您将其删除以查看是否可以成功生成新令牌?你关注this guide授权了吗?
  • 是的,我在正确的目录中有 credentials.json 文件
  • 我更新代码

标签: asp.net-mvc google-api google-drive-api google-oauth


【解决方案1】:

要在您的 ASP .NET Web 应用程序中实现 OAuth 2.0,请按照here 中描述的步骤操作。考虑到您已成功创建凭据,您只需要按照文档中提供的示例中所述调整您的 FlowMetadata(同时考虑到您已为您的应用程序包含相关包)。

请注意,在此实现中,它使用Client IDClient Secret 而不是使用credentials.json 文件进行身份验证。有关在 ASP .NET 中处理用户凭据的更多信息check this piece of documentation

【讨论】:

    猜你喜欢
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    相关资源
    最近更新 更多