【问题标题】:Not Authorized To Access This Resource/API (GCP)无权访问此资源/API (GCP)
【发布时间】:2021-02-15 20:02:51
【问题描述】:

我设置了一个具有域范围委派的服务帐户,我将客户端电子邮件、私钥、范围和用户电子邮件传递给 JWT 方法以模拟 G-Suite 用户。然后,我从 Admin API 获取特定的用户信息,并使用它来创建电子邮件签名并将其推送到 Gmail API。如果我传递给 JWT 方法的用户电子邮件是超级管理员,它会很好,但如果我尝试传递任何其他用户,我会收到错误响应,“未授权访问此资源/api”。关于如何让它在我的域中使用常规用户帐户的任何想法?

这里是代码。 (genSignature.js)

const { google } = require('googleapis');
const privatekey = require('../private-key.json');

const scopes = [
    'https://www.googleapis.com/auth/gmail.settings.basic',
    'https://www.googleapis.com/auth/gmail.settings.sharing', 
    'https://www.googleapis.com/auth/admin.directory.user', 
    'https://www.googleapis.com/auth/admin.directory.user.readonly'
];

const auth = async (user) => {
    try {
        const jwtClient = new google.auth.JWT(
            privatekey.client_email,
            null, 
            privatekey.private_key,
            scopes,
            user // User who will be impersonated using the JWT client.
        );

        await jwtClient.authorize();
        return jwtClient;

    } catch (err) {

        console.log(err.message);

    };
};

function genSig(e) {

    auth(e).then((jwtClient) => {

        // Authenticate with the gmail API.
        const gmail = google.gmail({
            version: 'v1', 
            auth: jwtClient
        }); 

        // Authenticate with the admin API.
        const dir = google.admin({
            version: 'directory_v1', 
            auth: jwtClient
        });


        // Get users contact and job data from the directory. This data will be used as variables in their email signature. 
        dir.users.get({ userKey: e }, (err, response) => {
            if (err) {
                console.log(err.message);
            } else {
                let phones = response.data.phones;
                let workPhone = '';

                if (phones) {
                    for (i = 0; i < phones.length; i++) {
                        if (phones[i].type == 'work') {
                            workPhone = phones[i].value;
                        };
                    };
                };

                function getUserData() {
                    let userData = {
                        name: response.data.name.fullName, 
                        email: response.data.primaryEmail, 
                        phone: workPhone, 
                        avatar: response.data.thumbnailPhotoUrl, 
                        department: response.data.organizations[0].department, 
                        title: response.data.organizations[0].title
                    }; 
                    return userData;
                };

                let requestBody = {
                    signature: 'Test' 
                };

                // Update the users email signature for their primary email.
                gmail.users.settings.sendAs.update({ userId: e, sendAsEmail: e, requestBody }, (err, response) => {
                    if (err) {
                        console.log(err.message);
                    } else {
                        console.log(response.data);
                    };
                });
            };
        });
    });
}

module.exports = genSig;

(signatures.js)

const express = require('express');
const router = express.Router();
const genSig = require('../../functions/genSignature');

// Get webhooks from Google.
router.post('/', (req, res) => {

    let email = req.body.email;
    let emailStr = email.toString();
    console.log(emailStr);
    genSig(emailStr);


    res.status(200).json({
        "msg": "data recieved..."
    });
});

module.exports = router;

(index.js)

const express = require('express');
const app = express();

app.use(express.json());
app.use('/email-signature', require('./routes/api/signatures'));

const PORT = process.env.PORT || 6000;

app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));

这里有一些截图。

API configuration on G-Suite

Service Account Setup

Successful request vs unsuccessful request

【问题讨论】:

    标签: node.js gmail-api google-admin-sdk google-workspace google-api-nodejs-client


    【解决方案1】:

    你需要冒充管理员:

    只有具有用户管理权限的帐户(如超级管理员或用户管理管理员)才能访问Users: get。您必须考虑到这是Admin SDK 的一部分,供管理员帐户使用。

    如果您尝试通过Try this API on the reference docs 调用它,您也可以检查这是不可能的(您将收到相同的消息:Not Authorized to access this resource/api)。

    使用具有域范围权限的服务帐户并不重要:当服务帐户冒充另一个用户时,它只能访问该用户可以访问的资源。

    解决方案:

    在这种情况下,如果您想从 Admin SDK 检索用户数据,被模拟的帐户应该具有用户管理权限。

    但由于调用 Gmail API 方法不需要这些权限,因此您可以在调用 Users: get 时模拟管理员帐户,在调用 users.settings.sendAs.update 时模拟普通帐户。

    参考:

    【讨论】:

    • 这很有意义,感谢您为我澄清。所以从技术上讲,服务帐户电子邮件被认为是管理员帐户对吗?也许我可以使用该帐户从 Admin SDK 中检索有关另一个用户的特定信息,然后更新他们的电子邮件签名。这个想法是有一个云功能来监听来自 Admin SDK 的事件并更新电子邮件签名以反映这些变化。
    • @RainePetersen 不,服务帐户在技术上甚至不是域的一部分,它无法自行访问这些数据;它需要模拟具有访问权限的帐户。如果你想使用 Admin SDK,你应该模拟一个具有管理员权限的普通帐户。
    • @RainePetersen 另外,如果它解决了您的问题,请考虑接受这个答案。
    【解决方案2】:

    这不是一个新帖子。但是,我面对它并找到了解决方案。

    您可以通过分配角色来使用服务帐号。请参阅Assign specific admin role 中的“为服务帐户分配角色”。 updates blog post.有详细介绍

    首先,您需要在 Google Workspace 管理控制台中创建一个自定义管理员角色。您可以使用电子邮件地址将服务帐户分配给自定义管理员角色。

    它在我的环境中适用于 Google Cloud Functions。

    【讨论】:

      猜你喜欢
      • 2014-12-12
      • 2018-11-10
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 2020-07-05
      • 1970-01-01
      • 2020-05-07
      相关资源
      最近更新 更多