Dan*_*nce 4 authentication api google-api node.js jwt
我正在尝试将YouTube Data API V3与Node一起使用,以尝试提供经过身份验证的代理,以进行视频搜索.由于服务将在服务器上运行,我已经创建了一个服务帐户.
这导致下载密钥文件和以下信息:
我对JWT有一个非常基本的了解,但OAuth2页面上的服务部分建议你应该使用他们的一个库来完成它,而不是自己实现它.但是,它们不会在其支持的库中列出节点库.
稍微挖掘一下,我遇到了Google API Nodejs客户端:https://github.com/google/google-api-nodejs-client,声称得到了Google的支持,并且开箱即用,支持OAuth2.
文档相当小,身份验证的示例包括将URL打印到终端,然后在浏览器中访问它以生成令牌.我对源代码进行了挖掘,看它是否支持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;
}
Run Code Online (Sandbox Code Playgroud)
这个构造函数上方的doc注释似乎是这里唯一的解释,我仍然不确定如何使用这个函数.
我假设:
email
是您的开发者帐户的电子邮件地址keyFile
是私钥的路径key
是私钥?(如果是这样,为什么还包括路径?)scopes
是您要访问的API范围数组subject
???有没有人以前使用过这项服务,或者能够对这里的差异略有说明?
作为参考,这必须是服务器端,因为我需要身份验证请求是自治的,我不能只使用未经过身份验证的请求,因为我在200个请求(或类似的东西)中超过了我的每日允许量.
Dan*_*nce 12
我已经设法将一个有效的解决方案拼凑在一起,所以我将把它留给其他任何在将来遇到类似问题的人.
repo中有一个JWT示例,它更详细地显示了如何使用JWT对象的构造函数.
https://github.com/google/google-api-nodejs-client/blob/master/examples/jwt.js
这是doc评论的略微修改版本.
/**
* @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).
*
*/
Run Code Online (Sandbox Code Playgroud)
完成的代码看起来像这样
// 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);
});
});
});
Run Code Online (Sandbox Code Playgroud)
这个代码示例很乱,但它足以让您了解这个想法.根据您应该能够做到的示例:
client.youtube.search.list({
...
})
.withAuth(authClient)
.execute(...);
Run Code Online (Sandbox Code Playgroud)
但由于某些原因,这种withAuth
方法并不存在.我花了一些时间来研究它做了什么,据我所知,如果你authClient
手动设置属性,它就可以正常工作,如上所示.
作为旁注,err
参数不是错误,但POJO和如上所述抛出它们实际上并不起作用.除非您乐意将其[Object object]
视为调试信息.
希望图书馆尽快得到一些适当的文档注意,这样的问题将被解决.
归档时间: |
|
查看次数: |
6108 次 |
最近记录: |