【问题标题】:C# WebApi 2 Google Drive authentication on server side服务器端的 C# WebApi 2 Google Drive 身份验证
【发布时间】:2018-06-19 16:52:36
【问题描述】:

我正在开发一个使用谷歌驱动 API 的网络 API,在我的本地机器上,首先运行它会将我重定向到谷歌页面进行身份验证,然后运行良好。现在我想将我的代码发布到 Azure,在我发布它之后,同样的功能失败了,因为我可以进行身份​​验证。如何在服务器上执行此身份验证?

我已遵循 .NET 快速入门 (https://developers.google.com/drive/api/v3/quickstart/dotnet),这是我的代码:

using (var stream =
            new FileStream($@"{dir}\client_secret.json", FileMode.Open, FileAccess.Read))
        {
            var cred = GoogleClientSecrets.Load(stream).Secrets;
            Credentials = GoogleWebAuthorizationBroker.AuthorizeAsync(
                cred,
                Scopes,
                "user",
                CancellationToken.None).Result;
        }
        service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = Credentials,
            ApplicationName = ApplicationName,
        });
}

【问题讨论】:

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


    【解决方案1】:

    您所遵循的教程是针对 .NET 控制台应用程序的,它是本机应用程序而不是 Web 应用程序。 GoogleWebAuthorizationBroker 用于已安装的应用程序。它可以在 localhost 上运行,因为它能够在您的机器上生成浏览器窗口。这在托管环境中不起作用,因为它无法在服务器上生成 Web 浏览器。

    你应该遵循这个例子Web applications

    using System;
    using System.Web.Mvc;
    
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Auth.OAuth2.Flows;
    using Google.Apis.Auth.OAuth2.Mvc;
    using Google.Apis.Drive.v2;
    using Google.Apis.Util.Store;
    
    namespace Google.Apis.Sample.MVC4
    {
        public class AppFlowMetadata : FlowMetadata
        {
            private static readonly IAuthorizationCodeFlow flow =
                new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                    {
                        ClientSecrets = new ClientSecrets
                        {
                            ClientId = "PUT_CLIENT_ID_HERE",
                            ClientSecret = "PUT_CLIENT_SECRET_HERE"
                        },
                        Scopes = new[] { DriveService.Scope.Drive },
                        DataStore = new FileDataStore("Drive.Api.Auth.Store")
                    });
    
            public override string GetUserId(Controller controller)
            {
                // In this sample we use the session to store the user identifiers.
                // That's not the best practice, because you should have a logic to identify
                // a user. You might want to use "OpenID Connect".
                // You can read more about the protocol in the following link:
                // https://developers.google.com/accounts/docs/OAuth2Login.
                var user = controller.Session["user"];
                if (user == null)
                {
                    user = Guid.NewGuid();
                    controller.Session["user"] = user;
                }
                return user.ToString();
    
            }
    
            public override IAuthorizationCodeFlow Flow
            {
                get { return flow; }
            }
        }
    }
    

    关于 azure 的注意事项,您可能必须更改 filedatastore 存储凭据的位置,这取决于您具有写入权限的位置。这可以通过提供要存储它的文件夹的路径来完成。

    new FileDataStore(@"c:\datastore",true)   
    

    【讨论】:

      猜你喜欢
      • 2020-04-22
      • 2015-03-31
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      • 2018-02-26
      相关资源
      最近更新 更多