首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gmail API userId在Gmail NodeJs客户端中的使用案例?

Gmail API userId在Gmail NodeJs客户端中的使用案例?
EN

Stack Overflow用户
提问于 2021-11-12 03:34:30
回答 1查看 68关注 0票数 0

在使用特殊值'me‘的情况下,如何使用userId属性?

据我所知,当您使用'me‘时,是指您已经作为最终用户进行了身份验证。

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

我正在尝试理解如何在我的应用程序上代表经过身份验证的用户执行操作,而不在userId字段中使用值'me‘?

使用Gmail nodeJs客户端的示例:

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-12 08:46:23

简短的回答:

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

解释:

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

但是,目标用户邮箱是在模拟时指定的(参见Preparing to make an authorized API call),也就是在调用接口之前。

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

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

全域委派示例:

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69937654

复制
相关文章

相似问题

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