2012-05-22 56 views
0

我試圖上傳到YouTube,通過它的API, 雖然我在做它本地化,但它工作正常。 如何過當它在我的Azure的應用它得到的(403)禁止通過Azure的API上傳到Youtube

這裏是我的代碼:

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) 

任何幫助任何人嗎?

+0

這似乎是構造函數'YouTubeRequestSettings',需要一個用戶名和密碼[這一個](HTTP://google-gdata.googlecode。 com/svn-history/r902/docs/folder56/M_Google_YouTube_YouTubeRequestSettings__ctor_2.htm)(5個參數)而不是你的4參數構造函數? –

+0

感謝您的回覆。但YouTubeRequestSettings有4個過載。加上它在我的本地環境中工作,所以沒有什麼錯。 – user730895

+0

是否有可能我的Azure RoleEnvironment LocalResource文件沒有正確的權限? – user730895

回答

1

問題是YourTube服務器拒絕您的授權請求與403這意味着從ASP.NET Web Role認證請求由於某種原因失敗。它還解釋說,你甚至還沒有開始上傳,所以它可能是網絡相關的訪問問題。

而且爲了解決這樣的問題,你可以嘗試以下方法:

  1. RDP到您的Azure的虛擬機並安裝Fiddler和收集網絡流量。比較一下您的桌面流量並查看可能存在的問題。
  2. 您還可以安裝網絡監視器在Azure中的VM比較網絡流量以瞭解是否是由於身份驗證的問題是失敗的還是有一些問題,網絡IP /端口等

此外,當您在本地測試,如果您的ASP.NET應用程序在計算機模擬器中運行,它是否工作?

根據以下文檔,您將被禁止使用Google授權序列,因此將成爲第一個排查問題的地方。根據我的經驗小提琴手是你最好的朋友的理解根本原因,然後你可以找到解決辦法: https://developers.google.com/youtube/2.0/developers_guide_protocol#Process_Flows_for_Uploading_Videos

注:以任何方式,這不是一個解決方案,但是我正在寫在這裏解釋如何解決這個問題。

1

我遇到了類似的問題,您的問題:

我談過這個情況與Azure的技術專家,他認爲如果YouTube API的端口關閉(80和443),也許這是關於服務器防火牆這個問題發生屬性。 所以我試圖改變很多方式(遠程服務器和更改防火牆屬性等)關於打開此端口,但它不起作用。

最後我解決了我的問題,我可以理解它不是關於端口訪問。** I authorized acces to our google account and granted our cloud services with this url**: 我們知道,我們必須爲每個應用程序生成一個新的特定密碼。 And also you must generate a DevKey with this URL :

也許這可能涉及到的問題...

相關問題