OAuth in Google Apps Scripts Libraries

2019-04-15 03:53发布

I am building some wrapper libraries in GAS for some of the Domain Admin APIs. I have a general library which handles OAuth, UrlFetch, XML to Object and Object to XML functions. I have added this as a Library to my API wrappers. The first one I created was for the EmailAuditAPI. This works just fine when I load the EmailAuditAPI as a library. The second API wrapper I created was for the GmailSettingsAPI. This wrapper works fine within itself, but is not working when I call it from another script where it has been loaded as a library. For the more visual of you

GoogleAPITools -> EmailAuditAPI Wrapper -> Project Script : Works!

GoogleAPITools -> GmailSettingsAPI Wrapper -> Project Script : Doesn't work!

GoogleAPITools -> GmailSettingsAPI Wrapper : Works!

I think this is the important code:

GoogleAPITools
function callGApi(apiUrl,authScope,method,payloadIn)
{  
  //oAuth & Fetch Arguments
  var fetchArgs = googleOAuth_("google", authScope);
  fetchArgs.method = method ? method : "GET";

  if(payloadIn)
  {
    fetchArgs.contentType = "application/atom+xml";
    fetchArgs.payload = payloadIn;
  }

  var urlFetch = UrlFetchApp.fetch(apiUrl, fetchArgs); //This line fails w/ not working wrapper
  var returnObject = urlFetch.getContentText() != '' ? Xml.parse(urlFetch.getContentText()) : urlFetch.getResponseCode();
  return returnObject;
}

//Google oAuth
function googleOAuth_(name,scope)
{
  var oAuthConfig = UrlFetchApp.addOAuthService(name);
  oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
  oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
  oAuthConfig.setConsumerKey("anonymous");
  oAuthConfig.setConsumerSecret("anonymous");
  return {oAuthServiceName:name, oAuthUseToken:"always"};
}

This wrapper works when loaded into a third script as a library.

function getAccountInfoRequest(user,reqId)
{
  var properties = GoogleAPITools.callGApi(ACCOUNTINFOURL + user + "/" + reqId,AUDITSCOPE).entry.property;
  return GoolgeAPITools.returnXmlToObject(properties);
}

This wrapper does not work when loaded into a third script as a library, but it does work on its own. The GoogleAPITools is loaded as a library in both wrapper scripts.

GmailSettingsAPI Wrapper
//Get Signature
function getSignatureForUser(user)
{
  var returnedInfo = GoogleAPITools.callGApi(EMAILSETTINGSURL + user + "/signature",EMAILSETTINGSSCOPE);
  return GoolgeAPITools.returnXmlToObject(new Array(returnedInfo.entry.property));
}

Third project script with both wrappers loaded as libraries:

function testApiWrappers() {
  var user = "john";
  var reqId = "123456789";
  Logger.log(AuditApi.getAccountInfoRequest(user,reqId));
  Logger.log(GmailSettingsApi.getSignatureForUser(user)); //Fails here
}

In my log I see the return for the AuditApi call, but I get an "Unexpected error:" that references the URLFetch in the GoogleAPITools script.

Is there something different about the Oauth for the two APIs? Is there something glaring in my code that I missed? Any assistance would be great.

0条回答
登录 后发表回答