使用Google Book api为.Net查找一(1)本书

时间:2013-11-04 15:46:13

标签: google-api-dotnet-client

我已经尝试了几个小时,但我无法让它发挥作用。我使用以下代码(VB)

Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim credential As UserCredential = Await GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets With {.ClientId = "my key", .ClientSecret = "my secret"}, New String() {BooksService.Scope.Books}, "user", CancellationToken.None, New FileDataStore("Books.ListMyLibrary"))
        Dim service As New BooksService(New BaseClientService.Initializer With {.HttpClientInitializer = credential, .ApplicationName = "LibraryX"})

        'Dim bookselve As Bookshelves = Await service.Mylibrary.Bookshelves.List.ExecuteAsync
        Dim mm = service.Volumes.Get("9780330441230")

    End Sub

它没有给我关于这本书的信息(The Camel Club,David Baldacci)

有人能帮助我吗?

问候, 史蒂芬

20131106

我做了一些改变:

Private Async Sub Form1_Load(sender as Object,e As EventArgs)处理MyBase.Load

    Dim credential As UserCredential = Await GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets With {.ClientId = "my key", .ClientSecret = "my secret"},
                                                                                         New String() {BooksService.Scope.Books}, "user", CancellationToken.None, New FileDataStore("Books.ListMyLibrary"))
    Dim service As New BooksService(New BaseClientService.Initializer With {.HttpClientInitializer = credential, .ApplicationName = "LibraryX"})

    'Dim bookselve As Bookshelves = Await service.Mylibrary.Bookshelves.List.ExecuteAsync
    Try
        rslt = Await service.Volumes.Get("9780330441230").ExecuteAsync
        Dim y As Integer = 0
    Catch gaex As Google.GoogleApiException
        ErrorMessageTextBox.Text = gaex.Message
    Catch ex As AggregateException
        ErrorMessageTextBox.Text = ex.Message
    End Try


End Sub

但请求的结果是:

enter code here Google.Apis.Requests.RequestError 找不到卷ID。 [404] 错误[     消息[无法找到卷ID。]位置[ - ]原因[notFound]域[全局] ]

知道VolumeId必须从Google获取图书信息吗?

2 个答案:

答案 0 :(得分:1)

知道了!您应该在GetRequest对象上使用Execute或ExecuteAsync方法。 所以你的最后一行代码应该是这样的:

Dim mm = Await service.Volumes.Get("9780330441230").ExecuteAsync()

答案 1 :(得分:0)

我使用Google API Explorer并在books.volumes.list方法中运行以下查询,q = intitle:“The Camel Club”; inauthoer:David Baldacci

您可以在以下链接https://developers.google.com/apis-explorer/#p/books/v1/books.volumes.list?q=intitle%253A%2522The+Camel+Club%2522%253Binauthor%253A+David+Baldacci&_h=13&中找到它。

我不是此API的专家,但我在https://developers.google.com/books/docs/v1/using#PerformingSearch中找到了所有必需的信息。 我很确定你可以优化查询,但这是一个很好的开始。

更新(添加.NET代码):
使用Google API资源管理器提供的每项操作也可以使用.NET客户端库获得,如下所示:

var service = new BooksService(new BaseClientService.Initializer()
    {
        // HttpClientInitializer = credential,
        ApplicationName = "Books API Sample",
    });
var volumes = await service.Volumes.List(
      "intitle:\"The Camel Club\";inauthor: David Baldacci").ExecuteAsync();
foreach (var item in volumes.Items)
{
    Console.WriteLine(item.VolumeInfo.Title);
}

请注意,在这种情况下我们不需要UserCredential,因为我们不查询用户的特定数据。如果要查询Mylibrary,则应使用UserCredential和经过身份验证的请求。