在Node中与googleapis软件包交换访问令牌的授权代码

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

我正在尝试为需要googledrive访问的服务器端NodeJS应用程序实现google登录,并且无法获取googleapis google对象以将授权代码交换为实际访问令牌。

有关于我想做的事情的Google文档here,但是用Java和Python描述了第7步,这就是我要坚持的地方,因此没有NodeJS谷歌对象。我还遵循了nodeJS quickstart here,但是身份验证的许多步骤都不同。也就是说,我无法以相同的方式创建google.auth.OAuth2对象,因为我的应用程序的凭据格式与示例的格式不同(它们没有重定向uris)

这是我的位置:

const fs = require('fs');
const { google } = require('googleapis');



formAuthClient('<PastedAuthCodeFromFrontEnd>');

async function formAuthClient(code) {
  // reads credentials from file
  const credentials = await readFile('./client_secret.json').then(JSON.parse);

  // cannot read redirect_uris like in NodeJS quickstart because these do not exist. 
  // Also, no credentials.installed, it's credentials.web
  const { client_secret, client_id } = credentials.web;
  const oAuth2Client = new google.auth.OAuth2( // form authObject
    client_id, client_secret
  );
  console.log(oAuth2Client);

  const token = await oAuth2ClientGetToken(oAuth2Client, code).catch(console.err);
  oAuth2Client.setCredentials(token);
  return oAuth2Client;
}


// just an async wrapper for fs.readFile
function readFile(file) {
  return new Promise((resolve, reject) => {
    fs.readFile(file, (err, content) => {
      if (err) reject(err);
      resolve(content);
    });
  })
}
// just an async wrapper for oAuth2Client.getToken
function oAuth2ClientGetToken(oAuth2Client, code) {
  return new Promise((resolve, reject) => {
    oAuth2Client.getToken(code, (err, token) => { // errors out here
      if (err) reject(err);
      resolve(token);
    });
  });
}

运行此代码首先显示oAuth2Client对象,然后给我以下错误:


OAuth2Client {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  transporter: DefaultTransporter {},
  credentials: {},
  certificateCache: {},
  certificateExpiry: null,
  certificateCacheFormat: 'PEM',
  refreshTokenPromises: Map {},
  _clientId: '<probablySecretClientID>.apps.googleusercontent.com',
  _clientSecret: '<defSecretClientSecret>',
  redirectUri: undefined,
  eagerRefreshThresholdMillis: 300000
}


(node:16186) UnhandledPromiseRejectionWarning: Error: invalid_grant
    at Gaxios.request (/home/miko/node/clouds/api/node_modules/gaxios/build/src/gaxios.js:70:23)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
    at async OAuth2Client.getTokenAsync (/home/miko/node/clouds/api/node_modules/google-auth-library/build/src/auth/oauth2client.js:119:21)
(node:16186) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:16186) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code

理想情况下,我希望top函数返回经过完全身份验证的客户端对象,以后可用于访问google drive api

javascript node.js oauth-2.0 google-oauth google-authentication
1个回答
0
投票

我在这里提出了一个解决方案https://github.com/googleapis/google-api-nodejs-client/issues/1951

对于跟随我的所有怒吼-解决方案先决条件如果您具有与我相同的设置,而不是在前端浏览器中执行代码交换,而是在后端设置中进行令牌交换。我做了以下事情似乎可以解决问题。

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