Search code examples
oauthmigrationgdatagoogle-oauth

Google oauth1 migration to oauth2: Invalid authorization header


I am stuck with the oauth1 migration to oauth2. I don't want to ask my users to grant contact access again, so I prefer to do migration myself but I am getting hard time figuring out why it doesn't work.

I'm getting this error from Google server:

DEBUG -  << "  "error" : "invalid_request",[\n]"
DEBUG -  << "  "error_description" : "Invalid authorization header."[\n]"

here is my code, I did almost the same thing when consuming google api, but for migration it is not working.

GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey(getConsumerKey());
oauthParameters.setOAuthConsumerSecret(getConsumerSecret());
oauthParameters.setOAuthToken(token);

ClassPathResource cpr = new ClassPathResource("mykey.pk8");
File file = cpr.getFile();
PrivateKey privKey = getPrivateKey(file);

OAuthRsaSha1Signer signer = new OAuthRsaSha1Signer(privKey);
GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer);

String requestUrl = "https://www.googleapis.com/oauth2/v3/token";

String header = oauthHelper.getAuthorizationHeader(requestUrl, "POST", oauthParameters);

String payload = "grant_type=urn:ietf:params:oauth:grant-type:migration:oauth1&client_id="+com.app.framework.utils.OAuthHelper.OAUTH2_CLIENT_ID+"&client_secret="+com.app.framework.utils.OAuthHelper.OAUTH2_CLIENT_SECRET;

HttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost(requestUrl);
httpPost.addHeader("Authorization", header);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(new ByteArrayEntity(payload.getBytes()));
String response = httpClient.execute(httpPost, new BasicResponseHandler());

Solution

  • Your request is failing signature verification. Please check out the responses to this related question for detailed instructions on how to construct the base string for your request and sign it.

    Hope that helps!