File tree 3 files changed +47
-1
lines changed
3 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -313,6 +313,13 @@ in these requests.
313
313
314
314
See the [ FitBit sample] ( samples/FitBit.gs ) for the complete code.
315
315
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
+
316
323
#### Modifying the access token payload
317
324
318
325
Some 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) {
33
33
this . params_ = { } ;
34
34
this . tokenFormat_ = TOKEN_FORMAT . JSON ;
35
35
this . tokenHeaders_ = null ;
36
+ this . tokenMethod_ = 'post' ;
36
37
this . expirationMinutes_ = 60 ;
37
38
} ;
38
39
@@ -106,6 +107,17 @@ Service_.prototype.setTokenHeaders = function(tokenHeaders) {
106
107
return this ;
107
108
} ;
108
109
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
+
109
121
/**
110
122
* @callback tokenHandler
111
123
* @param tokenPayload {Object} A hash of parameters to be sent to the token
@@ -508,7 +520,7 @@ Service_.prototype.fetchToken_ = function(payload, optUrl) {
508
520
payload = this . tokenPayloadHandler_ ( payload ) ;
509
521
}
510
522
var response = UrlFetchApp . fetch ( url , {
511
- method : 'post' ,
523
+ method : this . tokenMethod_ ,
512
524
headers : headers ,
513
525
payload : payload ,
514
526
muteHttpExceptions : true
Original file line number Diff line number Diff line change @@ -770,4 +770,31 @@ describe('Utilities', function() {
770
770
assert . deepEqual ( payload , { 'foo' : 'bar' } ) ;
771
771
} ) ;
772
772
} ) ;
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
+ } ) ;
773
800
} ) ;
You can’t perform that action at this time.
0 commit comments