将Google云端硬盘API上传到IIS后,上传失败.将错误显示为无法启动浏览器 [英] Google Drive API upload Fails after hosting it to IIS. showing the error as Failed to launch the Browser with

查看:130
本文介绍了将Google云端硬盘API上传到IIS后,上传失败.将错误显示为无法启动浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在我的本地主机代码中,将google驱动器文件上传功能托管到无法上传图像的IIS用户之后,可以正常工作.

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

System.AggregateException:发生一个或多个错误.---> System.NotSupportedException:无法使用" System.ComponentModel.Win32Exception:访问被拒绝

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

从stachoverflow和其他网站进行了许多尝试.但没有找到解决方案.

请帮助我解决这个问题.

谢谢.

尝试过

  credential = GoogleWebAuthorizationBroker.AuthorizeAsync(obj,范围用户",CancellationToken.None,新的FileDataStore(AppDomain.CurrentDomain.BaseDirectory +"Daimto.GoogleDrive.Auth.Store")).结果; 

以及:

 使用(可变流=新的FileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory)+"credentials.json",FileMode.Open,FileAccess.Read)){//文件token.json存储用户的访问令牌和刷新令牌,并已创建//首次完成授权流程时自动执行.字符串credPath = AppDomain.CurrentDomain.BaseDirectory +"token.json";凭证= GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,范围用户",CancellationToken.None,新的FileDataStore(credPath,true)).Result;} 

您正在使用 GoogleWebAuthorizationBroker.AuthorizeAsync ,该代码旨在用于已安装的应用程序.它在运行它的计算机上浏览Web浏览器.当您在本地工作时,这种方法会很好用,但是当您尝试在服务器上运行它时,它会失败,因为您无权在服务器上生成浏览器(不是那样会有所帮助,因为用户将看不到它)

您应该使用的是这样的

 私有静态只读IAuthorizationCodeFlow flow =新的GoogleAuthorizationCodeFlow(新的GoogleAuthorizationCodeFlow.Initializer{ClientSecrets =新的ClientSecrets{ClientId ="PUT_CLIENT_ID_HERE",ClientSecret ="PUT_CLIENT_SECRET_HERE"},范围= new [] {DriveService.Scope.Drive},DataStore =新的FileDataStore("Drive.Api.Auth.Store")}); 

可以在此处找到完整的示例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

In the redirect_uri I found "localhost". but after hosting it should be www.abc.com, and the port number is also always varying here.

Tried in many ways from the stachoverflow and other websites. but did not find the solution.

Please help me to resolve this.

Thanks.

Tried with

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

and also with:

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

解决方案

You are using GoogleWebAuthorizationBroker.AuthorizeAsync which is designed for use with installed applications. It spanws the web browser on the machine that it runs on. This will work fine when you are working locally but it will fail when you try to run it on a server as you dont have access to spawn a browser on the server (not that it would help as the user will not see it)

what you should be using is something like this

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

A full example can be found here Web applications (ASP.NET MVC)

这篇关于将Google云端硬盘API上传到IIS后,上传失败.将错误显示为无法启动浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆