简体   繁体   English

Gmail api 出现错误 400“invalid_grant”

[英]Gmail api thows an error 400 "invalid_grant"

I generated the refresh token using google oauth playground.我使用 google oauth playground 生成了刷新令牌。 I used this refresh token to generate the new access token on my java code below.我使用此刷新令牌在下面的 java 代码中生成新的访问令牌。 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." 4 天前它工作正常。但现在我看到以下错误,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.如果我从 oauth2 游乐场生成新的刷新令牌并替换我的 credentials.json 上的现有令牌,它工作正常。但如果我几天没有执行代码,同样的错误。我不确定我是什么失踪。 My java code我的java代码

 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.如果 Java 应用程序长时间未连接到 Gmail 服务器,则 API 服务将被禁止。 You can set it again by visiting the Google Account settings and turning it on again.您可以通过访问 Google 帐户设置并重新打开它来重新设置它。
You need to switch 'ON' allow less secure apps to connect;您需要打开“开启”以允许不太安全的应用程序连接; which can be found here可以在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gmail API服务器访问“ invalid_grant” iOS? - Gmail API server access “invalid_grant” iOS? 在bigquery中插入行时,com.google.aaaal 400 Bad Request {“error”:“invalid_grant”} - com.google.a.a.a.a.l 400 Bad Request { “error” : “invalid_grant” } while inserting row in bigquery 新 Apple 登录不断抛出错误 HTTP 400 Invalid_grant - New Apple Sign in keeps throwing Error HTTP 400 Invalid_grant 谷歌 API oauth invalid_grant - Google API oauth invalid_grant 如何解决 google contact api 中的 { "error": "invalid_grant", "error_description": "Bad Request" } 问题 - How to resolve { "error" : "invalid_grant", "error_description" : "Bad Request" } issue in google contact api 通过Google API访问电子表格invalid_grant - Access spreadsheet via Google API invalid_grant DEVs GlassFish中的Google API异常“ invalid_grant”,而不是本地的 - Google API Exception “invalid_grant” in DEVs GlassFish but not in Local &quot;error&quot;: &quot;invalid_grant&quot;, &quot;error_description&quot;: &quot;错误的凭据&quot; - "error": "invalid_grant", "error_description": "Bad credentials" Google Cloud Storage 和服务帐户的 invalid_grant 错误 - invalid_grant error with Google Cloud Storage and Service Account OAuth令牌invalid_grant - OAuth token invalid_grant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM