在 Gmail API 中识别用户的最佳实践?

问题描述 投票:0回答:1

Gmail API 使用 UserId,即用户电子邮件地址。然而,随着时间的推移,电子邮件地址“不是一个可靠的识别地址”。在 Google Workspace 上,用户电子邮件地址经常发生更改。 从长远来看,连接(并保持连接)到特定用户邮箱的最佳实践是什么?

注意:在

directory API

上,Google 实际上为用户使用一个不会更改的“id”。

在这个使用 axios 和 typescript 的实现中......
gmail gmail-api google-workspace
1个回答
0
投票
import axios from 'axios'; const googleApi = axios.create({ baseURL: 'https://www.googleapis.com', // Configure other Axios settings as needed }); async function getUserImmutableId(email: string, accessToken: string): Promise<string> { try { const response = await googleApi.get(`/admin/directory/v1/users/${email}`, { headers: { Authorization: `Bearer ${accessToken}`, }, }); return response.data.id; // The immutable user ID } catch (error) { console.error('Error fetching user ID:', error); throw error; } } // Example usage const userEmail = '[email protected]'; const userAccessToken = 'your_oauth_access_token_here'; // Ensure this token has the necessary scopes getUserImmutableId(userEmail, userAccessToken) .then(userId => { console.log(`The immutable ID for ${userEmail} is ${userId}`); // Use userId with Gmail API here }) .catch(console.error);

...您获得了 Axios 配置、OAuth2 授权、错误处理和不可变用户 ID,这些是我脑海中最重要的最佳实践。
    

© www.soinside.com 2019 - 2024. All rights reserved.