我正试图制作一个服务器应用程序,将用户添加/删除到我的域组。请注意,它不会与用户进行任何交互,它是服务器到服务器的应用程序。如何使用JWT使用NodeJS客户端库访问Google Directory(Admin SDK)?
我登记我在谷歌API控制台应用程序,下载的钥匙,并转化它通过发出
openssl pkcs12 -in my_google_key.p12 -out my_google_key.pem -nocerts -nodes
然后我去过的域管理,安全为.pem - >高级设置 - >认证 - >管理OAuth客户端访问。我在授权API客户端添加了一条记录。我用我从服务帐户在控制台中得到了客户ID和使用范围:
https://www.googleapis.com/auth/admin.directory.group.
我安装的NodeJS googleapis,使用
npm install googleapis
这里是我的代码:
var googleapis = require('googleapis');
var SERVICE_ACCOUNT_EMAIL = 'My Service Account E-mail Address';
var SERVICE_ACCOUNT_KEY_FILE = 'my_google_key.pem'; // The .pem file is at the root of my application
var jwt = new googleapis.auth.JWT(
SERVICE_ACCOUNT_EMAIL,
SERVICE_ACCOUNT_KEY_FILE,
null,
['https://www.googleapis.com/auth/admin.directory.group']
);
var client;
googleapis
.discover('admin', 'directory_v1')
.execute(function(err, data) {
client = data;
jwt.authorize(function(err, result) {
console.log(jwt);
client.admin.groups.list({
"customer": "my_customer", // This is actually "my_customer"
"domain": "domain.com" // The domain name I administer
})
.withAuthClient(jwt)
.execute(function(err, result) {
console.log(err);
console.log(result);
});
});
});
运行这段代码的结果是:
{ errors:
[ { domain: 'global',
reason: 'forbidden',
message: 'Not Authorized to access this resource/api' } ],
code: 403,
message: 'Not Authorized to access this resource/api' }
我错过了什么?如何使用Admin SDK授权我的应用程序?