使用Passport JS进行身份验证后,我正在尝试访问GDRIVE API。但是,我遇到了这个错误 -

发布于 2025-02-04 00:02:31 字数 1338 浏览 2 评论 0原文

错误 - 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偷得浮生 2025-02-11 00:02:31

回电传递给fs.ReadFile是在致电listFiles后执行的,因此,您正在传递oauth2client而无需设置访问令牌。

您至少有以下选项对其进行修复:

  • call listFiles readfile callback(setCredentials
  • 之后/code>带有fs.ReadFilesync,因此您不需要回调
  • 使用fs/promises和async/等待而不是回调

The callback passed to fs.readFile is executed after call to listFiles, therefore you are passing oauth2Client without access token being set.

You have at least the following options to fix it:

  • Call listFiles inside the readFile callback (after setCredentials)
  • Replace fs.readFile with fs.readFileSync so you don't need a callback
  • Use fs/promises and async/await instead of callbacks
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文