【问题标题】:vb search by keyword youtube api v3vb 按关键字搜索 youtube api v3
【发布时间】:2014-11-17 13:22:32
【问题描述】:

我正在尝试将 google 开发者页面上的“按关键字搜索”示例转换为 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

“语法错误”发生在:

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

“预期语句结束”发生在:

Dim searchListResponse = Await searchListRequest.ExecuteAsync()

“对非共享成员的引用需要对象引用”发生在:

For Each searchResult In SearchListResponse.Items

谢谢

【问题讨论】:

  • 好久没做VB了,不过我觉得第一个语法错误应该用:(New Search()).Run().Wait()
  • 谢谢,但我仍然遇到语法错误。

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


【解决方案1】:
            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

【讨论】:

    猜你喜欢
    • 2015-01-14
    • 2016-07-13
    • 2013-12-03
    • 1970-01-01
    • 2013-09-03
    • 2014-10-22
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多