在本地和服务器上使用Youtube ApiV3

时间:2016-03-07 06:08:15

标签: c# youtube-api youtube-data-api

我是C#的初学者,所以如果用简单的词语解释这个问题会很有帮助。我有关于使用YouTube API v3在我的YouTube频道上通过服务器上传视频的问题,但是当我在本地运行我的代码时,我没有上传视频的问题。但是在IIS / Server上托管会出现对象引用错误。 下面是我尝试执行的用于视频上传的代码,该代码是在上传按钮上写的。

protected async void btnUpload_Click(object sender, EventArgs e)
{
    if (txtVideoTitle.Text != "" && txtFileUpload.HasFile)
    {
        date = DateTime.Now;
        string CLIENT_ID = ConfigurationManager.AppSettings["ClientId"];
        string CLIENT_SECRET = ConfigurationManager.AppSettings["ClientSecret"];                
        var youtubeService = AuthenticateOauth(CLIENT_ID, CLIENT_SECRET,"singleuser");
        if (youtubeService == null) throw new  ArgumentNullException("youtubeService");
        var video = new Video();
        video.Snippet = new VideoSnippet();
        video.Snippet.Title = txtVideoTitle.Text;
        video.Snippet.Description = txtVideoDescription.Text;
        video.Snippet.Tags = new string[] { "tag1", "tag2" };                
        video.Status = new VideoStatus();
        video.Status.PrivacyStatus = "unlisted";
        if (video == null) throw new ArgumentNullException("video");
        var filename = txtFileUpload.FileName;
        Stream filepath = txtFileUpload.PostedFile.InputStream;
        var videosInsertRequest = youtubeService.Videos.Insert(video,"snippet,status", filepath, "video/*");
        videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
        await videosInsertRequest.UploadAsync();
        message.Text = "Video Uploaded Successfully!!!";
    }
    else
    {
        message.Text = "Please enter required details...";
    }
}
 public static YouTubeService AuthenticateOauth(string cLIENT_ID, string cLIENT_SECRET, string v)
    {
        string[] scopes = new string[] { YouTubeService.Scope.Youtube,
                                         YouTubeService.Scope.YoutubeForceSsl,
                                         YouTubeService.Scope.Youtubepartner,
                                         YouTubeService.Scope.YoutubepartnerChannelAudit,
                                         YouTubeService.Scope.YoutubeReadonly,
                                         YouTubeService.Scope.YoutubeUpload};

        try
        {
            // 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 = cLIENT_ID, ClientSecret = cLIENT_SECRET }
                                                                                         , scopes
                                                                                         , v
                                                                                         , CancellationToken.None
                                                                                         , new FileDataStore("Drive.Auth.Store")).Result;

            YouTubeService service = new YouTubeService(new YouTubeService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Web"
            });
            if (service == null) throw new ArgumentNullException("service");
            return service;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.InnerException);
            return null;
        }
    }

正如您在上面的代码中看到的,我已经处理了youtube服务,它会抛出异常,因为它在我在公司网络上的服务器或IIS上托管它时会收到空值。但是当我在本地运行我的代码时(开发机器)它没有&抛出任何异常。 我花了几天时间解决问题,但无济于事。任何帮助都将受到赞赏。 异常消息是 ' /'中的服务器错误应用。 值不能为空。 参数名称:youtubeService 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息。

异常详细信息:System.ArgumentNullException:值不能为null。 参数名称:youtubeService

0 个答案:

没有答案