googleAuth不是构造函数,google-api版本问题

时间:2019-02-15 22:49:07

标签: node.js google-sheets google-sheets-api google-auth-library

按照本指南操作Google电子表格- http://voidcanvas.com/node-js-googleapis-v4-spreadsheet/ 我一直收到“ googleAuth不是构造函数”错误。

搜索网络时,我发现新版本的google-auth-library的旧语法有问题。安装0.12.0版本已使我进入“通过访问此URL来授权应用程序:”部分,但是提供的URL是一个损坏的链接,内容为:

错误:invalid_request redirect_uri的参数值无效:方案无效:urn:ietf:wg:oauth:2.0:oob

另一方面,我尝试使用最新版本的google-auth-library,我发现了一个修复程序,说要更改这些行

let oauth = require('google-auth-library');
...
var oauth2Client = new oauth.OAuth2(clientId, clientSecret, redirectUrl);

但它创建了一个“ oauth.OAuth2不是 构造函数”错误。

这是原始代码,运行于0.12.0,并返回错误的URL。

let fs = require('fs');
let readline = require('readline');
let googleAuth = require('google-auth-library');
...
authorize(credentials) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

这是在更新版本+修复程序上运行的代码

 let fs = require('fs');
 let readline = require('readline');
 let oauth = require('google-auth-library'); 
 ...
 authorize(credentials) {
 var clientSecret = credentials.installed.client_secret;
 var clientId = credentials.installed.client_id;
 var redirectUrl = credentials.installed.redirect_uris[0];
 ///var auth = new googleAuth();
 var oauth2Client = new oauth.OAuth2(clientId, clientSecret, redirectUrl);

我希望结果是一个很好的URL,该URL返回完成授权所需的代码,并依次更新我的工作表。

1 个答案:

答案 0 :(得分:0)

对于较新的版本,您需要使用OAuth2Client属性,该属性可以通过解构获得。其他常见属性是authJWT

const {OAuth2Client} = require('google-auth-library');

另请参阅https://www.npmjs.com/package/google-auth-library