2012-11-02 88 views
1

我正在使用SCRIBE庫使我的應用程序中的Facebook連接登錄。我的問題是,登錄並獲取access_token後,每次我點擊我登錄按鈕時,我沒有授權登錄,因爲我登錄Facebook並且access_token仍然處於活動狀態。有沒有辦法強制Facebook註銷或每次都要求我提供新的access_token,使用cookie或使用connect.facebook.net/en_US/all.js或在某個點重定向它是個好主意到https://www.facebook.com/logout.php?access_token=appId&confirm=1&next=http://localhost:8080/。我知道關於這個話題有很多問題,但所有這些和建議的解決方案都讓我困惑。這是處理Facebook響應的託管bean中的我的post構造方法。 (){ FacesContext context = FacesContext.getCurrentInstance();}} HttpServletRequest req =(HttpServletRequest)context .getExternalContext()。getRequest();如何註銷用戶使用Facebook連接在java與scribe庫的用戶?

responseCode = req.getParameter("code"); 
    System.out.println("The code is: "+responseCode); 


    //facebook data 
    final String PROTECTED_RESOURCE_URL = "https://graph.facebook.com/me"; 
    final Token EMPTY_TOKEN = null; 
    String apiKey = "MY_API_KEY"; 
    String apiSecret = "MY_API_SECRET"; 
    String callbackUrl="the-redirect_page_in_my_application"; 

    OAuthService service = new ServiceBuilder().provider(FacebookApi.class) 
      .apiKey(apiKey).apiSecret(apiSecret) 
      //.scope(SCOPE) 
      .callback(callbackUrl).build(); 

    //get authorization Url 
    String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN); 

    Verifier v=new Verifier(responseCode); 
    Token accessToken = service.getAccessToken(EMPTY_TOKEN, v); 


    // Now let's go and ask for a protected resource! 

    OAuthRequest request = new OAuthRequest(Verb.GET, 
      PROTECTED_RESOURCE_URL); 
    service.signRequest(accessToken, request); 
    Response response = request.send(); 


    Gson gson = new GsonBuilder().create(); 
    FacebookUser faceUser = gson.fromJson(response.getBody(), FacebookUser.class); 

    setUserName(faceUser.getName()); 
    setUserFacebook(faceUser.getUsername()); 
    setGender(faceUser.getGender()); 

    FacesContext.getCurrentInstance().responseComplete(); 
    } 

回答

0

你有多種選擇:

  1. Tie your Logout button to the Facebook Javascript SDK's FB.logout() function.這有記錄的 用戶從Facebook.com的還的可能不需要的副作用(因爲你的應用程序可能已經 他們負責記錄進入它的第一個地方)
  2. Use the /me/permissions Graph API endpoint with an HTTP DELETE and a user access token to revoke permissions for that user for your app.這裏的缺點是,用戶將不得不重新認證,當他們下次登錄到您的應用程序(並再次看到登錄對話框)。
  3. 存儲一個會話變量,告訴您某人已經登錄。當他們註銷時,刪除該變量並存儲另一個表示他們已手動註銷的會話變量。然後,如果第二個變量存在,請不要再次自動重新驗證它們,直到他們點擊應用程序中的登錄按鈕。
相關問題