Google 驱动器 API 将其托管到 IIS 后上传失败。将错误显示为无法启动浏览器

Google Drive API upload Fails after hosting it to IIS. showing the error as Failed to launch the Browser with

我在我的应用程序中使用 google 外部登录,我需要一个功能,用户可以将图像从应用程序上传到他的 google 驱动器。

在我的本地主机代码中,google 驱动文件上传功能工作正常,在将其托管给 IIS 用户后无法上传图像。

我创建了一个错误日志文件。因为我发现了以下异常:

System.AggregateException: One or more errors occurred. ---> System.NotSupportedException: Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=347588538121-jeali0jufd389gqsi4ge22ent3939b4m.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A60931%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive" for authorization. See inner exception for details. ---> System.ComponentModel.Win32Exception: Access is denied

在 redirect_uri 中我找到了 "localhost"。但托管后应该是www.abc.com,而且这里的端口号也一直在变化。

从 stachoverflow 和其他网站尝试了很多方法。但是没有找到解决方法。

请帮我解决这个问题。

谢谢。

尝试过

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    obj,
    Scopes,
    "user",
    CancellationToken.None,
    new FileDataStore(AppDomain.CurrentDomain.BaseDirectory + "Daimto.GoogleDrive.Auth.Store")
).Result;

还有:

using (var stream = new FileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "credentials.json", FileMode.Open, FileAccess.Read))
{
    // The file token.json stores the user's access and refresh tokens, and is created
    // automatically when the authorization flow completes for the first time.
    string credPath = AppDomain.CurrentDomain.BaseDirectory + "token.json";
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        Scopes,
        "user",
        CancellationToken.None,
        new FileDataStore(credPath, true)).Result;
}

您正在使用 GoogleWebAuthorizationBroker.AuthorizeAsync,它专为与已安装的应用程序一起使用而设计。它跨越 运行 所在机器上的 Web 浏览器。当你在本地工作时这会工作正常但是当你尝试在服务器上 运行 它会失败因为你无权在服务器上生成浏览器(不是因为用户不会看到它会有所帮助它)

你应该使用的是这样的东西

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

可以在此处找到完整示例Web applications (ASP.NET MVC)