File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -1394,6 +1394,14 @@ def execute(self, http=None):
1394
1394
if http is None :
1395
1395
raise ValueError ("Missing a valid http object." )
1396
1396
1397
+ # Special case for OAuth2Credentials-style objects which have not yet been
1398
+ # refreshed with an initial access_token.
1399
+ if getattr (http .request , 'credentials' , None ) is not None :
1400
+ creds = http .request .credentials
1401
+ if not getattr (creds , 'access_token' , None ):
1402
+ LOGGER .info ('Attempting refresh to obtain initial access_token' )
1403
+ creds .refresh (http )
1404
+
1397
1405
self ._execute (http , self ._order , self ._requests )
1398
1406
1399
1407
# Loop over all the requests and check for 401s. For each 401 request the
Original file line number Diff line number Diff line change @@ -1072,6 +1072,34 @@ def test_execute_request_body(self):
1072
1072
header = parts [1 ].splitlines ()[1 ]
1073
1073
self .assertEqual ('Content-Type: application/http' , header )
1074
1074
1075
+ def test_execute_initial_refresh_oauth2 (self ):
1076
+ batch = BatchHttpRequest ()
1077
+ callbacks = Callbacks ()
1078
+ cred = MockCredentials ('Foo' )
1079
+
1080
+ # Pretend this is a OAuth2Credentials object
1081
+ cred .access_token = None
1082
+
1083
+ http = HttpMockSequence ([
1084
+ ({'status' : '200' ,
1085
+ 'content-type' : 'multipart/mixed; boundary="batch_foobarbaz"' },
1086
+ BATCH_SINGLE_RESPONSE ),
1087
+ ])
1088
+
1089
+ cred .authorize (http )
1090
+
1091
+ batch .add (self .request1 , callback = callbacks .f )
1092
+ batch .execute (http = http )
1093
+
1094
+ self .assertEqual ({'foo' : 42 }, callbacks .responses ['1' ])
1095
+ self .assertIsNone (callbacks .exceptions ['1' ])
1096
+
1097
+ self .assertEqual (1 , cred ._refreshed )
1098
+
1099
+ self .assertEqual (1 , cred ._authorized )
1100
+
1101
+ self .assertEqual (1 , cred ._applied )
1102
+
1075
1103
def test_execute_refresh_and_retry_on_401 (self ):
1076
1104
batch = BatchHttpRequest ()
1077
1105
callbacks = Callbacks ()
You can’t perform that action at this time.
0 commit comments