Batch request for oauth2 token requests returns 400 Bad Request error
See original GitHub issueI’m using google api client 1.7.4 with python 2.7 and unable to make successful batch requests with new_batch_http_request api. I keep getting 400 Bad Request error
After debugging google api client lib, final request body is following :
`–===============6159072106655335965== Content-Type: application/http MIME-Version: 1.0 Content-Transfer-Encoding: binary Content-ID: <943c8367-618b-4817-97d1-fe4f4005af25+0AL0173QlVaLoUk9PVA%40%40%3F%7C%3F%40%40administrator%40testcloudlockprimary1.com>
POST /oauth2/v4/token HTTP/1.1 Content-Type: application/json; charset=utf-8 MIME-Version: 1.0 Host: www.googleapis.com content-length: 715
grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=<assertion_code> `
Here is what code looks like : ###code to build batch request and nested requests####### ` logger.info(‘Refreshing credentials for %s users’, len(creds_to_refresh))
oauth_service = build('oauth2', 'v2', http=LoggingHttp())
batch_request = oauth_service.new_batch_http_request()
for request_id, creds in six.iteritems(credentials_to_refresh):
request = build_token_request(creds)
batch_request.add(request, request_id=request_id)
batch_request.execute()
def build_token_request(creds):
request = HttpRequest(http=LoggingHttp(), postproc=_postproc,
uri=creds.token_uri,
method='POST',
body=creds._generate_request_body(),
headers=creds._generate_request_headers())
return request
###methods generating request body and headers###
def _generate_request_body(self):
assertion = self._generate_assertion()
body = urllib.parse.urlencode({
'assertion': assertion,
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
})
return body
def _generate_request_headers(self):
"""Generate the headers that will be used in the refresh request."""
headers = {
'content-type': 'application/json; charset=utf-8',
}
if self.user_agent is not None:
headers['user-agent'] = self.user_agent
return headers
`
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:34 (14 by maintainers)
Top GitHub Comments
@SwapnaReddyL The batching looks correct to me (Batch requests in Python).
Note that if you are putting more than 1000 calls in the batch request you will need to make multiple batch requests.
It looks like it’s the individual requests that don’t have the correct fields.
A refresh request needs
refresh_token
,client_id
,client_secret
, andgrant_type
. Here is the documentation on refreshing an access token.A refresh request should look like this:
And returns a response
@tseaver It’s http error “<HttpError 400 when requesting https://www.googleapis.com/oauth2/v4/token returned “Bad Request”>”
Here is what i found in response content :
–batch_gHThtkbBoB8_AAJxz-pPuhE Content-Type: application/http Content-ID: <response-6b6d70c6-b4d2-46ef-90d2-9b3a1ab0bfb1+0AB7uSPqRpNOnUk9PVA%40%40%3F%7C%3F%40%40administrator%40testcloudlockprimary1.com>
HTTP/1.1 400 Bad Request Content-Type: application/json; charset=UTF-8 Date: Fri, 14 Dec 2018 21:37:50 GMT Expires: Fri, 14 Dec 2018 21:37:50 GMT Cache-Control: private, max-age=0 Content-Length: 103
{ “error”: “internal_failure”, “error_description”: “The request did not match the specified API.” }
–batch_gHThtkbBoB8_AAJxz-pPuhE–
I couldn’t retrieve stack trace information on http error, but attaching screenshot of captured error.