错误:“消息”:使用 Youtube 分析 API 时需要登录

Posted

技术标签:

【中文标题】错误:“消息”:使用 Youtube 分析 API 时需要登录【英文标题】:Error: "message": "Login Required" when use Youtube Analytics API 【发布时间】:2016-04-25 22:44:32 【问题描述】:

我正在使用 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"
 

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

【问题讨论】:

您应该解决您的问题,这是对 YouTube 分析 API 的请求。您需要使用 Oauth2 对该请求进行身份验证。 我已经拥有来自 Oauth 的访问令牌,但方式不同。现在如何获取这些数据? 尝试googleapis.com/youtube/analytics/v1/…来自oauth的访问令牌 key= 用于公共 api 密钥。 Access_token= 是当您拥有 oauth2 访问令牌时。 感谢@Dalm。它的工作 【参考方案1】:

在您的请求中,您发送 key=your key 以获得访问令牌,您应该发送 access_token=your oauth2 access token

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

【讨论】:

有没有办法在没有 Oauth 令牌的情况下使用我的密钥?我知道它不安全,但这只是我试图快速制定的概念证明。以后可能会用 Oauth 代替【参考方案2】:

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

首先,获取令牌:

// 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
    
);

【讨论】:

以上是关于错误:“消息”:使用 Youtube 分析 API 时需要登录的主要内容,如果未能解决你的问题,请参考以下文章

YouTube V3 API 播放列表项 resourceIdRequired

使用 Youtube Data API V3 和 Google API Client PHP 将视频上传到 Youtube - 获取 401(未经授权)消息

Youtube iframe API 无法发布消息

YouTube V3 API 配额超出每日限额仅为 10k

尝试使用 YouTube 分析 API 检索指标时禁止出现 403

Youtube API:搜索可在移动设备上播放的视频