0

我正在尝试通过其 API 上传到 youtube,而当我在本地进行时,它工作正常。当它在我的 Azure 应用程序上时,它如何获得 (403) Forbidden

这是我的代码:

YouTubeRequestSettings settings = new YouTubeRequestSettings("XXXXX", token, userName, Password);
YouTubeRequest request = new YouTubeRequest(settings);
Video newVideo = new Video();

newVideo.Title = "Test";
newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "car";

newVideo.Description = "My first try";
newVideo.YouTubeEntry.Private = false;
newVideo.Tags.Add(new MediaCategory("devTag",
  YouTubeNameTable.DeveloperTagSchema));
newVideo.YouTubeEntry.MediaSource = new MediaFileSource(**SaveFileToLocal**(fileUpload), fileUpload.ContentType);
Video createdVideo = request.Upload(newVideo);

private string **SaveFileToLocal**(HttpPostedFileBase fileUpload)
{
    string uploadedFilePath;
    if (Local)
    {
        var uploadPath = Server.MapPath("~/AlbumsUploads//" + "RD");
        uploadedFilePath = Path.Combine(uploadPath, fileUpload.FileName);
    }
    else
    {
        LocalResource localResource = RoleEnvironment.GetLocalResource("LocalWebRoleStorage");
        string[] paths = { localResource.RootPath, DateTime.Now.Ticks.ToString() + "_temp" + fileUpload.FileName.Substring(fileUpload.FileName.LastIndexOf('.')) };
        uploadedFilePath = Path.Combine(paths);
    }
    using (var fs = new FileStream(uploadedFilePath, FileMode.Create))
    {
        var buffer = new byte[fileUpload.InputStream.Length];
        fileUpload.InputStream.Read(buffer, 0, buffer.Length);
        fs.Write(buffer, 0, buffer.Length);
    }
    return uploadedFilePath;
}   

这是我的例外。

Google.GData.Client.GDataRequestException: Execution of request failed: https://uploads.gdata.youtube.com/feeds/api/users/default/uploads ---> System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.GetResponse()
at Google.GData.Client.GDataRequest.Execute()
--- End of inner exception stack trace ---
at Google.GData.Client.GDataRequest.Execute()
at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)
at Google.GData.Client.MediaService.EntrySend(Uri feedUri, AtomBase baseEntry, GDataRequestType type, AsyncSendData data)
at Google.GData.Client.Service.Insert(Uri feedUri, AtomEntry newEntry, AsyncSendData data)
at Google.YouTube.YouTubeRequest.Upload(String userName, Video v)
at MvcWedApp.Controllers.AlbumManagementController.UploadToYouTube(HttpPostedFileBase fileUpload, Int32 albumId)
at MvcWedApp.Controllers.AlbumManagementController.Upload(Nullable`1 chunk, String name) 

有什么帮助吗?

4

2 回答 2

1

I ran into a similar problem to your problem:

I talked about this situation with Azure Technical Evangelist and he thinks this problem occur when the Youtube API’s port close (80 and 443), Maybe It’s about Server Firewall properties. So I tried to change many way (Remote to Server and change firewall properties etc ) about open this ports but It doesn’t work.

Finally I solved my problem and I can understand It is not about port access.** I authorized acces to our google account and granted our cloud services with this url**: We understand, we have to generate a new specific password for each application. And also you must generate a DevKey with this URL :

Maybe it could be related to problems ...

于 2012-11-22T17:22:05.660 回答
1

问题是 YourTube 服务器正在拒绝您的 403 授权请求,这意味着来自 ASP.NET Web 角色的身份验证请求由于某种原因而失败。它还解释说您甚至还没有开始上传,因此可能是与网络相关的访问问题。

为了解决此类问题,您可以尝试以下操作:

  1. RDP 到您的 Azure VM 并安装Fiddler并收集网络流量。比较您的桌面流量,看看可能是什么问题。
  2. 您还可以在 Azure VM 中安装网络监视器比较网络流量,以了解这是由于身份验证失败而导致的问题,还是网络 IP/端口等问题。

此外,当您在本地测试时,如果您的 ASP.NET 应用程序在计算机模拟器中运行,它是否有效?

根据以下文档,您被 Google 授权序列卡住了,因此这将是排查问题的第一个地方。根据我的经验,Fiddler 是您最好的朋友,了解根本原因,然后您可以找到解决方案: https ://developers.google.com/youtube/2.0/developers_guide_protocol#Process_Flows_for_Uploading_Videos

注意:无论如何,这不是一个解决方案,但是我写在这里是为了解释如何解决这个问题。

于 2012-05-23T17:49:42.040 回答