Search code examples
oauth-2.0google-apipostmangoogle-oauthgmail-api

Error "java.security.SignatureException: Invalid signature for token" while testing Google API OAuth2 using Raw format in Postman


Please find below complete details for implementation of SAP-Gmail Integration.

Requirement : SAP- GMail Integration using OAuth2

Process : I created service account and followed below standard procedures defined in a GoogleAPI OAuth2 - Service Account blog.

https://developers.google.com/identity/protocols/oauth2/service-account

Issue : While hitting googleapi for Oauth 2 from SAP , data is pushed in raw format. Which gives error "java.security.SignatureException: Invalid signature for token". Please find below screenshot of error.

Google API OAuth error

Findings : Below are my findings while testing API in postman.

  1. Success of API :

If I am using Body -> x-www-form-urlencoded tab in postman then I am getting Access Token Successfully. With below shown HTTP code build.

Postman Screenshot 1: Postman Success

HTTP Build 1: Http Build 1

  1. Failure of API in Raw format:

If I am using Body-> Raw tab in postman then I am getting error with below shown HTTP code build.

Postman Screenshot 2: Postman error Postman header 2

HTTP Build 2: Http build 2

  1. If you can see above HTTP code build in both, then you can find that both HTTP code are similar still first mode is working and second mode (Raw format) not working.

RootCause : I found one issue while validating error that, when I am posting data with Raw format, then "+" is getting removed from JWToken signature somehow. which is highlighted in error screenshot.

This small bug is causing a trouble and not allowing to create a token.

So can you please help me out how to resolve this issue in Raw format as SAP supports Raw format for posting data.

Regards, Karan


Solution

  • Finally I found the solution for above query myself.

    So actually In Postman, tab "x-www-form-urlencoded" has a background small piece of code which encodes Plain text to URL Encode string with charset"UTF-8".

    This URL encode logic replaces many special characters like "=" to "%3D".

    Solution:- So while sending Raw data to Google API for OAuth2, JsonWebToken string needs to be URL Encoded with charset "UTF-8", as it is expected by GoogleAPI.

    Regards, Karan K