question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Batch request for oauth2 token requests returns 400 Bad Request error

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:34 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
busunkim96commented, Dec 17, 2018

@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.

http error “<HttpError 400 when requesting https://www.googleapis.com/oauth2/v4/token returned “Bad Request”>”

A refresh request needs refresh_token, client_id, client_secret, and grant_type. Here is the documentation on refreshing an access token.

A refresh request should look like this:

POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded

client_id=<your_client_id>&
client_secret=<your_client_secret>&
refresh_token=<refresh_token>&
grant_type=refresh_token

And returns a response

{
  "access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
  "expires_in":3920,
  "token_type":"Bearer"
}
1reaction
SwapnaReddyLcommented, Dec 14, 2018

@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.

httperror
Read more comments on GitHub >

github_iconTop Results From Across the Web

Access token request return bad request - Python SDK
A 400 response means there is something wrong with your request. The response body often indicates what was wrong with the request. You...
Read more >
LTI 1.3 Token Request causes HTTP 400 Bad Request
It's just attempting to get the access token fails and throws a 400 with this error from Canvas. For readability, the token request...
Read more >
OAuth2 with python gdata_api : 400 Bad request
It has worked fine for a few years, until some day it stopped, and began to return error 400 (= bad request) when...
Read more >
Known issues with Microsoft Graph
This article describes known issues and limitations with Microsoft Graph and provides workarounds when possible.
Read more >
Token endpoint - Amazon Cognito - AWS Documentation
Requests to the /oauth2/token endpoint return Amazon Cognito OAuth 2.0 ... HTTP/1.1 400 Bad Request Content-Type: application/json;charset=UTF-8 { "error":" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found