使用jwt从Google admin api获取用户信息

时间:2018-06-08 12:17:44

标签: google-api authorization jwt

  • 我已在Google API控制台中创建了一个服务帐户
  • 启用域范围委派
  • 我已下载密钥
  • 用户是在G Suite中创建的,匹配的client_id可以访问范围

我仍然收到错误403 - Not Authorized to access this resource/api

接下来我应该测试或检查什么?

const fs = require('fs');
const { google } = require('googleapis');

const privatekey = require('./keyfile.json');

// configure a JWT auth client
let jwtClient = new google.auth.JWT(
    privatekey.client_id,
    null,
    privatekey.private_key,
    ['https://www.googleapis.com/auth/admin.directory.user.readonly']);

// authenticate request
jwtClient.authorize(function (err, tokens) {
    if (err) {
        console.log(err);
        return;
    } else {
        console.log("Successfully connected!");
        authCallback(jwtClient);
    }
});

function authCallback(auth) {
    const service = google.admin('directory_v1');
    let a = service.users.get({
        'auth': auth,
        'userKey': 'user@domain.org',
        'viewType': 'domain_public'
    }, function(err, data) {
        // Shows the error here
        console.log(err || data);
    });
}

0 个答案:

没有答案