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
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;
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.
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
I tried @angelxmoreno 's example, but it doesn't seem to work any more:

@arkus7's hack is the only thing I can get to work:
type OAuth2Client = typeof google.auth.OAuth2.prototype
@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(...);
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:
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';
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
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.
Most helpful comment
Found this looking for the same answers. Thanks @JustinBeckwith for the work. Here is an example that answers @YoraiLevi 's question
Authis the namespace for thegoogle-auth-librarylib that this repo uses