javascript - typescript ,如何引用未导出的 OAuth2Client? google-api-nodejs-客户端

标签 javascript typescript google-api

我正在尝试关注 https://developers.google.com/sheets/api/quickstart/nodejs教程 我正在使用 typescript ,我喜欢它提供的类型注释和自动完成功能,我想用 typescript 和异步而不是回调来重写示例。 可悲的是,我被卡住了,OAuth2Client。 在我的代码中,我在这样的函数中创建了一个 oauth 客户端:

async function setupClient() {
  const content: string = await fs.readFile("credentials.json", "utf8");
  const credentials = JSON.parse(content);
  // eslint-disable-next-line camelcase
  const { client_secret, client_id, redirect_uris } = credentials.installed;
  const oAuth2Client: o2 = new google.auth.OAuth2(
    client_id,
    client_secret,
    redirect_uris[0]
  );
  type OAuth2Client = typeof oAuth2Client;
  return oAuth2Client;
}

我想将该客户端委托(delegate)给另外两个设置函数:

function setupAuthUrl(oAuth2Client, scopes) {
  const authUrl = oAuth2Client.generateAuthUrl({
    access_type: "offline",
    scope: scopes,
  });
  return authUrl;
}
function setupToken(oAuth2Client, code) {
  //code
}

但是我正在努力注释 oauth2client 类型,因为它没有直接暴露给任何东西? 有人会猜测它是在“oauth2_v2”命名空间下公开的吗?但似乎不是。

我一直在寻找一种在实例化之前引用这种类型的方法,最好是通过导入,遗憾的是,当通过 npm install googleapis 安装这个库时,它不提供对这些中任何一个的依赖google-auth-library , googleapis-common导出和暴露以供导入的位置。 eslint 错误,除非它添加了依赖项,我很难理解为什么它是必需的,因为该类型应该可以访问以轻松完成代码。

关于类型别名:

type OAuth2Client = typeof GoogleApis.prototype.auth.OAuth2.prototype;

这是我能想到的唯一不会为别名抛出错误/警告(对象作为命名空间)的选项。这既是一条很长的线,也是来自其他 OO 语言的非常奇怪的线

  • TLDR:在 typescript 中访问 OAuth2Client 和其他类型的代码完成的首选方式是什么?

最佳答案

截至 6 月 19 日,添加此功能的合并请求已获批准。 https://github.com/googleapis/google-api-nodejs-client/issues/2208

import { google, Auth } from 'googleapis';
const oauthClient: Auth.OAuth2Client = new google.auth.OAuth2();

感谢angelxmoreno例如

关于javascript - typescript ,如何引用未导出的 OAuth2Client? google-api-nodejs-客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62161978/

相关文章:

google-analytics - 为什么Google Analytics(分析)API在我的过滤器查询中使用“\”?

javascript - 无法使用 Skydrive API 获取文件夹内容

javascript - .focus() 不适用于输入,而其他属性则有效

python - 使用 python 的 Google API 文件权限

javascript - 通过 Google API 查找组织的公共(public)社交媒体资料

javascript - 如何使用jsdom测试 'document'的函数

javascript - 在 Jasmine 中 mock xhr 调用

javascript - jQuery - 在页面加载时执行函数?

javascript - 使用 URLSearchParams 防止百分比编码

angularjs - TypeScript 中指令、编译(前/后)的等价物是什么?