Gmail Api 访问令牌在过期后未更改 - 但访问仍然有效
Gmail Api access token not changing after expiration - but access still works
我正在开发一个基本应用程序,它只读取来自特定 gmail 帐户(例如 fakeEmail@gmail.com)的电子邮件。首先 运行 该应用程序被授予阅读 fakeEmail@gmail.com 的电子邮件的权限。以及 access_token、expiry_date(1 小时)和 refresh_token 等被保存到 'token.json' 文件。
在随后的 运行 秒,甚至在访问令牌过期后,我 NOT 看到正在发出刷新令牌请求,但应用程序能够获取和读取来自 fakeEmail@gmail.com.
的电子邮件
该应用程序是 运行 从命令行作为 'node app.js',它获取具有特定标签的电子邮件并在控制台上打印电子邮件的内容。
方法authorize()是每次应用运行时调用的第一个方法。当用户 fakeEmail@gmail.com 授予 App 阅读其电子邮件的权限。
这是这个简单应用的相关代码:
const SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
const TOKEN_PATH = 'token.json';
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err)
return console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
function startApp(){
fs.readFile('gmailCredentials.json', (err, content) => {
if (err)
return console.log('Error loading client secret file:', err);
authorize(JSON.parse(content), function(auth){
checkForMailsWithLabel(auth, strLabel, function(err, res){
console.log("auth holds: " + util.inspect(auth) );
// this prints the same accessToken and expiration as the one we have in 'token.json'
// Even though the token is expired, the app is still able to read the emails
});
});
});
}
您在此处看到的代码几乎与他们在 github 上的示例完全相同:https://github.com/gsuitedevs/node-samples/blob/master/gmail/quickstart/index.js
如果重要的话,我还没有用 google 完成此应用程序的验证过程。由于我是用自己的邮箱测试的,授权时的untrusted warning暂时没有问题。
我的 question/assumption 是 googleapi 库会检查访问令牌是否已过期,如果已经过期,它将请求另一个令牌自动写入到'token.json'文件中。
但是据我从 运行 宁这段代码中可以看出,'token.json' 文件是在第一个 运行 上创建的。即使 expiry_date(1 小时)已过期,google 库也不会请求另一个令牌。我这样说是因为:
- 我没有看到 'token.json'
的任何更新
- 即使在成功获取电子邮件后,用于获取的令牌仍持有相同的访问令牌和到期时间。
我可以编写代码来检查 expiry_date 是否已通过,并可能强制刷新 access_token .但这并不能解释为什么我的应用程序能够使用过期令牌获取电子邮件。
我宁愿使用推荐的方法,如果应该的话,让库处理令牌。
请指教,我在这里缺少什么。
在Google APIs Node.js Client as it is stated in this section中:
Access tokens expire. This library will automatically use a refresh
token to obtain a new access token if it is about to expire.
因此,您不必担心自己获取新的访问令牌。虽然,因为您正在使用 Node.js Quickstart 并且每次 运行 它,您使用 .setCredentials()
设置凭据,所以您通过从 json 文件.
有关令牌处理的更多信息,您可以查看 Google Auth Library。
我正在开发一个基本应用程序,它只读取来自特定 gmail 帐户(例如 fakeEmail@gmail.com)的电子邮件。首先 运行 该应用程序被授予阅读 fakeEmail@gmail.com 的电子邮件的权限。以及 access_token、expiry_date(1 小时)和 refresh_token 等被保存到 'token.json' 文件。
在随后的 运行 秒,甚至在访问令牌过期后,我 NOT 看到正在发出刷新令牌请求,但应用程序能够获取和读取来自 fakeEmail@gmail.com.
的电子邮件该应用程序是 运行 从命令行作为 'node app.js',它获取具有特定标签的电子邮件并在控制台上打印电子邮件的内容。
方法authorize()是每次应用运行时调用的第一个方法。当用户 fakeEmail@gmail.com 授予 App 阅读其电子邮件的权限。
这是这个简单应用的相关代码:
const SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
const TOKEN_PATH = 'token.json';
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err)
return console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
function startApp(){
fs.readFile('gmailCredentials.json', (err, content) => {
if (err)
return console.log('Error loading client secret file:', err);
authorize(JSON.parse(content), function(auth){
checkForMailsWithLabel(auth, strLabel, function(err, res){
console.log("auth holds: " + util.inspect(auth) );
// this prints the same accessToken and expiration as the one we have in 'token.json'
// Even though the token is expired, the app is still able to read the emails
});
});
});
}
您在此处看到的代码几乎与他们在 github 上的示例完全相同:https://github.com/gsuitedevs/node-samples/blob/master/gmail/quickstart/index.js
如果重要的话,我还没有用 google 完成此应用程序的验证过程。由于我是用自己的邮箱测试的,授权时的untrusted warning暂时没有问题。
我的 question/assumption 是 googleapi 库会检查访问令牌是否已过期,如果已经过期,它将请求另一个令牌自动写入到'token.json'文件中。
但是据我从 运行 宁这段代码中可以看出,'token.json' 文件是在第一个 运行 上创建的。即使 expiry_date(1 小时)已过期,google 库也不会请求另一个令牌。我这样说是因为:
- 我没有看到 'token.json' 的任何更新
- 即使在成功获取电子邮件后,用于获取的令牌仍持有相同的访问令牌和到期时间。
我可以编写代码来检查 expiry_date 是否已通过,并可能强制刷新 access_token .但这并不能解释为什么我的应用程序能够使用过期令牌获取电子邮件。
我宁愿使用推荐的方法,如果应该的话,让库处理令牌。
请指教,我在这里缺少什么。
在Google APIs Node.js Client as it is stated in this section中:
Access tokens expire. This library will automatically use a refresh token to obtain a new access token if it is about to expire.
因此,您不必担心自己获取新的访问令牌。虽然,因为您正在使用 Node.js Quickstart 并且每次 运行 它,您使用 .setCredentials()
设置凭据,所以您通过从 json 文件.
有关令牌处理的更多信息,您可以查看 Google Auth Library。