Reputation: 877
I have a GWT web application. I have big table with Google tokens and token secrets. I want to move to OAuth2.0 without reauthenticating the users. I have tried the following in Java tests
String url = "https://accounts.google.com/o/oauth2/token";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
String generatedString = Utils.generateRandomString(10);
String key = new Date().getTime() + "";
// add header
post.setHeader("Host", "accounts.google.com");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setHeader("Authorization", "OAuth " +
"oauth_consumer_key=\"" + CONSUMER_KEY + "\"," +
"oauth_token=\"" + token + "\"," +
"oauth_signature_method=\"HMAC-SHA1\"" +
"timestamp=\"" + key + "\"," +
"oauth_nonce=\"" + generatedString + "\"," +
"oauth_signature=\"" + calculateRFC2104HMAC(key, generatedString) + "\"," +
"oauth_signature_method=\"HMAC-SHA1\"");
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("grant_type", "urn:ietf:params:oauth:grant-type:migration:oauth1"));
urlParameters.add(new BasicNameValuePair("client_id", CONSUMER_KEY));
urlParameters.add(new BasicNameValuePair("client_secret", CONSUMER_SECRET));
urlParameters.add(new BasicNameValuePair("oauth_consumer_key", CONSUMER_KEY));
urlParameters.add(new BasicNameValuePair("oauth_consumer_secret", CONSUMER_SECRET));
urlParameters.add(new BasicNameValuePair("oauth_signature_method", "HMAC-SHA1"));
urlParameters.add(new BasicNameValuePair("oauth_timestamp", "" + new Date().getTime()));
urlParameters.add(new BasicNameValuePair("oauth_token", token));
urlParameters.add(new BasicNameValuePair("oauth_token_secret", token));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
System.out.println("Sending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
But I still can't get any adequate response. I get only:
Upvotes: 1
Views: 128
Reputation: 1724
You will need to generate a valid oauth_signature as per the google documentation
See this answer for some useful tips when generating the base string
The POST body should only contain 3 params as follows
grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Amigration%3Aoauth1&
client_id=8819981768.apps.googleusercontent.com&
client_secret=xxxxxxxxxx
Upvotes: 3