Giter VIP home page Giter VIP logo

Comments (9)

erickoledadevrel avatar erickoledadevrel commented on May 29, 2024 1

The API actually uses 1.0a, meaning the callback URL is passed when fetching the request token. However they limit their callback URLs to 255 characters, which is not enough to hold Apps Script's /usercallback URL and the state token. The work around was to publish the script as the web app and use the web app URL instead.

Etsy also seems to want you to pull the authorization URL out of the request token field "login_url", which previously was being dropped during parsing. I made a change that preserves those extra fields so you can access it.

You can see a working sample here:

https://github.com/googlesamples/apps-script-oauth1/blob/master/samples/Etsy.gs

from apps-script-oauth1.

tylerclark avatar tylerclark commented on May 29, 2024

Here is my current code. It's obviously quick and dirty. (and not really fun or functional)

var CONSUMER_KEY = '...';
var CONSUMER_SECRET = '...';
var SHOP_NAME = '...';

function onOpen(e) {
  SpreadsheetApp.getUi().createAddonMenu().addItem('Start', 'showSidebar').addToUi();
}

function onInstall(e) {
  onOpen(e);
}

function showSidebar() {
  var service = getService();
  if (service.hasAccess()) {
    var response = service.fetch('https://api.etsy.com/v2/shops/' + SHOP_NAME + '/transactions');
    var result = JSON.parse(response.getContentText());
    Logger.log(JSON.stringify(result, null, 2));

    var html = '<style>*{font-family:Arial;font-size:16px;line-height:1.5;}input,button{background:#fff;border:1px #000 solid;padding:10px;border-radius:5px;padding:4px 20px;}</style>'
    +'<b>Authorized</b><br/><a href="#" onClick="javascript:google.script.run.reset();return false">Reset</a>';
    
    var template = HtmlService.createTemplate(html);
    template.authorizationUrl = authorizationUrl;
    var page = template.evaluate();
    SpreadsheetApp.getUi().showSidebar(page);

  } else {
    var authorizationUrl = service.authorize();
    Logger.log('Open the following URL: %s', authorizationUrl);
    
    var callbackUrl = authorizationUrl.split('oauth_callback=');
    var decodedCallbackUrl = unescape(callbackUrl[1]);
    Logger.log(decodedCallbackUrl);
    
    var html = '<style>*{font-family:Arial;font-size:16px;line-height:1.5;}input,button{background:#fff;border:1px #000 solid;padding:10px;border-radius:5px;padding:4px 20px;}</style>'
    +'<b>Not yet authorized</b><br/><a href="<?= authorizationUrl ?>" target="_blank">Click here to login</a>, then enter verification code in the box below.<br/><br/>'
    +'<center><input type="text" size="10" style="text-align:center;" id="verCode" placeholder="Code" /> <button onClick="google.script.run.sendBackVerifier(\'' + decodedCallbackUrl + '&oauth_verifier=\' + document.getElementById(\'verCode\').value);">Verify</button></center>';
    
    var template = HtmlService.createTemplate(html);
    template.authorizationUrl = authorizationUrl;
    var page = template.evaluate();
    SpreadsheetApp.getUi().showSidebar(page);
    
  }
}

function sendBackVerifier(url) {
  Logger.log('sendBackVerifier: ' + url);
  var service = getService();
  var response = service.fetch(url); // Can't get this to run. But if I visit the URL manually, it'll run authCallback()
  HtmlService.createHtmlOutput('Success!');
}

/**
 * Reset the authorization state, so that it can be re-tested.
 */
function reset() {
  var service = getService();
  service.reset();
}

/**
 * Configures the service.
 */
function getService() {
  return OAuth1.createService('Etsy')
      .setRequestTokenUrl('https://openapi.etsy.com/v2/oauth/request_token?scope=transactions_r')
      .setAuthorizationUrl('https://www.etsy.com/oauth/signin')
      .setAccessTokenUrl('https://openapi.etsy.com/v2/oauth/access_token')
      .setOAuthVersion('1.0')
      .setConsumerKey(CONSUMER_KEY)
      .setConsumerSecret(CONSUMER_SECRET)
      .setCallbackFunction('authCallback')
      .setPropertyStore(PropertiesService.getUserProperties());
}

/**
 * Handles the OAuth2 callback.
 */
function authCallback(request) {
  Logger.log('authCallback called: ' + request);
  var service = getService();
  var authorized = service.handleCallback(request);
  if (authorized) {
    Logger.log('Success');
    return HtmlService.createHtmlOutput('Success!');
  } else {
    Logger.log('Denied');
    return HtmlService.createHtmlOutput('Denied');
  }
}

from apps-script-oauth1.

tylerclark avatar tylerclark commented on May 29, 2024

When I run run() it comes back with:

Open the following URL and re-run the script: undefined

from apps-script-oauth1.

erickoledadevrel avatar erickoledadevrel commented on May 29, 2024

Make sure you update to version 15 of the library.

from apps-script-oauth1.

tylerclark avatar tylerclark commented on May 29, 2024

Thanks for the prompt reply. It now returns a URL, however when I visit that URL, login, and click "Allow Access", it sends me back to a script.google.com page that says:

Script function not found: doGet

I do have that method at the bottom as I've used your example in it's entirety.

from apps-script-oauth1.

erickoledadevrel avatar erickoledadevrel commented on May 29, 2024

Make sure you have the doGet() function as shown in the sample. You may need to publish a new version of your web app.

from apps-script-oauth1.

tylerclark avatar tylerclark commented on May 29, 2024

Thank you for your help on this matter! Greatly appreciated. I do have that doGet() function as shown in the example. I've even created a screen recording to demonstrate this: https://youtu.be/rhxQBPHLJbo

from apps-script-oauth1.

tylerclark avatar tylerclark commented on May 29, 2024

My apologies! After publishing a new version of the web app, it started working! bitmoji

from apps-script-oauth1.

anthonyponcex avatar anthonyponcex commented on May 29, 2024

Hi @tylerclark .. I'm unable to get past this step with "doGet()" despire 'publishing a new version'.

Can you walk me through the steps you took?

from apps-script-oauth1.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.