0

I'm trying to call StackExchange's API, using Google Apps Script and Google Sheets. I can't figure out where in the OAuth process things are going wrong. My current code is not granting access:

function getStackExchangeService_() {
  var CLIENT_ID = PropertiesService.getScriptProperties().getProperty('SE_CLIENT_ID');
  var CLIENT_SECRET = PropertiesService.getScriptProperties().getProperty('SE_CLIENT_SECRET');

  return OAuth2.createService('StackExchange')
  .setAuthorizationBaseUrl('https://stackoverflow.com/oauth')
  .setTokenUrl('https://stackoverflow.com/oauth/access_token')
  .setClientId(CLIENT_ID)
  .setClientSecret(CLIENT_SECRET)
  .setCallbackFunction('authCallback')
  .setPropertyStore(PropertiesService.getUserProperties())
  .setRedirectUri('https://stackexchange.com/oauth/login_success')
  .setScope('global');
}

When I call this and log the response I always get "false":

var service = getStackExchangeService_();
Logger.log(service.hasAccess());

Thanks for the help!

poyter16
  • 25
  • 3
  • That's not quite a proper [mcve]. And, for example, are you using [this library](https://github.com/gsuitedevs/apps-script-oauth2)? – Brock Adams Jan 14 '19 at 00:38
  • You should also configure the code to relay any (error) messages from the Stack Exchange server and report them here. – Brock Adams Jan 14 '19 at 00:42

1 Answers1

0

The most obvious issue is that global is not a valid Stack Exchange scope.

Probably use .setScope('read_inbox'); to start.

Also, be sure that:

  1. You have registered your app and configured it for explicit (server-side) OAuth2. (Omit the port information in your case.)
  2. Apparently you should use https://script.google.com/macros/d/{SCRIPT ID}/usercallback for the Application Website, per these instructions??.
  3. Which means you would use script.google.com for the OAuth Domain.
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks @brock, this is helpful and I think am on the right track. `https://stackoverflow.com/oauth?client_id={ID}&response_type=code&redirect_uri=https://script.google.com/macros/d/{SCRIPT ID}/usercallback&scope=read_inbox` gets me the code, though `https://stackoverflow.com/oauth/access_token?client_id={ID}&client_secret={SECRET}&code={CODE}&redirect_uri=https://script.google.com/macros/d/{SCRIPT ID}/usercallback` takes me to a "Page not found" on Stackoverflow. Any idea where I am still going wrong? – poyter16 Jan 14 '19 at 19:48
  • @poyter16, it may be that you did not `POST` to stackoverflow.com/oauth/access_token. Also, the `redirect_uri` may be invalid. But we'd need you to provide a proper [mcve] to be sure. Anyway, [mark this Q&A as accepted](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) and ask a new question, with that MCVE, for the new error. – Brock Adams Jan 14 '19 at 20:01