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


using vb.net 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 asp.net 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:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
    <div>



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 https://developers.google.com/youtube/v3/docs/videoCategories/list
        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
                Else
                    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, "###,###,###,###,###,###"), ")")

                    Console.WriteLine(strMessage)
                    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.")
        Response.Write(str)
    End Sub
    Protected Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Try
        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 />")
        'Next
        'End Try

    End Sub
End Class

Web.config:

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

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
      <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

  <runtime>

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

      <dependentAssembly>

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

        <bindingRedirect oldVersion="0.0.0.0-4.2.28.0" newVersion="4.2.28.0" />

      </dependentAssembly>

      <dependentAssembly>

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

        <bindingRedirect oldVersion="0.0.0.0-1.0.168.0" newVersion="1.0.168.0" />

      </dependentAssembly>

      <dependentAssembly>

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

        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />

      </dependentAssembly>

      <dependentAssembly>

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

        <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />

      </dependentAssembly>

      <dependentAssembly>

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

        <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />

      </dependentAssembly>

    </assemblyBinding>

  </runtime>

<system.webServer>
    <handlers>
      <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" />
    </handlers>
  </system.webServer></configuration>

        <br /> <asp:Button ID="Button2" runat="server" Text="upload" />
    </div>
        <asp:TextBox ID="TextBox2" runat="server" Height="151px" Width="675px"></asp:TextBox>
    </form>
</body>
</html>
Posted
Comments
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