Comments (9)
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.
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.
When I run run()
it comes back with:
Open the following URL and re-run the script: undefined
from apps-script-oauth1.
Make sure you update to version 15 of the library.
from apps-script-oauth1.
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.
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.
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.
My apologies! After publishing a new version of the web app, it started working!
from apps-script-oauth1.
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)
- QuickBooks error when querying with = HOT 1
- Modify Callback Function? HOT 2
- Not working anymore? HOT 1
- Lack of RSA-SHA1 support HOT 3
- Support for "2-legged" OAuth v1.0a? HOT 6
- Goodreads OAuth Problems HOT 3
- Support for Standalone Google Apps Script Projects HOT 1
- OAUTH Token undefined HOT 2
- OAuth1a without TokenUrl HOT 5
- Can I pass Basic Auth of request url ? HOT 4
- Unknown or previously rejected token HOT 1
- XeroPrivate.gs needs PLAINTEXT oauth signature method but RSA-SHA1 encoded consumer secret HOT 1
- Upgrade to the latest version of the OAuth signer library
- Implement fetchAll
- OAuth1 not working in Chrome V8 Engine HOT 1
- Trello Invalid Signature HOT 1
- Security: OAuth Client ID and Secret are accessible to any user of an Apps Script in almost all samples HOT 1
- [SyncRepoSettings bot] - Invalid config file
- .
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apps-script-oauth1.