Skip to content

Commit 477cce9

Browse files
author
Eric Koleda
committed
1.32.0
1 parent 7b9367a commit 477cce9

File tree

7 files changed

+205
-30
lines changed

7 files changed

+205
-30
lines changed

dist/OAuth2.gs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ function createService(serviceName) {
6363
/**
6464
* Returns the redirect URI that will be used for a given script. Often this URI
6565
* needs to be entered into a configuration screen of your OAuth provider.
66-
* @param {string} scriptId The script ID of your script, which can be found in
67-
* the Script Editor UI under "File > Project properties".
66+
* @param {string} [optScriptId] The script ID of your script, which can be
67+
* found in the Script Editor UI under "File > Project properties". Defaults
68+
* to the script ID of the script being executed.
6869
* @return {string} The redirect URI.
6970
*/
70-
function getRedirectUri(scriptId) {
71+
function getRedirectUri(optScriptId) {
72+
var scriptId = optScriptId || ScriptApp.getScriptId();
7173
return 'https://script.google.com/macros/d/' + encodeURIComponent(scriptId) +
7274
'/usercallback';
7375
}
@@ -116,7 +118,6 @@ var Service_ = function(serviceName) {
116118
this.params_ = {};
117119
this.tokenFormat_ = TOKEN_FORMAT.JSON;
118120
this.tokenHeaders_ = null;
119-
this.scriptId_ = eval('Script' + 'App').getScriptId();
120121
this.expirationMinutes_ = 60;
121122
};
122123

@@ -384,6 +385,27 @@ Service_.prototype.setGrantType = function(grantType) {
384385
return this;
385386
};
386387

388+
/**
389+
* Sets the URI to redirect to when the OAuth flow has completed. By default the
390+
* library will provide this value automatically, but in some rare cases you may
391+
* need to override it.
392+
* @param {string} redirectUri The redirect URI.
393+
* @return {Service_} This service, for chaining.
394+
*/
395+
Service_.prototype.setRedirectUri = function(redirectUri) {
396+
this.redirectUri_ = redirectUri;
397+
return this;
398+
};
399+
400+
/**
401+
* Returns the redirect URI that will be used for this service. Often this URI
402+
* needs to be entered into a configuration screen of your OAuth provider.
403+
* @return {string} The redirect URI.
404+
*/
405+
Service_.prototype.getRedirectUri = function() {
406+
return this.redirectUri_ || getRedirectUri();
407+
};
408+
387409
/**
388410
* Gets the authorization URL. The first step in getting an OAuth2 token is to
389411
* have the user visit this URL and approve the authorization request. The
@@ -396,12 +418,10 @@ Service_.prototype.setGrantType = function(grantType) {
396418
Service_.prototype.getAuthorizationUrl = function(optAdditionalParameters) {
397419
validate_({
398420
'Client ID': this.clientId_,
399-
'Script ID': this.scriptId_,
400421
'Callback function name': this.callbackFunctionName_,
401422
'Authorization base URL': this.authorizationBaseUrl_
402423
});
403424

404-
var redirectUri = getRedirectUri(this.scriptId_);
405425
var stateTokenBuilder = eval('Script' + 'App').newStateToken()
406426
.withMethod(this.callbackFunctionName_)
407427
.withArgument('serviceName', this.serviceName_)
@@ -414,7 +434,7 @@ Service_.prototype.getAuthorizationUrl = function(optAdditionalParameters) {
414434
var params = {
415435
client_id: this.clientId_,
416436
response_type: 'code',
417-
redirect_uri: redirectUri,
437+
redirect_uri: this.getRedirectUri(),
418438
state: stateTokenBuilder.createToken()
419439
};
420440
params = extend_(params, this.params_);
@@ -441,15 +461,13 @@ Service_.prototype.handleCallback = function(callbackRequest) {
441461
validate_({
442462
'Client ID': this.clientId_,
443463
'Client Secret': this.clientSecret_,
444-
'Script ID': this.scriptId_,
445464
'Token URL': this.tokenUrl_
446465
});
447-
var redirectUri = getRedirectUri(this.scriptId_);
448466
var payload = {
449467
code: code,
450468
client_id: this.clientId_,
451469
client_secret: this.clientSecret_,
452-
redirect_uri: redirectUri,
470+
redirect_uri: this.getRedirectUri(),
453471
grant_type: 'authorization_code'
454472
};
455473
var token = this.fetchToken_(payload);
@@ -530,16 +548,6 @@ Service_.prototype.getLastError = function() {
530548
return this.lastError_;
531549
};
532550

533-
/**
534-
* Returns the redirect URI that will be used for this service. Often this URI
535-
* needs to be entered into a configuration screen of your OAuth provider.
536-
* @return {string} The redirect URI.
537-
*/
538-
Service_.prototype.getRedirectUri = function() {
539-
return getRedirectUri(this.scriptId_);
540-
};
541-
542-
543551
/**
544552
* Fetches a new token from the OAuth server.
545553
* @param {Object} payload The token request payload.

docs/Service_.html

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3501,6 +3501,160 @@ <h5>Parameters:</h5>
35013501

35023502

35033503

3504+
<h5>Returns:</h5>
3505+
3506+
3507+
<div class="param-desc">
3508+
This service, for chaining.
3509+
</div>
3510+
3511+
3512+
3513+
<dl>
3514+
<dt>
3515+
Type
3516+
</dt>
3517+
<dd>
3518+
3519+
<span class="param-type"><a href="Service_.html">Service_</a></span>
3520+
3521+
3522+
</dd>
3523+
</dl>
3524+
3525+
3526+
3527+
3528+
3529+
3530+
3531+
3532+
3533+
3534+
3535+
3536+
3537+
<h4 class="name" id="setRedirectUri"><span class="type-signature"></span>setRedirectUri<span class="signature">(redirectUri)</span><span class="type-signature"> &rarr; {<a href="Service_.html">Service_</a>}</span></h4>
3538+
3539+
3540+
3541+
3542+
3543+
3544+
<div class="description">
3545+
Sets the URI to redirect to when the OAuth flow has completed. By default the
3546+
library will provide this value automatically, but in some rare cases you may
3547+
need to override it.
3548+
</div>
3549+
3550+
3551+
3552+
3553+
3554+
3555+
3556+
3557+
3558+
<h5>Parameters:</h5>
3559+
3560+
3561+
<table class="params">
3562+
<thead>
3563+
<tr>
3564+
3565+
<th>Name</th>
3566+
3567+
3568+
<th>Type</th>
3569+
3570+
3571+
3572+
3573+
3574+
<th class="last">Description</th>
3575+
</tr>
3576+
</thead>
3577+
3578+
<tbody>
3579+
3580+
3581+
<tr>
3582+
3583+
<td class="name"><code>redirectUri</code></td>
3584+
3585+
3586+
<td class="type">
3587+
3588+
3589+
<span class="param-type">string</span>
3590+
3591+
3592+
3593+
</td>
3594+
3595+
3596+
3597+
3598+
3599+
<td class="description last">The redirect URI.</td>
3600+
</tr>
3601+
3602+
3603+
</tbody>
3604+
</table>
3605+
3606+
3607+
3608+
3609+
3610+
3611+
<dl class="details">
3612+
3613+
3614+
3615+
3616+
3617+
3618+
3619+
3620+
3621+
3622+
3623+
3624+
3625+
3626+
3627+
3628+
3629+
3630+
3631+
3632+
3633+
3634+
3635+
3636+
3637+
3638+
3639+
3640+
3641+
3642+
3643+
3644+
</dl>
3645+
3646+
3647+
3648+
3649+
3650+
3651+
3652+
3653+
3654+
3655+
3656+
3657+
35043658
<h5>Returns:</h5>
35053659

35063660

@@ -4668,7 +4822,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.
46684822
<br class="clear">
46694823

46704824
<footer>
4671-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Nov 14 2018 09:13:44 GMT-0500 (EST)
4825+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jan 08 2019 11:46:13 GMT-0500 (EST)
46724826
</footer>
46734827

46744828
<script> prettyPrint(); </script>

docs/Storage_.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.
763763
<br class="clear">
764764

765765
<footer>
766-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Nov 14 2018 09:13:44 GMT-0500 (EST)
766+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jan 08 2019 11:46:13 GMT-0500 (EST)
767767
</footer>
768768

769769
<script> prettyPrint(); </script>

docs/global.html

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ <h5>Returns:</h5>
732732

733733

734734

735-
<h4 class="name" id="getRedirectUri"><span class="type-signature"></span>getRedirectUri<span class="signature">(scriptId)</span><span class="type-signature"> &rarr; {string}</span></h4>
735+
<h4 class="name" id="getRedirectUri"><span class="type-signature"></span>getRedirectUri<span class="signature">(optScriptId<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {string}</span></h4>
736736

737737

738738

@@ -765,6 +765,8 @@ <h5>Parameters:</h5>
765765
<th>Type</th>
766766

767767

768+
<th>Attributes</th>
769+
768770

769771

770772

@@ -777,7 +779,7 @@ <h5>Parameters:</h5>
777779

778780
<tr>
779781

780-
<td class="name"><code>scriptId</code></td>
782+
<td class="name"><code>optScriptId</code></td>
781783

782784

783785
<td class="type">
@@ -790,11 +792,22 @@ <h5>Parameters:</h5>
790792
</td>
791793

792794

795+
<td class="attributes">
796+
797+
&lt;optional><br>
798+
799+
800+
801+
802+
803+
</td>
804+
793805

794806

795807

796-
<td class="description last">The script ID of your script, which can be found in
797-
the Script Editor UI under "File > Project properties".</td>
808+
<td class="description last">The script ID of your script, which can be
809+
found in the Script Editor UI under "File > Project properties". Defaults
810+
to the script ID of the script being executed.</td>
798811
</tr>
799812

800813

@@ -1348,7 +1361,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.
13481361
<br class="clear">
13491362

13501363
<footer>
1351-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Nov 14 2018 09:13:44 GMT-0500 (EST)
1364+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jan 08 2019 11:46:13 GMT-0500 (EST)
13521365
</footer>
13531366

13541367
<script> prettyPrint(); </script>

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Service_.
689689
<br class="clear">
690690

691691
<footer>
692-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Nov 14 2018 09:13:44 GMT-0500 (EST)
692+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jan 08 2019 11:46:13 GMT-0500 (EST)
693693
</footer>
694694

695695
<script> prettyPrint(); </script>

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apps-script-oauth2",
3-
"version": "1.31.0",
3+
"version": "1.32.0",
44
"description": "OAuth2 for Apps Script is a library for Google Apps Script that provides the ability to create and authorize OAuth2 tokens as well as refresh them when they expire.",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)