使用Passport JS进行身份验证后,我正在尝试访问GDRIVE API。但是,我遇到了这个错误 -
错误 - API返回错误:错误:无访问,刷新令牌,API键或刷新处理程序回调。
我要做的是 -
app.get("/getinfo", (req,resp)=>{
const oauth2Client = new google.auth.OAuth2(myClientID, myClientSecret, redirectURL )
var data="";
fs.readFile("token.json", function(err, dat) {
if (err) throw err;
data=JSON.parse(dat);
oauth2Client.setCredentials({
'access_token': data.token
});
});
listFiles(oauth2Client);
resp.send("Getting drive files");
});
“ token.json”由Passport JS成功验证后收到的身份标记组成。 “ ListFiles”功能是 -
function listFiles(auth) {
console.log(auth);
const drive = google.drive({version: 'v3', auth});
drive.files.list({
pageSize: 10,
fields: 'nextPageToken, files(id, name)',
}, (err, res) => {
console.log("inside drive api call");
if (err) return console.log('The API returned an error: ' + err);
const files = res.data.files;
if (files.length) {
console.log('Files:');
files.map((file) => {
console.log(`${file.name} (${file.id})`);
});
} else {
console.log('No files found.');
}
});
}
有人可以帮助我解决该功能工作。我的目标是在成功身份验证后阅读一个人驱动器的文件列表。我也检查了验证所需的范围。
Error -
The API returned an error: Error: No access, refresh token, API key or refresh handler callback is set.
What I am trying to do is -
app.get("/getinfo", (req,resp)=>{
const oauth2Client = new google.auth.OAuth2(myClientID, myClientSecret, redirectURL )
var data="";
fs.readFile("token.json", function(err, dat) {
if (err) throw err;
data=JSON.parse(dat);
oauth2Client.setCredentials({
'access_token': data.token
});
});
listFiles(oauth2Client);
resp.send("Getting drive files");
});
Where "token.json" consists of the auth token received upon successful validation via passport js. The "listFiles" function is -
function listFiles(auth) {
console.log(auth);
const drive = google.drive({version: 'v3', auth});
drive.files.list({
pageSize: 10,
fields: 'nextPageToken, files(id, name)',
}, (err, res) => {
console.log("inside drive api call");
if (err) return console.log('The API returned an error: ' + err);
const files = res.data.files;
if (files.length) {
console.log('Files:');
files.map((file) => {
console.log(`${file.name} (${file.id})`);
});
} else {
console.log('No files found.');
}
});
}
Can somebody help me out on how to make this function work. My objective is to read the list of files of a person's Drive after successful authentication. I have checked my scopes too required for validation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回电传递给
fs.ReadFile
是在致电listFiles
后执行的,因此,您正在传递oauth2client
而无需设置访问令牌。您至少有以下选项对其进行修复:
listFiles
readfile callback(setCredentials
fs.ReadFilesync
,因此您不需要回调fs/promises
和async/等待而不是回调The callback passed to
fs.readFile
is executed after call tolistFiles
, therefore you are passingoauth2Client
without access token being set.You have at least the following options to fix it:
listFiles
inside thereadFile
callback (aftersetCredentials
)fs.readFile
withfs.readFileSync
so you don't need a callbackfs/promises
and async/await instead of callbacks