node.js - 在 Nodejs 中使用 Google API 进行服务身份验证

标签 node.js api authentication google-api jwt

我正在尝试将 YouTube 数据 API V3 与 Node 结合使用,以尝试在进行视频搜索时提供经过身份验证的代理。由于该服务将在服务器上运行,因此我创建了一个服务帐户。

这会导致下载 key 文件和以下信息:

我对 JWT 有非常基本的了解,但是 OAuth2 page 上的服务部分建议您应该使用他们的库之一来执行此操作,而不是自己实现。然而,他们没有在他们的 supported libraries 中列出 Node 库。 .

再深入一点,我发现了 Google API Nodejs 客户端:https://github.com/google/google-api-nodejs-client它声称受 Google 支持,并且开箱即用地支持 OAuth2。

文档相当少,身份验证示例涉及将 URL 打印到终端,然后在浏览器中访问它以生成 token 。我深入研究了源代码,看看它是否支持 JWT,它似乎有一个 JWTClient。内置。

/**
 * @constructor
 * JWT service account credentials.
 *
 * Retrieve access token using gapitoken.
 *
 * @param {string=} email service account email address.
 * @param {string=} keyFile path to private key file.
 * @param {array=} scopes list of requested scopes.
 * @param {string=} subject impersonated account's email address.
 *
 */
function JWT(email, keyFile, key, scopes, subject) {
  JWT.super_.call(this);
  this.email = email;
  this.subject = subject;
  this.keyFile = keyFile;
  this.key = key;
  this.scopes = scopes;
  this.GAPI = GAPI;
}

构造函数上方的文档注释似乎是这里唯一的解释,我仍然不确定如何使用此函数。

我假设:

  • email 是您的开发者帐户的电子邮件地址
  • keyFile是私钥的路径
  • key 是私钥? (如果是这样,为什么还要包括路径?)
  • scopes 是您要访问的 API 范围的数组
  • 主题 ???

有没有人以前使用过这项服务,或者能够阐明这里的差异?

作为引用,这必须是服务器端,因为我需要对请求的身份验证是自主的,而且我不能只使用未经身份验证的请求,因为我超过了 200 个请求(或类似的东西)的每日限额.

最佳答案

我已经设法拼凑出一个可行的解决方案,所以我会把它留在这里供以后遇到类似问题的其他人使用。

repo 中有一个 JWT 示例,它更详细地展示了如何使用 JWT 对象的构造函数。

https://github.com/google/google-api-nodejs-client/blob/master/examples/jwt.js

这里是文档注释的略微修改版本。

/**
 * @constructor
 * JWT service account credentials.
 *
 * Retrieve access token using gapitoken.
 *
 * @param {string=} email service account email address from developer console.
 * @param {string=} keyFile absolute path to private key file.
 * @param {string=} key the contents of the key if you are loading it yourself (optional)
 * @param {array=} scopes list of requested scopes.
 * @param {string=} subject impersonated account's email address (optional).
 *
 */

完成后的代码是这样的

// Create the JWT client
var authClient = new googleapis.auth.JWT(email, keypath, key,
  ['https://www.googleapis.com/auth/youtube']);

// Authorize it to produce an access token
authClient.authorize(function(err, tokens) {
  if(err) throw err;

  // Use discovery to get the youtube api
  googleapis.discover('youtube', 'v3')
  .execute(function(err, client) {

    // Create a search
    var search = client.youtube.search.list({
      q: '<query string>',
      part: 'snippet',
      maxResults: 50
    });

    // Authenticate with current auth client
    search.authClient = authClient;

    // Hey presto!
    search.execute(function(err, results) {
      if(err) throw err;
      console.log(results.items);
    });

  });

});

这个代码示例很乱,但足以让您了解思路。根据示例,您应该能够执行以下操作:

client.youtube.search.list({
 ... 
})
.withAuth(authClient)
.execute(...); 

但由于某些原因,withAuth 方法不存在。花了一些时间来弄清楚它做了什么,据我所知,如果您手动设置 authClient 属性,它工作正常,如上所示。

作为旁注,err 参数不是错误,但 POJO 并像上面那样抛出它们实际上不起作用。除非您很高兴看到 [Object object] 作为您的调试信息。

希望该库能尽快得到一些适当的文档关注,这样的问题将得到解决。

关于node.js - 在 Nodejs 中使用 Google API 进行服务身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23181339/

相关文章:

node.js - 使用 Yeoman/Grunt 设置 Jade 进行模板化的正确方法

node.js - 使用安全网关将 nodeJs 应用程序连接到本地服务器

javascript - JSON.parse 和 JSON.stringify

security - 客户端身份验证对于 k8s 来说足够好吗?

javascript - (ui-)router 中的 Angular-app、身份验证和解析器顺序

ios - 与 parse-server 和 auth0 的自定义身份验证集成

node.js - 是否可以用pug编写一个javascript文件

javascript - jscs 错误 : validateLineBreaks: Invalid line break at filename. js

api - 使用 OAuth 和 PEAR Services_Twitter 获取 Twitter 请求 token 时出错

java - JNA:找不到指定的过程