我正在尝试将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
谢谢
最佳答案
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