【问题标题】:Failed to launch browser access denied after hosting to IIS托管到 IIS 后无法启动浏览器访问被拒绝
【发布时间】:2018-03-03 10:10:25
【问题描述】:

我正在使用 Google.net 客户端库对 Google API 进行身份验证,此代码在本地工作,但是当我将其上传到我的服务器时出现此错误

“无法使用 \"https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=123&redirect_uri=http%3A%2F%2Flocalhost%3A54597%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive\" 启动浏览器进行授权。有关详细信息,请参阅内部异常。”

我的代码

       Google.Apis.Drive.v3.DriveService service = null;
        try
        {
            UserCredential credential;
           // string strUploadFolder = ConfigurationManager.AppSettings["UploadFolder"];

            string credPath = AppDomain.CurrentDomain.BaseDirectory;
            credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
               new ClientSecrets
               {
                   ClientId = ConfigurationManager.AppSettings["client_id"],
                   ClientSecret = ConfigurationManager.AppSettings["client_secret"],
               },
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;

            // Create Drive API service.
            service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });
        }
        catch (Exception ex)
        {
            throw;
        }
        return service;

【问题讨论】:

  • 为此提供所需的详细信息。如果可能,添加一些您尝试过的代码 sn-p。
  • 请添加标签 Google-api-dotnet-client 和 c#

标签: c# .net wcf google-api-dotnet-client


【解决方案1】:

您使用的代码用于已安装的应用程序。随着安装应用程序的浏览器窗口在机器上打开。

对于网络应用程序,需要在用户计算机上打开用户同意的浏览器窗口

documentation on mvc authentication

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

【讨论】:

  • 对于 Web 应用程序,您需要在 Dev Console 中指定重定向 URL。如果应用程序缺少恒定的 IP 地址/URL,建议使用 Google API 的方式是什么?例如,我的应用程序可以作为 localhost/appname 或某个 serveripaddress/appname 启动。由于在我的情况下无法确定访问源/重定向 URL,因此我选择了已安装的应用程序选项。但它会导致“无法启动浏览器”错误。任何解决方法可用于此类场景?谢谢。
  • 如果它是一个 Web 应用程序,那么它需要有一个静态 IP 位置。在您的情况下,我会将两者都添加为重定向 uri。但如果这是一个生产环境,我不会运行 add localhost,这将是一个安全风险,您需要有一个静态位置来托管您的应用程序。安装的应用程序将无法工作,因为它会在运行它的机器上打开 Web 浏览器,在这种情况下是您的 Web 服务器。您的解决方案是为您的应用程序创建一个静态托管位置。没有解决用户安全问题的方法。这样做是有原因的。
  • @DalmTo 感谢您的想法。将 Google auth API 函数调用从控制器移动到 Windows 服务怎么样?当用户单击登录按钮时,控制器将接收控制,控制器会将其发送到 Windows 服务,该服务将启动“已安装的应用程序”Google auth API 函数调用?这样的设计还是要面临“无法启动浏览器”的错误?
猜你喜欢
  • 2020-10-27
  • 1970-01-01
  • 2017-03-02
  • 2014-06-23
  • 1970-01-01
  • 2013-01-17
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多