【问题标题】:Gmail API userId use cases in Gmail NodeJs client?Gmail NodeJs客户端中的Gmail API userId用例?
【发布时间】:2021-12-24 11:47:23
【问题描述】:

在使用特殊值“我”的情况之外,如何使用 userId 属性?

据我了解,当您使用“我”时,是指您已作为最终用户进行身份验证。

但是当您没有作为最终用户进行身份验证时,您不能使用“me”,也不能使用任何其他值,因为您没有执行给定请求的权限。

我试图了解如何在不使用 userId 字段中的值“me”的情况下代表经过身份验证的用户在我的应用上执行操作?

使用 Gmail nodeJs 客户端的示例:

const googleClient = new google.auth.OAuth2(client_id, client_secret, redirect_uri);

async function getUnreadEmailsFrom(userID) {
let gmail = google.gmail({ version: 'v1', auth: googleClient });

/* Here on the line below, I'd like to use the email or ID of an already authenticated user in order to perform a given action.

But the only way I understand that I am able to perform any action is by loading the token of the user and saving it via 
googleClient.setCredentials(userToken)
and then using the special value 'me' inside the userId propery.
*/
let response = await gmail.users.messages.list({userId: 'me', q: 'is:unread', maxResults: 500 });

return response.data;
} 

【问题讨论】:

标签: node.js api google-api gmail-api


【解决方案1】:

简答:

userId 的唯一允许值是 me 和经过身份验证(或模拟)的用户电子邮件地址。

说明:

访问其他用户数据的唯一方法是 grant domain-wide authority to a service account 并使用它来模拟您要访问其数据的目标用户(该用户应该在同一个域中)。

不过,目标用户电子邮件在模拟时指定(请参阅Preparing to make an authorized API call),即在调用 API 之前。

稍后,在调用 API 时,特殊值 me 将对应于模拟用户,并且唯一接受的值将是 me 和模拟用户电子邮件。任何其他值都会导致错误(因为服务帐户将充当模拟用户,并且只能访问模拟用户的数据)。

因此,me 的唯一替代方案是经过身份验证的用户电子邮件地址,但您当然会得到相同的数据。

域范围的委托示例:

creds = service_account.Credentials.from_service_account_file('credentials.json', scopes=SCOPES)
creds = creds.with_subject('your_target_user@email.com')
service = build('gmail', 'v1', credentials=creds)
messages = service.users().messages().list(userId="me").execute()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-31
    • 2021-11-10
    • 1970-01-01
    • 2021-01-16
    • 2018-09-01
    • 2020-06-11
    • 2017-02-19
    • 1970-01-01
    相关资源
    最近更新 更多