简体   繁体   中英

Gmail api thows an error 400 "invalid_grant"

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;
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM