2011-08-21 73 views
0

我想要一個簡單的JSP頁面中完成Facebook驗證下面這個例子:http://www.sergiy.ca/how-to-implement-facebook-oauth-2.0-app-authorization-process-in-java/Facebook的身份驗證與JSP

不幸的是,我不是在這一點上是非常全成。您的幫助將不勝感激。作爲應用程序的開發人員,我以某種方式設法接受了該應用程序,並且可以在我的應用程序列表中看到它。但是當我以其他用戶身份登錄時,我無法接受該應用。雖然重定向請求似乎已發送給FB,但並未提示用戶授予應用程序權限。任何幫助將非常感激。我的代碼:

<%@ page import="java.util.*,org.apache.commons.codec.binary.*, java.net.*, org.json.simple.*" %> 
<html> 
<body> 
<% 
String fbSecretKey = "efqec6fdedd17a64055712dcc7d81f58"; 
String fbAppId = "116041890091"; 
String fbCanvasPage = "http://apps.facebook.com/stupidgame/"; 
String fbCanvasUrl = "http://stupidgame.com:8090/stupidgame/"; 
String accessToken; 
    if(request.getParameter("signed_request") != null) { 
    //it is important to enable url-safe mode for Base64 encoder 
    Base64 base64 = new Base64(true); 
    //split request into signature and data 
    String[] signedRequest = request.getParameter("signed_request").split("\\.", 2); 
    //parse signature 
    String sig = new String(base64.decode(signedRequest[0].getBytes("UTF-8"))); 
    //parse data and convert to json object 
    JSONObject data = (JSONObject)JSONValue.parse(new String(base64.decode(signedRequest[1].getBytes("UTF-8")))); 
    //check if user authorized the app 
    if(data.get("user_id")==null || data.get("oauth_token")==null) {   
     //this is guest, create authorization url that will be passed to javascript 
     //note that redirect_uri (page the user will be forwarded to after authorization) is set to fbCanvasUrl 
     response.sendRedirect("https://www.facebook.com/dialog/oauth?client_id=" + fbAppId + 
       "&redirect_uri=" + fbCanvasUrl + "&scope=publish_stream,offline_access,email"); 
     return; 
    } 
    accessToken=data.get("oauth_token")+""; 
}else{ 
    response.sendRedirect("https://www.facebook.com/dialog/oauth?client_id=" + fbAppId + 
      "&redirect_uri=" + URLEncoder.encode(fbCanvasUrl, "UTF-8") + 
      "&scope=publish_stream,offline_access,email"); 
    return; 
} 
System.out.println("All set with accessToken:"+accessToken); 
%> 
</body> 
</html> 

回答

0

既然你的應用程序是在一個iframe「response.sendRedirect是」只運行重定向iframe和身份驗證對話框必須是整個頁面。

替換:

response.sendRedirect(...) 

有:

%><script language="JavaScript"> top.location.href = "<%=auth_url%>"; </script> <% 

或類似的東西,它應該工作。 JavaScript應該類似於PHP文檔https://developers.facebook.com/docs/authentication/

+1

除此之外,我還有其他問題。 redirect_uri缺少「www」,而畫布網址只有一個。另外我不太明白訪問令牌不是來自請求參數,而是來自請求主體。 – Hema