File tree Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -313,6 +313,13 @@ in these requests.
313313
314314See the [ FitBit sample] ( samples/FitBit.gs ) for the complete code.
315315
316+ #### Setting the token HTTP method
317+
318+ Almost all services use the ` POST ` HTTP method when retrieving the access token,
319+ but a few services deviate from the spec and use the ` PUT ` method instead. To
320+ accomodate those cases you can use the ` setTokenMethod() ` method to specify the
321+ HTTP method to use when making the request.
322+
316323#### Modifying the access token payload
317324
318325Some OAuth providers, such as the Smartsheet API, require you to
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ var Service_ = function(serviceName) {
3333 this . params_ = { } ;
3434 this . tokenFormat_ = TOKEN_FORMAT . JSON ;
3535 this . tokenHeaders_ = null ;
36+ this . tokenMethod_ = 'post' ;
3637 this . expirationMinutes_ = 60 ;
3738} ;
3839
@@ -106,6 +107,17 @@ Service_.prototype.setTokenHeaders = function(tokenHeaders) {
106107 return this ;
107108} ;
108109
110+ /**
111+ * Sets the HTTP method to use when retrieving or refreshing the access token.
112+ * Default: "post".
113+ * @param {string } tokenMethod The HTTP method to use.
114+ * @return {!Service_ } This service, for chaining.
115+ */
116+ Service_ . prototype . setTokenMethod = function ( tokenMethod ) {
117+ this . tokenMethod_ = tokenMethod ;
118+ return this ;
119+ } ;
120+
109121/**
110122 * @callback tokenHandler
111123 * @param tokenPayload {Object} A hash of parameters to be sent to the token
@@ -508,7 +520,7 @@ Service_.prototype.fetchToken_ = function(payload, optUrl) {
508520 payload = this . tokenPayloadHandler_ ( payload ) ;
509521 }
510522 var response = UrlFetchApp . fetch ( url , {
511- method : 'post' ,
523+ method : this . tokenMethod_ ,
512524 headers : headers ,
513525 payload : payload ,
514526 muteHttpExceptions : true
Original file line number Diff line number Diff line change @@ -770,4 +770,31 @@ describe('Utilities', function() {
770770 assert . deepEqual ( payload , { 'foo' : 'bar' } ) ;
771771 } ) ;
772772 } ) ;
773+
774+ describe ( '#setTokenMethod()' , function ( ) {
775+ var decodeJwt_ = OAuth2 . decodeJwt_ ;
776+
777+ it ( 'should defautl to POST' , function ( done ) {
778+ mocks . UrlFetchApp . resultFunction = function ( url , urlOptions ) {
779+ assert . equal ( urlOptions . method , 'post' ) ;
780+ done ( ) ;
781+ } ;
782+ var service = OAuth2 . createService ( 'test' )
783+ . setGrantType ( 'client_credentials' )
784+ . setTokenUrl ( 'http://www.example.com' )
785+ service . exchangeGrant_ ( ) ;
786+ } ) ;
787+
788+ it ( 'should change the HTTP method used' , function ( done ) {
789+ mocks . UrlFetchApp . resultFunction = function ( url , urlOptions ) {
790+ assert . equal ( urlOptions . method , 'put' ) ;
791+ done ( ) ;
792+ } ;
793+ var service = OAuth2 . createService ( 'test' )
794+ . setGrantType ( 'client_credentials' )
795+ . setTokenUrl ( 'http://www.example.com' )
796+ . setTokenMethod ( 'put' ) ;
797+ service . exchangeGrant_ ( ) ;
798+ } ) ;
799+ } ) ;
773800} ) ;
You can’t perform that action at this time.
0 commit comments