MailOnline/google-ad-manager-api

Feature Request: Ability to overload and/or change authenication mechanism

Closed this issue ยท 2 comments

Would really like the ability to use a different authentication mechanism other than JWT which is hard coded and required for construct(). Would like to be able to support any kind of google auth method such as previously obtained OAuth2 credentials (https://developers.google.com/ad-manager/api/authentication). If not then the JWT section should be made optional under construct() and/or give the ability for an extending class to overload the authentication mechanisms as needed to use a different method. Example: this.#jwt is not set until the first call to this.credentials under the parent class so new JWT() is never called (https://github.com/johngeorgewright/google-ad-manager-api/blob/master/src/build/api.ts#L47).

The example below works but requires "valid" JWT options or the TS won't compile.

Example:

import {OAuth2Client} from 'google-auth-library';
const oAuth2Client = new OAuth2Client(
    'oauth_id',
    'oauth_secret'
);
oAuth2Client.setCredentials({credential values from previous OAuth2 acceptance by user});
new GoogleAdManagerApi(oAuth2Client.credentials, 'GoogleAdManager', '#######')

GAM.createNetworkServiceClient()
    .then(async client => {
      const [response] = await client.getAllNetworksAsync({});
      console.log('res', response);
    })
    .catch(error => {
      console.error('error', error);
    });

Example extending class:

import {GoogleAdManager} from '@johngw/google-ad-manager-api';
import {Credentials} from 'google-auth-library';

export class GoogleAdManagerApi extends GoogleAdManager {
  private readonly googleAuth: Credentials;

  /**
   * Overload parent constructor to define bogus information as we are going to bypass JWT authentication
   * @param googleAuth
   * @param applicationName
   * @param networkCode
   */
  constructor(googleAuth: Credentials, applicationName = '', networkCode = '') {
    // Call parent with bogus JWT information
    super({
      applicationName: applicationName,
      networkCode: networkCode,
      jwtOptions: {
        keyId: '',
        key: '',
        email: '',
        scopes: [],
      },
    });

    // Set the credentials we want to use
    this.googleAuth = googleAuth;
  }

  /**
   * Overload credentials to pass back non-JWT credentials
   */
  get credentials(): Promise<Credentials> {
    return new Promise(resolve => {
      resolve(this.googleAuth);
    });
  }
}

I have started a PR: #329

This should allow any kind of google-auth-library authentication.

๐ŸŽ‰ This issue has been resolved in version 7.0.0 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€