首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未授权访问此资源/API (GCP)

未授权访问此资源/API (GCP)
EN

Stack Overflow用户
提问于 2020-11-03 06:36:37
回答 2查看 1.8K关注 0票数 3

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

,这是代码。(genSignature.js)

代码语言:javascript
复制
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)

代码语言:javascript
复制
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)

代码语言:javascript
复制
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}`));

这里有一些截图。

G-Suite的API配置

服务帐户设置

成功请求与不成功请求

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-03 09:42:01

您需要模拟管理员:

只有具有用户管理权限的帐户(如超级管理或用户管理管理)才能访问用户:获取。您必须考虑到这是管理SDK的一部分,它将由管理帐户使用。

如果尝试通过Try this API 关于参考文档调用它,也可以检查这是不可能的(您将得到相同的消息:Not Authorized to access this resource/api)。

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

解决方案:

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

但是,由于调用Gmail方法不需要这些特权,所以在调用Users: get时可以模拟管理帐户,在调用users.settings.sendAs.update时可以模拟常规帐户。

参考资料:

票数 3
EN

Stack Overflow用户

发布于 2021-11-29 14:49:31

这不是新职位。然而,我面对它,并找到了解决办法。

您可以通过分配角色来使用服务帐户。请参阅指定特定的管理角色中的“向服务帐户分配角色”。更新博客文章。中有详细信息

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

它在我的环境中运行于Google函数上。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64657836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档