[Beginner] OAuth2 with Google Script

Hi,

I’m beginner in API and I want to use Google Script to do OAuth2 to get access token from Twitch API. And use implicit way to get authorization.
Document OAuth2 for Google Scripts is here: https://github.com/googlesamples/apps-script-oauth2

My program is

function GetTwitchService_() {
  // Create a new service with the given name. The name will be used when
  // persisting the authorized token, so ensure it is unique within the
  // scope of the property store.
  return OAuth2.createService('twitch')

      // Set the endpoint URLs, which are the same for all Google services.
      .setAuthorizationBaseUrl('https://api.twitch.tv/kraken/oauth2/authorize')
      .setTokenUrl('https://api.twitch.tv/kraken/oauth2/token')

      // Set the client ID and secret, from the Google Developers Console.
      .setClientId('myClientId')
      .setClientSecret('myClientSecret')

      // Set the name of the callback function in the script referenced
      // above that should be invoked to complete the OAuth flow.
      .setCallbackFunction('authCallback')

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getUserProperties())

      // Set the scopes to request (space-separated for Google services).
      .setScope('user_read channel_read');

//  .setParam('response_type', 'token')
//  .setParam('client_id', '3flmpq1ka8j5g5wjslxtd9yk7r8xi5j')
//  .setParam('redirect_uri', 'https://script.google.com/macros/d/myProjectId/usercallback');  
}

function authCallback(request) {
  var twitchService = GetTwitchService_();
  var isAuthorized = twitchService.handleCallback(request);
  if (isAuthorized) {
    return HtmlService.createHtmlOutput('Success! You can close this tab.');
  } else {
    return HtmlService.createHtmlOutput('Denied. You can close this tab');
  }
}

And then I try to get OAuth2 service as following

var twitchService = GetTwitchService_();

but it throws an error “Access not granted or expired. (line 352, file “”)

I think there are some my mistakes in creating service, but I could not figure it out.
I’m really appreciate that if anyone can help me get out of this problem.

Note that, the Redirect URL in Twitch App, I already set it as: https://script.google.com/macros/d/myProjectId/usercallback

Thanks,
Thai Ngo

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.