Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用googleapi的异步身份验证_Javascript_Node.js_Asynchronous_Promise - Fatal编程技术网

Javascript 使用googleapi的异步身份验证

Javascript 使用googleapi的异步身份验证,javascript,node.js,asynchronous,promise,Javascript,Node.js,Asynchronous,Promise,我有以下代码: async function initApi() { const googleKey = await readJSON(appRoot + '/secrets/google-auth.json'); const jwt = new google.auth.JWT( googleKey.client_email, null, googleKey.private_key, scopes); return jwt.authorize(); } const ca

我有以下代码:

async function initApi() {
  const googleKey = await readJSON(appRoot + '/secrets/google-auth.json');

  const jwt = new google.auth.JWT(
    googleKey.client_email, null, googleKey.private_key, scopes);

  return jwt.authorize();
}

const calendar = {
  events: events,

  api: google.calendar({
    version: 'v3',
    auth: this.jwt
  }),

  list: async function() {
    await this.api.calendarList.list();
  },
};

module.exports = async () => Object.assign(calendar, { jwt: await initApi() });
我不断得到错误登录要求。然而,当我记录结果时,jwt可以很好地解析为访问令牌:

const Calendar = require('./above-code.js');

Calendar().then(c => console.log(c.jwt));
// { access_token: ... }

Calendar().then(c => console.log(c.list());
// Error: login required
我不明白为什么。这简直让我发疯。我想把我的笔记本电脑扔出窗外

我做错了什么?这是使用module.exports的async和Wait的最佳模式吗?有没有更好的方法可以直接返回该对象,这样我就可以调用Calendar.list并获得结果,而无需跳转到我目前所做的工作,直接获取API并调用方法?例如:

const Calendar = require('./above-code.js');
await Calendar.list();
将jwt添加到对象中,而不是作为单独的变量,因此它应该是:

  google.calendar({
    version: 'v3',
    auth: this.jwt //here
}),
Object.assigncalendar,{jwt:'value'};它相当于calendar.jwt='value'。你指的好像是这样做的:

let jwt  = 'value';
  google.calendar({
    version: 'v3',
    auth: jwt //here
}),

在我回到这个话题之前,我休息了一下——对于我自己来说,最好的解决办法似乎是在我继续解决一个令人沮丧的问题之前不再那么沮丧

首先,我将api属性移动到initApi函数,并在那里返回该属性-这似乎解决了我的问题:

async function initApi() {
  const googleKey = await readJSON(appRoot + '/secrets/google-auth.json');

  const jwt = new google.auth.JWT(
    googleKey.client_email, null, googleKey.private_key, scopes);

  jwt.authorize();

  return google.calendar({
    version: 'v3',
    auth: jwt
  });
}

const calendar = {
  events: events,

  list: async function() {
    await this.api.calendarList.list();
  },
};

module.exports = async () => Object.assign(calendar, { api: await initApi() });
然而,第二个警告是,现在谷歌一直在抱怨没有找到client.request——结果谷歌有两套auth工具

在改为使用google auth library而不是使用googleapis的内置auth.JWT后,我从服务器上得到了一个响应,没有客户机请求投诉:

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

...

async function initApi() {
  const googleKey = await readJSON(appRoot + '/secrets/google-auth.json');

  const jwt = new JWT(
    googleKey.client_email, null, googleKey.private_key, scopes);

  return await google.calendar({
    version: 'v3',
    auth: jwt
  });
}
现在它工作了!希望这能帮助任何有这个问题的人


编辑:此外,请参阅Google的示例,从Google API仪表板的可下载JSON文件加载auth。

由于作者构建模块对象的方式,答案有些难以理解

这是一个使用promises的简化版本,不需要GoogleAuth库

const{google}=requiregoogleapis; 函数身份验证{ 常量gAccount=// const jwt=new google.auth.JWTgAccount.client_email,null,gAccount.private_key,gAccount.scope; 返回jwt.authorize.then=>google.calendar{version:v3,auth:jwt}; } 功能列表日历{ 返回auth.thencalendar=>{ 返回日历 .calendarList.list{showHidden:true} .thenres=>res.data.items; }; }
对不起,我不太明白。这正是我在问题中的代码。你能再澄清一点吗?对不起,巴托,看起来这不是我的问题。我马上回答!