googleworkspace / apps-script-oauth2

An OAuth2 library for Google Apps Script.
https://developers.google.com/apps-script/
Apache License 2.0
1.56k stars 429 forks source link

Support for accessing token endpoint response JSON object #327

Closed lewis-cobry closed 3 years ago

lewis-cobry commented 3 years ago

I've been using this library to connect to an API, but now that my app is on the production environment, I need access to the OAuth2 token endpoint's JSON response to retrieve a field called 'api_endpoint' which is returned with the rest of the normal OAuth2 fields.

Having looked at the code, it doesn't seem like I can return the response directly without it being passed into getTokenFromResponse_(), meaning I don't have an opportunity to extract the response in my own code.

Is it currently possible to do this? Thanks :\^)

lewis-cobry commented 3 years ago

Solved this with the following code:

Service_.prototype.getApiEndpoint = function() {
  if (!this.hasAccess()) {
    throw new Error('Access not granted or expired');
  }
  let token = this.getToken();
  try {
    return token.api_endpoint;
  }
  catch {
    return null;
  }
}