2012-12-11 77 views
0

這個問題是在跟進我剛纔的問題谷歌的OAuth Java客戶端得到一個訪問令牌失敗,「400錯誤的請求{」錯誤「:」 INVALID_REQUEST「}」

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

我跳入水中到JAVA API來解決在谷歌的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> 

本頁面我提出了一個簡單的鏈接「連接谷歌帳戶」,這給我帶來了成功,以谷歌的網頁,我不得不「允許」我應用訪問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" 
} 

我會很感激在這一個任何幫助。 謝謝

+0

你能發佈要求你發送到谷歌?一些提琴手捕獲 – Vlad

回答

2

在訪問令牌端點進行POST時,所需的參數不應該被url編碼(至少是谷歌API)。

這裏,redirect_uri參數被編碼,因此它與客戶端註冊時使用的參數不同,導致invalid_request

根據上述JSP代碼,如果redirect_uri參數是固定的,則令牌服務器響應可能導致invalid_grant,因爲code也正在編碼。通常情況下,谷歌發佈授權碼,這是不友好的網址。

刪除上面coderedirect_uri參數的編碼應該導致包含訪問令牌的服務器響應。

+1

你是對的,沒有編碼的URI部分解決了問題。現在我得到「錯誤」:「redirect_uri_mismatch」!我確信http:// localhost:8080/BroadCastr/step3.jsp是在Google API Access Console中配置的 – Sap

+0

那麼你是對的,我通過重新回到step2.jsp來修復最後一個問題 – Sap

相關問題