@@ -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) {
396418Service_ . 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.
0 commit comments