Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:36:50+00:00 2026-05-25T22:36:50+00:00

I have a web page made in GWT. There I use all the login

  • 0

I have a web page made in GWT. There I use all the login facebook stuff with a manipulated gwtfb library, all works fine. After migrating to oauth 2.0 now the cookie sent to the server has changed to a encrypted one.

I want to get a java example code that implements in the server the same than the old one:

  • I need to validate the call like I did before using the cookie md5 trick to know if the call has been made by my client page.
  • Get data from that cookie: I need the facebook user.

If possible not calling FB, just using the cookie data.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:36:51+00:00Added an answer on May 25, 2026 at 10:36 pm

    Well, although I have a few good answers I answer myself with what I have written in my blog:
    http://pablocastilla.wordpress.com/2011/09/25/how-to-implement-oauth-f/

    Now the cookie has changed a lot: it is encrypted, doesn’t have the accesstoken and its content format has changed a lot. Here you have a few links talking about it:

    http://developers.facebook.com/docs/authentication/signed_request/

    http://developers.facebook.com/docs/authentication/

    http://blog.sociablelabs.com/2011/09/19/server-side-changes-facebook-oauth-2-0-upgrade/

    So to validate the cookie, get the user from it and get the access token you could use this code:

    public class FaceBookSecurity {
    
    // return the fb user in the cookie.
    public static String getFBUserFromCookie(HttpServletRequest request)
            throws Exception {
        Cookie fbCookie = getFBCookie(request);
    
        if (fbCookie == null)
            return null;
    
        // gets cookie value
        String fbCookieValue = fbCookie.getValue();
    
        // splits it.
        String[] stringArgs = fbCookieValue.split("\\.");
        String encodedPayload = stringArgs[1];
    
        String payload = base64UrlDecode(encodedPayload);
    
        // gets the js object from the cookie
        JsonObject data = new JsonObject(payload);
    
        return data.getString("user_id");
    
    }
    
    public static boolean ValidateFBCookie(HttpServletRequest request)
            throws Exception {
    
        Cookie fbCookie = getFBCookie(request);
    
        if (fbCookie == null)
            throw new NotLoggedInFacebookException();
    
        // gets cookie information
        String fbCookieValue = fbCookie.getValue();
    
        String[] stringArgs = fbCookieValue.split("\\.");
        String encodedSignature = stringArgs[0];
        String encodedPayload = stringArgs[1];
    
        //decode
        String sig = base64UrlDecode(encodedSignature);
        String payload = base64UrlDecode(encodedPayload);
    
        // gets the js object from the cookie
        JsonObject data = new JsonObject(payload);
    
        if (!data.getString("algorithm").Equals("HMAC-SHA256")) {
            return false;
        }
    
        SecretKey key = new SecretKeySpec(
                ApplicationServerConstants.FacebookSecretKey.getBytes(),
                "hmacSHA256");
    
        Mac hmacSha256 = Mac.getInstance("hmacSHA256");
        hmacSha256.init(key);
        // decode the info.
        byte[] mac = hmacSha256.doFinal(encodedPayload.getBytes());
    
        String expectedSig = new String(mac);
    
        // compare if the spected sig is the same than in the cookie.
        return expectedSig.equals(sig);
    
    }
    
    public static String getFBAccessToken(HttpServletRequest request)
            throws Exception {
        Cookie fbCookie = getFBCookie(request);
    
        String fbCookieValue = fbCookie.getValue();
    
        String[] stringArgs = fbCookieValue.split("\\.");
        String encodedPayload = stringArgs[1];
    
        String payload = base64UrlDecode(encodedPayload);
    
        // gets the js object from the cookie
        JsonObject data = new JsonObject(payload);
    
        String authUrl = getAuthURL(data.getString("code"));
        URL url = new URL(authUrl);
        URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(),
                url.getQuery(), null);
        String result = readURL(uri.toURL());
    
        String[] resultSplited = result.split("&");
    
        return resultSplited[0].split("=")[1];
    
    }
    
    // creates the url for calling to oauth.
    public static String getAuthURL(String authCode) {
        String url = "https://graph.facebook.com/oauth/access_token?client_id="
                + ApplicationConstants.FacebookApiKey
                + "&redirect_uri=&client_secret="
                + ApplicationServerConstants.FacebookSecretKey + "&code="
                + authCode;
    
        return url;
    }
    
    // reads the url.
    private static String readURL(URL url) throws IOException {
    
        InputStream is = url.openStream();
    
        InputStreamReader inStreamReader = new InputStreamReader(is);
        BufferedReader reader = new BufferedReader(inStreamReader);
    
        String s = "";
    
        int r;
        while ((r = is.read()) != -1) {
            s = reader.readLine();
        }
    
        reader.close();
        return s;
    }
    
    private static String base64UrlDecode(String input) {
        String result = null;
        Base64 decoder = new Base64(true);
        byte[] decodedBytes = decoder.decode(input);
        result = new String(decodedBytes);
        return result;
    }
    
        private static Cookie getFBCookie(HttpServletRequest request) 
        {
            Cookie[] cookies = request.getCookies();
    
            if (cookies == null)
                return null;
    
            Cookie fbCookie = null;
    
            for (Cookie c : cookies) {
                if (c.getName().equals(
                    "fbsr_" + ApplicationServerConstants.FacebookApiKey)) {
                    fbCookie = c;
                }
            }
            return fbCookie;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a web application that uses master page for Login & Logout
I have made a Web page using jquery and php where all files are
I have made a simple web-page including a couple of static pages, a css
I have web page in PHP which displays all records in a table. I
Hello fellow developers. I have previously made a facebook fan page with an iframe
I have a problem with printing a chart. I made a web page contains
I have a ASP.NET (C#) web page which utilizes a VB class library. The
I have a web page containing a login form which loads via HTTP, but
I have a web page for which I made two css files, one for
I have made a simple web page using jQuery. When opening it on another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.