Error: "message": "Login Required" when use Youtube Analytics API

Error: "message": "Login Required" when use Youtube Analytics API

我正在使用 YouTube api。当我点击这个 url "https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date=2015-01-01&end-date=2016-01-31&metrics=likes%2Cdislikes&key={API Key}"

它给出 401

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

但我在资源管理器中点击了“https://developers.google.com/apis-explorer/?” 它工作正常。
如何使第一个请求生效?

在您的请求中,您正在发送 key={您的密钥} 作为您应该发送的访问令牌 access_token={您的 oauth2 访问令牌}

注意:密钥用于 public 请求。访问令牌用于经过身份验证的请求。

如果其他人在 Google API 上使用 JWT 身份验证时偶然发现了这个问题(例如,在使用服务帐户时),请确保在您的 [=24] 中包含 auth: <your jwtClient> =] 打电话,比如:

首先获取token:

// Configure JWT auth client
var privatekey = require("./<secret>.json")
var jwtClient = new google.auth.JWT(
  privatekey.client_email,
  null,
  privatekey.private_key,
  ['https://www.googleapis.com/auth/drive']
);

// Authenticate request
jwtClient.authorize(function (err, tokens) {
  if (err) {
    return;
  } else {
    console.log("Google autorization complete");
  }
});

然后,调用 API (但不要忘记 auth:jwtClient 部分)

drive.files.create({
    auth: jwtClient,
    resource: {<fileMetadata>},
    fields: 'id'
  }, function (err, file) {
    if (err) {
      // Handle error
    } else {
      // Success is much harder to handle
    }
});