C# 从windows共享主机访问Google Drive API时拒绝访问
下面的代码在本地环境中运行良好,但在共享宿主环境中运行已发布的代码时面临问题 例外情况: 发生了一个或多个错误 System.Net.HttpListenerException(0x80004005):访问被拒绝 System.Net.HttpListener.AddAllPrefixes()位于 System.Net.HttpListener.Start()位于 Google.api.Auth.OAuth2.LocalServerCodeReceiver.StartListener()位于 Google.api.Auth.OAuth2.LocalServerCodeReceiver.d__8.MoveNext() ---来自引发异常的上一个位置的堆栈结束跟踪---在 System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Google.api.Auth.OAuth2.AuthorizationCodeInstalledApp.d_u8.MoveNext() ---来自引发异常的上一个位置的堆栈结束跟踪---在 System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Google.api.Auth.OAuth2.GoogleWebAuthorizationBroker.d_uu4.MoveNext() ---来自引发异常的上一个位置的堆栈结束跟踪---在 System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Google.api.Auth.OAuth2.GoogleWebAuthorizationBroker.d_u1.MoveNext() 我的代码是:C# 从windows共享主机访问Google Drive API时拒绝访问,c#,.net,api,oauth,C#,.net,Api,Oauth,下面的代码在本地环境中运行良好,但在共享宿主环境中运行已发布的代码时面临问题 例外情况: 发生了一个或多个错误 System.Net.HttpListenerException(0x80004005):访问被拒绝 System.Net.HttpListener.AddAllPrefixes()位于 System.Net.HttpListener.Start()位于 Google.api.Auth.OAuth2.LocalServerCodeReceiver.StartListener()位于 G
UserCredential credential;
using (var stream =
new FileStream(HttpContext.Current.Server.MapPath("~/GoogleDrive\\client_secret.json"), FileMode.Open, FileAccess.Read))
{
string credPath = HttpContext.Current.Server.MapPath("~/GoogleDrive\\credentials\\sheets.googleapis.com-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
var driveService = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
string folderId = string.Empty;
folderId = "FolderID here";
string fileExt = Path.GetExtension(fileuploaded.FileName);
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = FileName + fileExt,
Parents = new List<string>
{
folderId
}
};
FilesResource.CreateMediaUpload request;
using (var stream = fileuploaded.InputStream)
{
request = driveService.Files.Create(
fileMetadata, stream, fileuploaded.ContentType);
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
UserCredential凭证;
使用(var)流=
新文件流(HttpContext.Current.Server.MapPath(“~/GoogleDrive\\client\u secret.json”)、FileMode.Open、FileAccess.Read)
{
字符串credPath=HttpContext.Current.Server.MapPath(“~/GoogleDrive\\credentials\\sheets.googleapis.com.net quickstart.json”);
凭证=GoogleWebAuthorizationBroker.AuthorizationAsync(
GoogleClientSecrets.Load(stream.Secrets),
范围,
“用户”,
取消令牌。无,
新文件数据存储(credPath,true))。结果;
}
var driveService=new driveService(new BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=ApplicationName,
});
string folderId=string.Empty;
folderId=“folderId here”;
字符串fileExt=Path.GetExtension(fileupload.FileName);
var fileMetadata=new Google.api.Drive.v3.Data.File()
{
Name=FileName+fileExt,
父母=新名单
{
福德里德
}
};
FileResource.CreateMediaUpload请求;
使用(var stream=fileupload.InputStream)
{
请求=driveService.Files.Create(
fileMetadata、stream、fileuploaded.ContentType);
request.Fields=“id”;
request.Upload();
}
var file=request.ResponseBody;
我已将代码发布到共享windows主机。面临上述错误-访问被拒绝。请在此帮助我解决此问题。您是否使用管理员权限启动应用程序?您可以帮助我如何启动吗?如果您有
exe
文件,只需右键单击它并选择以管理员身份运行
否,它的网站是用MVC开发的,仅包含DLL。在共享的windows主机上发布。你有过这样的经历吗?我在尝试从网站运行时遇到了类似的问题。您是否使用管理员权限启动您的应用程序?您能否帮助我如何启动?如果您有exe
文件,只需右键单击它并选择以管理员身份运行
否,它的网站是用MVC开发的,仅包含DLL。在共享的windows主机上发布。你有过这样的经历吗?我在尝试从网站上运行时遇到了类似的问题