简体   繁体   English

为什么我的 api 请求返回一个空对象?

[英]Why my api request returns an empty object?

So I use Google Books API in my application, and i make a request do display data in console, but for some reason it returns an empty object, instead of array of books.所以我在我的应用程序中使用了 Google Books API,并且我发出一个请求在控制台中显示数据,但由于某种原因它返回一个空对象,而不是书籍数组。

Javascript code: Javascript代码:

const API_KEY = 'key=<API-KEY>';
const BASE_URL = 'https://www.googleapis.com/books/v1/';
const API_URL = BASE_URL + 'volumes?q=subject:fiction' + API_KEY;

getBooks(API_URL);

function getBooks(url){
    fetch(url).then(res => res.json()).then(data =>{
        console.log(data);
    })
}

Console in browser:浏览器中的控制台:

在此处输入图片说明

Edit: So I added '&' in the url and it changed, but now it gives 403 error:编辑:所以我在 url 中添加了 '&' 并且它改变了,但现在它给出了 403 错误:

{
  "error": {
    "code": 403,
    "message": "Requests from this Android client application \u003cempty\u003e are blocked.",
    "errors": [
      {
        "message": "Requests from this Android client application \u003cempty\u003e are blocked.",
        "domain": "global",
        "reason": "forbidden"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}

Maybe I got the wrong key?也许我拿错了钥匙? I got it from Google Books credentials, so I thought it should've worked.我是从 Google Books 凭据中获得的,所以我认为它应该有效。

You forgot the & sign between the query params, so you need to change您忘记了查询参数之间的&符号,因此您需要更改

const API_URL = BASE_URL + 'volumes?q=subject:fiction' + API_KEY;

to

const API_URL = BASE_URL + 'volumes?q=subject:fiction&' + API_KEY;

And also revoke this API Key并且还撤销这个 API Key

您将密钥作为 q 值的一部分发送,因为您忘记了“&”。

const API_URL = BASE_URL + 'volumes?q=subject:fiction&' + API_KEY;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM