googleworkspace / apps-script-oauth2

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

Version for Zoho CRM doesn't appear to use refresh token automatically #223

Closed RonaldJCoote closed 4 years ago

RonaldJCoote commented 4 years ago

Hi - great sample code, thanks. For the Zoho CRM version, I need to manually reconnect each hour - i.e. the code doesn't appear to be using the refresh tokens automatically.

Zoho says access tokens expire after an hour: https://www.zoho.com/crm/developer/docs/api/refresh.html

The response from the initial auth connection is as follows (gleaned from PropertiesService.getUserProperties().getProperties())):

{"oauth2.Zoho.account-server":"\"https://accounts.zoho.com\"","oauth2.Zoho":"{\"access_token\":\"1000.xxxxxxxxxxxxx.xxxxxxxxx59a3c9172a902c2eae4\",\"refresh_token\":\"1000.98cb29989f30xxxxxxxxxxxxxxxxx.xxxxxxxxxxxxd3e0937e156972a430ef68\",\"api_domain\":\"https://www.zohoapis.com\",\"token_type\":\"Bearer\",\"expires_in\":3600,\"granted_time\":1583332920}"}

so a refresh token is being returned correctly.

Is the apps-script-oauth2 code meant to automatically handle refresh tokens?

Thanks,

RC

erickoledadevrel commented 4 years ago

Yes, it is supposed to refresh automatically, and it looks like your have the correct information in the token response. It could be there is some error encountered when refreshing the token. Those errors get swallowed by default, but you can read them using service.getLastError(). For example:

if (service.hasAccess()) {
  // ...
} else {
  console.log(service.getLastError());
  // ...
}
erickoledadevrel commented 4 years ago

Closing due to lack of response.

RonaldJCoote commented 4 years ago

Hi @erickoledadevrel - thanks for your patience. I have confirmed with Zoho Support that it was a problem at their end. They changed the format for the issued tokens and associated parameters, about 18 months ago, to correct this bug. But all previously-issued tokens were not updated - and hence mine were failing since I created mine earlier. (Poor comms on their part, to not alert existing token-owners.....). All now resolved. Thanks again.

erickoledadevrel commented 4 years ago

Thanks for following up, and glad to hear you found the root cause.