使用Youtube Analytics API时出现错误:"消息":"需要登录"

Kis*_*ore 17 google-api google-oauth youtube-data-api youtube-analytics-api

我正在使用youtube api.当我点击此网址时" 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"
 }
}
Run Code Online (Sandbox Code Playgroud)

但是我在浏览器中点击了" https://developers.google.com/apis-explorer/?" 它工作正常.
如何使第一个请求起作用?

DaI*_*mTo 21

在您的请求中,您发送了key = {your key}以获取您应发送的访问令牌access_token = {your oauth2 access token}

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


Dav*_*mon 5

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

首先,获取令牌:

// 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");
  }
});
Run Code Online (Sandbox Code Playgroud)

然后,调用 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
    }
});
Run Code Online (Sandbox Code Playgroud)