【问题标题】:Find one (1) book with Google Book api for .Net使用适用于 .Net 的 Google Book api 查找一 (1) 本书
【发布时间】:2013-11-07 23:21:13
【问题描述】:

我已经尝试了几个小时,但我无法让它工作。我使用以下代码(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

它没有给我关于这本书的信息(骆驼俱乐部,大卫巴尔达奇)

有人可以帮帮我吗?

您好, 史蒂文

20131106

我做了一些改变:

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
    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 hereGoogle.Apis.Requests.RequestError 找不到卷 ID。 [404] 错误 [ 消息[找不到卷 ID。]位置[-]原因[未找到]域[全局] ]

知道 VolumeId 必须是什么才能从 Google 获取图书信息吗?

【问题讨论】:

  • API 正在寻找客户端密钥和密钥。您是从上面的示例中删除了这些,还是在尝试代码时没有包括它们?
  • 你有异常吗? mm的内容是什么?
  • 是的,我故意在这篇文章中留下了密钥和秘密消息。
  • ?mm {Google.Apis.Books.v1.VolumesResource.GetRequest} Alt:无 国家:无 ETagAction:默认 {0} 字段:无 HttpMethod:“GET”键:无 记录器:{Google .Apis.Logging.NullLogger} MethodName: "get" OauthToken: Nothing Partner: Nothing PrettyPrint: Nothing Projection: Nothing QuotaUser: Nothing RequestParameters: Count = 12 RestPath: "volumes/{volumeId}" Service: {Google.Apis.Books. v1.BooksService} 服务:{Google.Apis.Books.v1.BooksService} 来源:Nothing UserIp:Nothing VolumeId:“9780330441230”

标签: google-api-dotnet-client


【解决方案1】:

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

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

【讨论】:

  • peleyal,非常感谢,但我仍然无法获取图书信息,请参阅我的原始问题
  • 您确定这是正确的音量吗?你得到 404 这意味着这个卷不存在。首先尝试使用我们的 Google API Explorer (developers.google.com/apis-explorer/#p/books/v1/…) 获取它,在您看到它在那里工作后,您应该尝试使用您的代码使用相同的 ID。请从您提供的示例代码中删除您的客户机密并删除此客户,然后创建一个新客户。您不应该在线提供这些信息!
  • 嗨 peleyal,你是赖特,我已经更改了我的密钥和程序,所以它们不会再显示了。我已经用至少 15 个 ISBN 号测试了您的网址,但它们都没有返回任何内容。我已经用英语和荷兰语书籍进行了测试。没有结果!
【解决方案2】:

我使用 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 和经过身份验证的请求。

【讨论】:

  • 嘿 peleyal,我想我们说的是不同的语言。您说的是通过 REST 进行通信,带有 url,例如我说的是通过 DOTNET 客户端库查找书籍。这是一个很大的不同。
  • 这正朝着正确的方向发展,谢谢 peleyal。还有一个问题: Dim rslt = Await service.Volumes.List("isbn: 9789022992364").ExecuteAsync Dim rslt = Await service.Volumes.Get("q=isbn: 9789022992364").ExecuteAsync 第一个查询给出的结果是3其中第一个是请求的书籍,第二个查询仍然产生 404 Not Found。您知道 Volumes.Get 请求的查询字符串必须是什么才能获得好的结果吗?
猜你喜欢
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
  • 1970-01-01
  • 2022-11-14
相关资源
最近更新 更多