google-api-nodejs-client icon indicating copy to clipboard operation
google-api-nodejs-client copied to clipboard

typescript, how to reference the unexported OAuth2Client?

Open YoraiLevi opened this issue 6 years ago • 9 comments

I am trying to follow https://developers.google.com/sheets/api/quickstart/nodejs tutorial I am using typescript and I like the type annotation and auto-complition features it provides and I would like to re-write the example in typescript and async rather than callbacks. Sadly I am stuck, OAuth2Client. in my code I creating an oauth client inside a function like this:

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;
}

And I would like to delegate that client to two other setup functions:

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

But I am struggling to annotate oauth2client type as it is not directly exposed by anything? One would guess that it is exposed under the 'oauth2_v2' namespace? but doesn't seem to be it.

I have been looking for a way to reference this type prior to instantiation, preferably via importing, sadly, when installing this libary via npm install googleapis it doesn't provide a dependency to niether of these google-auth-library, googleapis-common where it is exported and exposed for importing. eslint errors unless it adds the dependency and I struggle to see why It is required as the type should be accessable for code completion with ease.

As for type aliasing:

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

This is the only option I could figure out that doesn't throw an error/warning(object as namespace) for aliasing. this is both a long line and very weird one at that too coming from other OO languages

  • TLDR: What's the prefered way to access OAuth2Client and maybe other types for code completion in typescript?

YoraiLevi avatar Jun 02 '20 21:06 YoraiLevi

Same goes for Credentials used in oAuth2Client.setCredentials(credentials: Credentials) method. There is no exported type, I'm importing it like this:

import { google } from 'googleapis';
type Credentials = typeof google.auth.OAuth2.prototype.credentials;

arkus7 avatar Jun 08 '20 20:06 arkus7

There were ways to do this before, but they aren't pretty 😛 Have a proposal over in https://github.com/googleapis/google-api-nodejs-client/pull/2250 to try and make things nicer.

JustinBeckwith avatar Jun 19 '20 16:06 JustinBeckwith

Found this looking for the same answers. Thanks @JustinBeckwith for the work. Here is an example that answers @YoraiLevi 's question

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

Auth is the namespace for the google-auth-library lib that this repo uses

angelxmoreno avatar Aug 11 '20 01:08 angelxmoreno

I tried @angelxmoreno 's example, but it doesn't seem to work any more:

image

@arkus7's hack is the only thing I can get to work:

type OAuth2Client = typeof google.auth.OAuth2.prototype

HerbCaudill avatar Oct 24 '20 12:10 HerbCaudill

@HerbCaudill Make sure you're using 54.0.0 or later. According to commit #2250, the fix was released in 53.0.0. Strangely, that version doesn't appear to be available in NPM:

$ npm view [email protected]
$ 

I've confirmed it's available for me in v54 and beyond though.

My tendency is to hide these behind my own type in case they move around. So I have a separate file with this:

import { Auth } from 'googleapis';

export type GoogleCredentials = Auth.Credentials;
export type GoogleOauth2Client = Auth.OAuth2Client;
export const GoogleOAuth2Client = Auth.OAuth2Client;

Then elsewhere...

import { GoogleCredentials,  GoogleOAuth2Client } from './google/types';

...
// Using the type ...
let oauthClient: GoogleOAuth2Client;

/// ... and later, using the class ...
oauthClient = new GoogleOAuth2Client(...);

pwagener avatar Oct 25 '20 19:10 pwagener

This shouldn't be closed. Having got the most recent googleapis -^66.0.0 Auth is still not exported.

Also when clicking link - 'typescript' from - https://npm.io/package/googleapis It returns 404 page error. 12/12/2020 1130am

It looks like the problem is here:

  • googleapis/build/src/index.js
const google = new googleapis_1.GoogleApis();
exports.google = google;
exports.Auth = require("google-auth-library");

google-auth-library is a dependency of this node page but it isn't getting built and therefore not available to export. To work around this I have installed google-auth-library as an additional package and imported it from there:

import { google } from 'googleapis';
import Auth from 'google-auth-library';

inspiraller avatar Dec 12 '20 11:12 inspiraller

I have no idea what npm.io is, but canonical it is not :) The link for TypeScript works from npmjs.com: https://www.npmjs.com/package/googleapis#typescript

To get a reference to the auth stuff, please, please do not install your own version of google-auth-library. This is going to end up in diamond dependency hell, where the version of google-auth-library is incompatible (way worse with TypeScript) with the version linked in with googleapis. This should work:

import {
  google,     // top level library used to snag API
  drive_v2,   // type for a specific service
  Auth,       // all of the auth stuff
} from 'googleapis';

let drive: drive_v2.Drive;
let auth: Auth.OAuth2Client;

drive = google.drive('v2');
auth = new Auth.OAuth2Client({ ... });
drive.files.get({ ... })

Can you verify this works for you using the latest version?

JustinBeckwith avatar Dec 12 '20 18:12 JustinBeckwith

@JustinBeckwith This is really odd. I removed google-auth-library, reinstalled my node packages. Did a npm cache clean to be sure. It works now. Strange. Thanks for reconfirmig.

inspiraller avatar Dec 12 '20 20:12 inspiraller

I'm trying to access OAuth2Client from @googleapis/calendar submodule, but I can't... Moreover, I tried using the above typeof hack, but there's a mismatch between the OAuth2Client returned by the method and the type accessed through typeof

import { auth, AuthPlus, calendar } from '@googleapis/calendar';
import { v4 as uuidv4 } from 'uuid';
import { User } from '../entities/user.entity';
import { CLIENT_ID, CLIENT_SECRET, REDIRECT_URL } from '../util/config.util';
import { getFormattedDate } from '../util/functions.util';

type OAuth2Client = typeof auth.OAuth2;

class GoogleServiceClass {
  private googleClient!: OAuth2Client;

  constructor() {
    this.googleClient = new auth.OAuth2({
      clientId: CLIENT_ID,
      clientSecret: CLIENT_SECRET,
      redirectUri: REDIRECT_URL,
    });
  }

image

Sheharyar566 avatar Jun 08 '21 06:06 Sheharyar566