【问题标题】:invalid_grant response from exchange of authorization code来自授权码交换的 invalid_grant 响应
【发布时间】:2019-04-04 15:31:35
【问题描述】:

我正在尝试向 Google 联系人 API 验证我的应用程序。我已经完成了 Oauth2 流程的第一步,并获得了授权码。我正在尝试将此代码交换为访问令牌和刷新令牌,但是当我尝试从 googleapis.com/oauth2/v4/token 获取令牌时,使用

响应:“invalid_grant”“错误请求”错误 400。

我的代码

try
        {
            Map<String,Object> params = new LinkedHashMap<>();
            params.put("grant_type","authorization_code");
            params.put("code", authCode);
            params.put("client_id",CLIENTE_ID);
            params.put("client_secret",CLIENTE_ID_SECRETO);
            params.put("redirect_uri","http://localhost:8080/conob/api2/contatos/insert");

            StringBuilder postData = new StringBuilder();
            for(Map.Entry<String,Object> param : params.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://www.googleapis.com/oauth2/v4/token");
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setUseCaches(false);
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setRequestProperty("charset", "utf-8");
            con.setRequestProperty("Content-Length", postData.toString().length() + "");
            con.getOutputStream().write(postDataBytes);


            BufferedReader reader = null;
            try {
                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 e) {
                reader = new BufferedReader(new InputStreamReader(con.getErrorStream()));

                StringBuffer buffer = new StringBuffer();

                for (String line = reader.readLine(); line != null; line = reader.readLine()){
                    buffer.append(line);
                }

                System.out.println(buffer.toString());
                System.out.println(e.toString());
            }

        }
        catch (Exception ex)
        {
            ex.printStackTrace(); 
        }
        return null;

参数输出:

grant_type=authorization_code&code=AUTHORIZATION_CODE&client_id=CLIENTE_ID&client_secret=CLIENTE_SECRET&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fconob%2Fapi2%2Fcontatos%2Finsert

我在许多论坛中搜索了很多小时,但没有找到解决我问题的方法。

基本上,我的应用需要在公司 Intranet 的 google 帐户上插入新联系人。

我的问题是“invalid_grant”的响应是什么?

好的代码,从现在开始感谢;

【问题讨论】:

标签: java google-api google-oauth google-contacts-api


【解决方案1】:

OAuth2 规范中,"invalid_grant" 是所有与无效/过期/撤销令牌(授权授权或刷新令牌)相关的错误响应的统称。

常见原因

  1. 用户主动撤销了对我们应用的访问权限
  2. 用户已重置/恢复其 Google 密码 2015 年 12 月,Google 更改了他们的默认行为,以便非 Google Apps 用户的密码重置将自动撤销所有用户的应用程序刷新令牌。并非所有 api 联系人都不是其中之一,但我想我还是会注意到这一点。

除此之外,还有无数其他可能触发错误的原因:

  1. 服务器时钟/时间不同步
  2. 未授权离线访问
  3. 受 Google 限制
  4. 使用过期的刷新令牌
  5. 使用过期的授权码。
  6. 用户已闲置 6 个月
  7. 刷新令牌不正确或无效
  8. 使用服务人员电子邮件而不是客户端 ID
  9. 短时间内访问令牌过多
  10. 客户端 SDK 可能已过时

端点

我知道发现文档说googleapis.com/oauth2/v4/token,但由于某种原因,这个端点并不总是有效,尝试使用accounts.google.com/o/oauth2/token

redirect_uri_mismatch

表示您随请求发送的重定向 uri http://localhost:8080/conob/api2/contatos/insert 不是您在 Google 开发者控制台中添加的重定向 uri。您需要返回 google 开发者控制台并添加此重定向 uri。

请注意,您可能需要考虑使用 google people api,它比旧的 google contacts api 更容易使用。

【讨论】:

  • DalmTo 感谢您的帮助。我尝试为accounts.google.com/o/oauth2/token 更改 POST,现在错误是:"error": "redirect_uri_mismatch", "error_description": "Bad Request" 我正在使用我的帐户来测试 api,所以 Server/时钟时间是正确的,在同意屏幕中,access_type 是离线的,授权码是我第一次用户重定向的响应。
  • 检查我的编辑以获取 redirect_uri_missmatch 的解决方案
  • 在 Google Cloud 上的开发控制台中,无论如何,我真的考虑使用 People API,如果有人建议解决 de Uri_missmatch 我感谢。
  • 解决重定向不匹配的方法是将重定向uri添加到客户端console.developers.google.com这是唯一的方法。您只需编辑您的客户端添加正确的重定向 uri,它就会起作用
  • 请参阅下面授予我的 Cliente_Secret.json 的重定向 uri:["http://localhost:8080/conob/api2/contatos/token", "http://localhost:8080/conob/api2/contatos/insert", "http://localhost:8080/conob/api2/contatos/insert/", "http://localhost:8080/conob/api2/contatos/token/", "http://127.0.0.1:8080/conob/api2/contatos/insert", "http://127.0.0.1:8080/conob/api2/contatos/insert/"], "javascript_origins":["http://localhost:8080"]}}
猜你喜欢
  • 2019-10-17
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2020-11-06
相关资源
最近更新 更多