【问题标题】:Google oauth java client to get an access token fails with “400 Bad Request { ”error“ : ”invalid_request“ }”Google oauth java 客户端获取访问令牌失败并显示“400 Bad Request { ”error“ : ”invalid_request“ }”
【发布时间】:2012-12-12 04:31:00
【问题描述】:

这个问题是我上一个问题的后续问题

Google oauth java client to get an access token fails with "400 Bad Request { "error" : "invalid_request" }"

我深入研究 JAVA API 以解决在 google 的 oAuth API 中为 authToken 交换代码的问题,但找不到答案。因此,我选择了一条非常简单的路线。

我创建了以下 JSP

index.jsp

<%@page import="java.net.URLEncoder"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <a href="https://accounts.google.com/o/oauth2/auth?
scope=https://gdata.youtube.com/&
redirect_uri=<%=URLEncoder.encode("http://localhost:8080/BroadCastr/step2.jsp","UTF-8")%>&
response_type=code&
client_id=X985XXXXXXXX.apps.googleusercontent.com&approval_prompt=force">Connect google account</a>
    </body>
</html>

此页面向我提供了一个简单的链接“连接 google 帐户”,该链接成功将我带到了 google 页面,我必须“允许”我的应用代表我访问 youtube

在step2.jsp中

<%@page import="java.net.URLEncoder"%>
<%@page import="java.util.Iterator"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <form id="frm" method="post" action="https://accounts.google.com/o/oauth2/token" enctype="application/x-www-form-urlencoded">
            <input type="hidden" name="code" value="<%=URLEncoder.encode(request.getParameter("code"),"UTF-8")%>"/>
            <input type="hidden" name="client_id" value="XXXXXXXXXXX.apps.googleusercontent.com"/>
            <input type="hidden" name="client_secret" value="XXXXxxxxXXXXXX"/>
            <input type="hidden" name="redirect_uri" value="<%=URLEncoder.encode("http://localhost:8080/BroadCastr/step3.jsp","UTF-8")%>"/>
            <input type="hidden" name="grant_type" value="authorization_code"/>
            <input type="hidden" name="scope" value=""/>
        </form>
    </body>
</html>
<script>
    document.getElementById("frm").submit();
</script>

但最后 step2.jsp 将自己提交到谷歌的服务器,我得到的只是遵循无用的 JSON

{
"error": "invalid_request"
}

我将非常感谢对此提供的任何帮助。 谢谢

【问题讨论】:

  • 你能发布你发送给谷歌的请求吗?一些提琴手捕获

标签: java jsp rest google-oauth


【解决方案1】:

在向访问令牌端点发送 POST 时,所需参数不应进行 url 编码(至少对 google API)。

这里,redirect_uri 参数被编码,因此,它与客户端注册时使用的参数不同,导致invalid_request

根据上面的 JSP 代码,如果 redirect_uri 参数是固定的,令牌服务器响应可能会导致 invalid_grant,因为 code 也在被编码。通常,谷歌会发出一个授权码,这不是 url 友好的。

删除上面coderedirect_uri 参数的编码应该会导致服务器响应包含访问令牌。

【讨论】:

  • 你是对的,不编码 URI 部分解决了这个问题。现在我得到“错误”:“redirect_uri_mismatch”!我确保在 Google API 访问控制台中配置了 localhost:8080/BroadCastr/step3.jsp
  • 你是对的,我通过重定向到 step2.jsp 解决了最后一个问题
猜你喜欢
  • 2012-11-18
  • 2021-09-12
  • 1970-01-01
  • 2015-05-10
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
  • 1970-01-01
相关资源
最近更新 更多