【发布时间】:2016-01-29 12:36:34
【问题描述】:
我正在尝试在我的 asp.net MVC 项目的 ActionResult 中使用以下代码将视频上传到我的 YouTube 帐户:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload([Bind(Include = " Title, Description")] HttpPostedFileBase uploadFile, Videos videos)
{
var credential = AuthYouTube();
YouTubeService youtubeService = new YouTubeService(new
YouTubeService.Initializer()
{
ApplicationName = "app-name",
HttpClientInitializer = credential
});
// Do Stuff with the video here.
}}
AuthYouTube() 看起来像这样(同一个控制器):
public UserCredential AuthYouTube()
{
string filePath = Server.MapPath("~/Content/YT/client_secret.json");
UserCredential credential;
try{
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows for full read/write access to the
// authenticated user's account.
new[] { YouTubeService.Scope.Youtube },
"username@domain.com",
CancellationToken.None,
new FileDataStore(Server.MapPath("~/Content/YT"),true)
).Result;
};
return credential;
}
catch(EvaluateException ex)
{
Console.WriteLine(ex.InnerException);
return null;
}
}
我已将我从 Google Developer Console 下载的 client_secret.json 存储在 [project]/Content/YT 中。 (也在 /App_Data 文件夹中尝试过。
上传调试器时显示以下消息:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ComponentModel.Win32Exception: Access is denied
发生错误的地方:
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
堆栈跟踪:
[Win32Exception (0x80004005): Access is denied]
Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +115
Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task) +78
Google.Apis.Auth.OAuth2.<AuthorizeAsync>d__1.MoveNext() in C:\Users\mdril\Documents\GitHub\google-api-dotnet-client\Src\GoogleApis.Auth.DotNet4\OAuth2\GoogleWebAuthorizationBroker.cs:59
[AggregateException: One or more errors occurred.]
System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) +4472256
Project.Controllers.VideosController.AuthYouTube() in d:\dev\Development\project\project\Controllers\VideosController.cs:133
project.Controllers.VideosController.Upload(HttpPostedFileBase uploadFile, Videos videos) in d:\dev\project\project\Controllers\VideosController.cs:71
这是什么原因? - 谷歌 API? - 文件夹/IIS 权限?
2016 年 1 月 2 日更新
会不会是 API 端的一些访问错误? 如果没有,请有人提供授予正确 IIS 权限的步骤,授予文件夹权限后仍然出现错误。
运行以下代码确实会在我的 App_Data 中按预期创建文件夹,但也会返回相同的“访问被拒绝”错误。文件夹是空的。
var path = HttpContext.Current.Server.MapPath("~/App_Data/Drive.Api.Auth.Store");
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, userName
, CancellationToken.None
, new FileDataStore(path,true)).Result;
有人可以解释一下如何让它工作吗?
【问题讨论】:
-
添加一个检查它可以找到文件filePath并访问它。
-
@dalmo 已经做好了。文件路径(流)存在。更多操作?
-
确保您有权写入该目录。在我看来,filedatastore 无法创建文件。向 IIS 用户授予管理员权限。
-
检查了我的 IIS 用户(授予它对文件夹的权限),但它仍然是;不工作。您能否提供更多关于设置权利的细节?不知道是不是文件夹/iis权限,会不会是API端的东西?
-
我不是 IIS 专家。但是我看到了这个错误,我只是不记得我做了什么来修复它。我在您的问题中标记了 IIS,幸运的是,有人将 IIS 知识能够为我们提供更多信息。
标签: asp.net-mvc iis youtube-api google-api-client google-api-dotnet-client