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

Commit 5b3e324

Browse files
author
ekoleda
committed
Escape application/x-www-form-urlencoded payloads using the same method used when generating the OAuth signature, to ensure that they are in sync. Fixed #10.
1 parent 3322dde commit 5b3e324

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

Service.gs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,17 @@ Service_.prototype.fetchInternal_ = function(url, params, opt_token,
396396
default:
397397
throw 'Unknown param location: ' + this.paramLocation_;
398398
}
399+
if (params.payload && (!params.contentType ||
400+
params.contentType == 'application/x-www-form-urlencoded')) {
401+
// Disable UrlFetchApp escaping and use the signer's escaping instead.
402+
// This will ensure that the escaping is consistent between the signature and the request.
403+
var payload = request.data;
404+
payload = Object.keys(payload).map(function(key) {
405+
return signer.percentEncode(key) + '=' + signer.percentEncode(payload[key]);
406+
}).join('&');
407+
params.payload = payload;
408+
params.escaping = false;
409+
}
399410
return UrlFetchApp.fetch(url, params);
400411
};
401412

samples/TripIt.gs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function reset() {
3030
* Configures the service.
3131
*/
3232
function getService() {
33-
return OAuth1.createService('Twitter')
33+
return OAuth1.createService('TripIt')
3434
// Set the endpoint URLs.
3535
.setRequestTokenUrl('https://api.tripit.com/oauth/request_token')
3636
.setAuthorizationUrl('https://www.tripit.com/oauth/authorize')

samples/Twitter.gs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ function run() {
1111
var payload = {
1212
status: 'It\'s a tweet!'
1313
};
14-
payload = Object.keys(payload).map(function(key) {
15-
return encodeRfc3986(key) + '=' + encodeRfc3986(payload[key]);
16-
}).join('&');
1714
var response = service.fetch(url, {
1815
method: 'post',
19-
payload: payload,
20-
escaping: false
16+
payload: payload
2117
});
2218
var result = JSON.parse(response.getContentText());
2319
Logger.log(JSON.stringify(result, null, 2));

0 commit comments

Comments
 (0)