Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit 24aa38f

Browse files
author
Eric Koleda
committed
Preserve all fields in parsed tokens. Add Etsy sample.
1 parent 4f614f4 commit 24aa38f

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

samples/Etsy.gs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* This script must be published as a web app (Publish > Deploy as web app) in
3+
* order to function. The web app URL is used instead of the normal callback
4+
* URL to work around a limitation in the Etsy API's OAuth implementation
5+
* (callback URLs are limited to 255 characters).
6+
*/
7+
8+
var CONSUMER_KEY = '...';
9+
var CONSUMER_SECRET = '...';
10+
11+
/**
12+
* Authorizes and makes a request to the Etsy API.
13+
*/
14+
function run() {
15+
var service = getService();
16+
if (service.hasAccess()) {
17+
var url = 'https://openapi.etsy.com/v2/users/__SELF__/profile';
18+
var response = service.fetch(url);
19+
var result = JSON.parse(response.getContentText());
20+
Logger.log(JSON.stringify(result, null, 2));
21+
} else {
22+
service.authorize();
23+
// Retrieve the authorization URL from the "login_url" field of the request
24+
// token.
25+
var authorizationUrl = service.getToken_().login_url;
26+
Logger.log('Open the following URL and re-run the script: %s',
27+
authorizationUrl);
28+
}
29+
}
30+
31+
/**
32+
* Reset the authorization state, so that it can be re-tested.
33+
*/
34+
function reset() {
35+
var service = getService();
36+
service.reset();
37+
}
38+
39+
/**
40+
* Configures the service.
41+
*/
42+
function getService() {
43+
var service = OAuth1.createService('Etsy')
44+
// Set the endpoint URLs.
45+
// Pass the desired scopes in the request token URL.
46+
.setRequestTokenUrl('https://openapi.etsy.com/v2/oauth/request_token?scope=profile_r')
47+
// The authorization URL isn't used, since Etsy sends it back in the
48+
// "login_url" field of the request token.
49+
.setAuthorizationUrl('N/A')
50+
.setAccessTokenUrl('https://openapi.etsy.com/v2/oauth/access_token')
51+
52+
// Set the consumer key and secret.
53+
.setConsumerKey(CONSUMER_KEY)
54+
.setConsumerSecret(CONSUMER_SECRET)
55+
56+
// Set the name of the callback function in the script referenced
57+
// above that should be invoked to complete the OAuth flow.
58+
.setCallbackFunction('authCallback')
59+
60+
// Set the property store where authorized tokens should be persisted.
61+
.setPropertyStore(PropertiesService.getUserProperties());
62+
63+
// Override the callback URL method to use the web app URL instead.
64+
service.getCallbackUrl = function() {
65+
return ScriptApp.getService().getUrl();
66+
};
67+
68+
return service;
69+
}
70+
71+
/**
72+
* Handles GET requests to the web app.
73+
*/
74+
function doGet(request) {
75+
// Determine if the request is part of an OAuth callback.
76+
if (request.parameter.oauth_token) {
77+
var service = getService();
78+
var authorized = service.handleCallback(request);
79+
if (authorized) {
80+
return HtmlService.createHtmlOutput('Success!');
81+
} else {
82+
return HtmlService.createHtmlOutput('Denied');
83+
}
84+
}
85+
}

src/Service.gs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,15 @@ Service_.prototype.fetchInternal_ = function(url, params, opt_token,
450450
* @private
451451
*/
452452
Service_.prototype.parseToken_ = function(content) {
453-
var fields = content.split('&').reduce(function(result, pair) {
453+
var token = content.split('&').reduce(function(result, pair) {
454454
var parts = pair.split('=');
455455
result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
456456
return result;
457457
}, {});
458-
return {
459-
public: fields.oauth_token,
460-
secret: fields.oauth_token_secret
461-
};
458+
// Set fields that the signing library expects.
459+
token.public = token.oauth_token;
460+
token.secret = token.oauth_token_secret;
461+
return token;
462462
};
463463

464464
/**

0 commit comments

Comments
 (0)