按关键字youtube api v3搜索vb

时间:2014-11-17 13:22:33

标签: vb.net youtube youtube-api youtube-data-api

我试图通过关键字转换"搜索"谷歌开发者页面上的示例到vb。我得到了一些我无法解决的错误。 https://developers.google.com/youtube/v3/code_samples/dotnet#search_by_keyword

Imports System.Collections.Generic
Imports System.IO
Imports System.Reflection
Imports System.Threading
Imports System.Threading.Tasks

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

Namespace Google.Apis.YouTube.Samples
' <summary>
' YouTube Data API v3 sample: search by keyword.
' Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
' See https://code.google.com/p/google-api-dotnet-client/wiki/GettingStarted
'
' Set ApiKey to the API key value from the APIs & auth > Registered apps tab of
'   https://cloud.google.com/console
' Please ensure that you have enabled the YouTube Data API for your project.
' </summary>
Friend Class Search
    <STAThread()> _
    Private Shared Sub Main(args As String())
        Console.WriteLine("YouTube Data API: Search")
        Console.WriteLine("========================")

        Try
            New Search().Run().Wait()
        Catch ex As AggregateException
            For Each e In ex.InnerExceptions
                Console.WriteLine("Error: " & e.Message)
            Next
        End Try

        Console.WriteLine("Press any key to continue...")
        Console.ReadKey()
    End Sub

    Private Function Run() As Task
        Dim youtubeService = New YouTubeService(New BaseClientService.Initializer()   With { _
         .ApiKey = "REPLACE_ME", _
         .ApplicationName = Me.[GetType]().ToString() _
        })

        Dim searchListRequest = youtubeService.Search.List("snippet")
        searchListRequest.Q = "Google"
        ' Replace with your search term.
        searchListRequest.MaxResults = 50

        ' Call the search.list method to retrieve results matching the specified query term.
        Dim searchListResponse = Await searchListRequest.ExecuteAsync()

        Dim videos As New List(Of String)()
        Dim channels As New List(Of String)()
        Dim playlists As New List(Of String)()

        ' Add each result to the appropriate list, and then display the lists of
        ' matching videos, channels, and playlists.
        For Each searchResult In SearchListResponse.Items
            Select Case searchResult.Id.Kind
                Case "youtube#video"
                    videos.Add([String].Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId))
                    Exit Select

                Case "youtube#channel"
                    channels.Add([String].Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId))
                    Exit Select

                Case "youtube#playlist"
                    playlists.Add([String].Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId))
                    Exit Select
            End Select
        Next

        Console.WriteLine([String].Format("Videos:" & vbLf & "{0}" & vbLf, String.Join(vbLf, videos)))
        Console.WriteLine([String].Format("Channels:" & vbLf & "{0}" & vbLf, String.Join(vbLf, channels)))
        Console.WriteLine([String].Format("Playlists:" & vbLf & "{0}" & vbLf, String.Join(vbLf, playlists)))
    End Function
End Class
End Namespace

A&#34;语法错误&#34;发生在:

Try
    New Search().Run().Wait()

&#34;预期声明结束&#34;发生在:

Dim searchListResponse = Await searchListRequest.ExecuteAsync()

A&#34;对非共享成员的引用需要一个对象引用&#34;发生在:

For Each searchResult In SearchListResponse.Items

由于

1 个答案:

答案 0 :(得分:0)

            dim item as string = "searchitem"
            Dim youtubeService = New YouTubeService(New BaseClientService.Initializer() With {
     .ApiKey = "KEYHERE",
     .ApplicationName = Me.[GetType]().ToString()
    })

            Dim searchListRequest = youtubeService.Search.List("snippet")
            searchListRequest.Q = item

            Dim searchListResponse = searchListRequest.Execute()             
            searchListRequest.MaxResults = 1

            ' Call the search.list method to retrieve results matching the specified query term.

            Dim videos As New List(Of String)()
            Dim channels As New List(Of String)()
            Dim playlists As New List(Of String)()
            Dim resultid As String = ""
            Dim resulttitle As String = ""
            ' Add each result to the appropriate list, and then display the lists of
            ' matching videos, channels, and playlists.
            For Each searchResult In searchListResponse.Items
                Select Case searchResult.Id.Kind
                    Case "youtube#video"
                        resulttitle = searchResult.Snippet.Title
                        resultid = searchResult.Id.VideoId
                        Exit For
                        videos.Add([String].Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId))
                        Exit Select

                    Case "youtube#channel"
                        channels.Add([String].Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId))
                        Exit Select

                    Case "youtube#playlist"
                        playlists.Add([String].Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId))
                        Exit Select
                End Select
            Next