Click here to Skip to main content
15,793,547 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
down vote favorite

using i'm trying to upload a youtube video using the API v3, what we need is : -Uploading a youtube using the provided json file from Google console. -Running on IIS local webserver ex (localhost/ApplicationName/..) and Not (localhost:port/..)

the problem is IIS, whenever uploading from a specific port everything is perfect. but my application will be accessed from different devices that's why i needed to have my application running on IIS webserver directly.

what i'm having is a infinite loop in GoogleWebAuthorizationBroker.AuthorizeAsync that never ends. i'm not getting any error, the API never return a response

below is my code, please advice:

<pre><pre lang="xml"><%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication4.WebForm1" Async="true"%>

<!DOCTYPE html>

<html xmlns="">
<head id="Head1" runat="server">
    <form id="form2" runat="server">

Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports Google.Apis.Upload
Imports Google.Apis.Util.Store
Imports Google.Apis.YouTube.v3
Imports Google.Apis.YouTube.v3.Data

Imports System
Imports System.IO
Imports System.Reflection
Imports System.Threading
Imports System.Threading.Tasks

Public Class WebForm1
    Inherits System.Web.UI.Page

    Dim lngFileLength As Long = 0
    Dim dblProgressPercent As Double = 0.0

    Private Async Function Run() As Task
        Dim credential As UserCredential

        Dim myClientSecretLocation As String = HttpContext.Current.Server.MapPath("client_secrets.json")

        Using stream = New FileStream(myClientSecretLocation, FileMode.Open, FileAccess.Read)
            credential = Await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, _
                  {YouTubeService.Scope.Youtube, YouTubeService.Scope.Youtubepartner, YouTubeService.Scope.YoutubeUpload, YouTubeService.Scope.YoutubepartnerChannelAudit, YouTubeService.Scope.YoutubeReadonly}, _
                  "user", CancellationToken.None, Nothing) 'myStorageFolderLocation
        End Using

        Dim youtubeService__1 = New YouTubeService(New BaseClientService.Initializer() With { _
              .HttpClientInitializer = credential, _
              .ApplicationName = Assembly.GetExecutingAssembly().GetName().Name _

        Response.Write("Passed<br />")
        Dim video = New Video()
        video.Snippet = New VideoSnippet()
        video.Snippet.Title = "Default Video Title"
        video.Snippet.Description = "Default Video Description"
        video.Snippet.Tags = New String() {"tag1", "tag2"}
        video.Snippet.CategoryId = "22"
        ' See
        video.Status = New VideoStatus()
        video.Status.PrivacyStatus = "unlisted" ' or "private" or "public"
        Dim filePath = "c:\Small Video.mp4"
        lngFileLength = New FileInfo(filePath).Length
        dblProgressPercent = 0.0

        Using fileStream = New FileStream(filePath, FileMode.Open)
            Dim videosInsertRequest = youtubeService__1.Videos.Insert(video, "snippet,status", fileStream, "video/*")
            AddHandler videosInsertRequest.ProgressChanged, AddressOf videosInsertRequest_ProgressChanged
            AddHandler videosInsertRequest.ResponseReceived, AddressOf videosInsertRequest_ResponseReceived
            Await videosInsertRequest.UploadAsync()
        End Using

    End Function

    Private Sub videosInsertRequest_ProgressChanged(objUploadStatus As Global.Google.Apis.Upload.IUploadProgress)
        Select Case objUploadStatus.Status
            Case UploadStatus.Completed
                Console.WriteLine("Status: Completed")

            Case UploadStatus.Starting
                Console.WriteLine("Status: Starting")

            Case UploadStatus.NotStarted
                Console.WriteLine("Status: Not Started")

            Case UploadStatus.Uploading
                Dim dblPercent As Double = 0

                If lngFileLength > 0 Then
                    dblPercent = (100 * objUploadStatus.BytesSent) / lngFileLength
                    dblPercent = 0
                End If

                Dim p As Integer = CInt(Int(dblPercent))
                If p > CInt(Int(dblProgressPercent)) Then

                    Dim strMessage As String = String.Concat("Status: Uploading ", Format(p, "###"), _
                        "% (", Format(objUploadStatus.BytesSent, "###,###,###,###,###,###"), ")")

                    dblProgressPercent = dblPercent
                End If

                Console.WriteLine(objUploadStatus.BytesSent & " bytes sent.")
                Exit Select

            Case UploadStatus.Failed
                Console.WriteLine("An error prevented the upload from completing." & vbLf & objUploadStatus.Exception.ToString)
                Exit Select
        End Select
    End Sub

    Private Sub videosInsertRequest_ResponseReceived(video As Video)
        Dim str As String = ("Video id " & video.Id & " was successfully uploaded.")
    End Sub
    Protected Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Await Run()
        TextBox1.Text = "done"
        'Catch ex As AggregateException
        '    Dim er
        'For Each er In ex.InnerExceptions
        '    Response.Write("Error: " & Convert.ToString(er.Message) & " <br />")
        'End Try

    End Sub
End Class


<?xml version="1.0" encoding="utf-8"?>

  For more information on how to configure your ASP.NET application, please visit

      <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />


    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">


        <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />

        <bindingRedirect oldVersion="" newVersion="" />



        <assemblyIdentity name="Microsoft.Threading.Tasks.Extensions.Desktop" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />

        <bindingRedirect oldVersion="" newVersion="" />



        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />

        <bindingRedirect oldVersion="" newVersion="" />



        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />

        <bindingRedirect oldVersion="" newVersion="" />



        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />

        <bindingRedirect oldVersion="" newVersion="" />




      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

        <br /> <asp:Button ID="Button2" runat="server" Text="upload" />
        <asp:TextBox ID="TextBox2" runat="server" Height="151px" Width="675px"></asp:TextBox>
evry1falls 17-May-20 15:51pm    
Did you find a solution ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900