Java 来自授权代码交换的\u授权响应无效

Java 来自授权代码交换的\u授权响应无效,java,google-api,google-oauth,google-contacts-api,Java,Google Api,Google Oauth,Google Contacts Api,我正在尝试通过GoogleContactsAPI验证我的应用程序。我已经完成了Oauth2流程的第一步,并且有一个授权代码。我试图将此代码交换为访问令牌和刷新令牌,但当我尝试从googleapis.com/oauth2/v4/token receive with获取令牌时 响应:“无效的授权”“错误的请求”错误400 我的代码 try { Map<String,Object> params = new LinkedHashMap<>

我正在尝试通过GoogleContactsAPI验证我的应用程序。我已经完成了Oauth2流程的第一步,并且有一个授权代码。我试图将此代码交换为访问令牌和刷新令牌,但当我尝试从googleapis.com/oauth2/v4/token receive with获取令牌时

响应:“无效的授权”“错误的请求”错误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;
试试看
{
Map params=新建LinkedHashMap();
参数put(“授权类型”、“授权代码”);
参数put(“代码”,authCode);
参数put(“客户id”,客户id);
参数put(“客户秘密”,客户身份秘密);
参数put(“重定向uri”http://localhost:8080/conob/api2/contatos/insert");
StringBuilder postData=新建StringBuilder();
对于(Map.Entry参数:params.entrySet())
{
如果(postData.length()!=0){
postData.append('&');
}
append(URLEncoder.encode(param.getKey(),“UTF-8”);
postData.append('=');
append(URLEncoder.encode(String.valueOf(param.getValue()),“UTF-8”);
}
字节[]postDataBytes=postData.toString().getBytes(“UTF-8”);
URL=新URL(“https://www.googleapis.com/oauth2/v4/token");
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod(“POST”);
con.设置输出(真);
con.setUseCaches(假);
con.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
con.setRequestProperty(“字符集”、“utf-8”);
con.setRequestProperty(“内容长度”,postData.toString().Length()+”);
con.getOutputStream().write(postDataBytes);
BufferedReader reader=null;
试一试{
reader=newbufferedReader(newInputStreamReader(con.getInputStream());
StringBuffer=新的StringBuffer();
for(String line=reader.readLine();line!=null;line=reader.readLine()){
buffer.append(行);
}
JSONObject json=新的JSONObject(buffer.toString());
String accessToken=json.getString(“访问令牌”);
返回accessToken;
}捕获(例外e){
reader=新的BufferedReader(新的InputStreamReader(con.getErrorStream());
StringBuffer=新的StringBuffer();
for(String line=reader.readLine();line!=null;line=reader.readLine()){
buffer.append(行);
}
System.out.println(buffer.toString());
System.out.println(例如toString());
}
}
捕获(例外情况除外)
{
例如printStackTrace();
}
返回null;
参数输出:

授权类型=授权\u代码和代码=授权\u代码和客户端\u id=客户端\u id和客户端\u机密=客户端\u机密和重定向\u uri=http%3A%2F%2Flocalhost%3A8080%2Fconob%2Fapi2%2Fcontatos%2Finsert

我在很多论坛上搜索了好几个小时,但没有找到解决问题的方法

基本上,我的应用程序需要在公司内部网的谷歌账户上插入新联系人

我的问题是什么样的回答是“无效”?

良好的代码和感谢从现在起

在规范中,
“invalid_grant”
是一种与无效/过期/撤销令牌(auth grant或refresh令牌)相关的所有错误响应的总括

常见原因

  • 用户已主动撤销对我们应用程序的访问权限
  • 用户已重置/恢复其Google密码 2015年12月,谷歌改变了他们的默认行为,以便非谷歌应用程序用户的密码重置将自动撤销所有用户的应用程序刷新令牌。这并不是所有的API都是如此,联系人不是其中之一,但我想我还是会注意到这一点
  • 除此之外,还有无数其他可能引发错误的潜在原因:

  • 服务器时钟/时间不同步
  • 未授权脱机访问
  • 被谷歌扼杀
  • 使用过期的刷新令牌
  • 使用过期的授权代码
  • 用户已停用6个月
  • 刷新令牌不正确或无效
  • 使用服务人员电子邮件而不是客户端ID
  • 短时间内访问令牌过多
  • 客户端SDK可能已过时
  • 终点

    我意识到发现文档中说的是
    googleapis.com/oauth2/v4/token
    ,但由于某些原因,这个端点并不总是有效,请尝试使用
    accounts.google.com/o/oauth2/token

    重定向\u uri\u不匹配

    表示您正在发送的重定向uri与您的请求一起
    http://localhost:8080/conob/api2/contatos/insert
    不是您在Google开发者控制台中添加的。您需要返回到GoogleDeveloper控制台并添加这个重定向uri


    注意,您可能想考虑使用谷歌人API,它是一个更容易的API,与旧的谷歌联系人API。

    在这种情况下,什么是“坏请求”,抱歉:/任何原因,您没有使用尝试,并张贴它不@博格的问题和问题,我很清楚。然而,由于你在理解上有问题,我编辑了他的问题。现在清楚了吗?欢迎收看stackoverflow,Nicolas!这个问题现在可读性更强了,非常感谢!多尔姆托谢谢你的帮助。我尝试更改帖子,现在错误是:“错误”:“重定向uri不匹配”,“错误描述”:“错误请求”我正在使用我的帐户测试api,因此服务器/时钟时间正确,在同意屏幕中访问类型为脱机,身份验证