Reputation: 1
I generated the refresh token using google oauth playground. I used this refresh token to generate the new access token on my java code below. It was working fine 4 days ago.But now I am seeing the below error, POST https://oauth2.googleapis.com/token { "error": "invalid_grant", "error_description": "Token has been expired or revoked." } if i generate the new refresh token from oauth2 playground and replace the existing one on my credentials.json,it's working fine.But if I didn't execute the code for couple of days,same error.I am not sure what I am missing. My java code
private String getAccessToken() {
try {
credentials.put("grant_type", "refresh_token");
credentials.put("client_id", credential.get("client_id"));
credentials.put("client_secret", credential.get("client_secret"));
credentials.put("refresh_token", credential.get("refresh_token"));
credentials.put("project_id", credential.get("project_id"));
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : credentials.entrySet()) {
if (postData.length() != 0) {
postData.append('&');
}
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
URL url = new URL("https://accounts.google.com/o/oauth2/token");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.getOutputStream().write(postDataBytes);
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuffer buffer = new StringBuffer();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
buffer.append(line);
}
JSONObject json = new JSONObject(buffer.toString());
String accessToken = json.getString("access_token");
return accessToken;
} catch (Exception ex) {
log.error("Error on generating access token:"+ExceptionUtils.getFullStackTrace(ex));
}
return null;
}
Upvotes: 0
Views: 272
Reputation: 151
The API service is barred if the Java application is not connected to the Gmail server for long. You can set it again by visiting the Google Account settings and turning it on again.
You need to switch 'ON' allow less secure apps to connect; which can be found here
Upvotes: 0