You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Josh Kamau <jo...@gmail.com> on 2011/05/02 16:34:46 UTC

wicket facebook application

I am doing a facebook application in wicket. To get facebook authentication
token, I have written a wicket filter as below:



package com.mycompany.myapp.config;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.visural.common.StringUtil;

public class FBOAuth implements Filter {
    public void init(FilterConfig fc) throws ServletException {
    }

    public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain
fc)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) sr;
        HttpServletResponse res = (HttpServletResponse) sr1;
        res.sendRedirect(FaceBook.getLoginRedirectURL());
        String code = sr.getParameter("code");
        if (StringUtil.isNotBlankStr(code)) {
            String authURL = FaceBook.getAuthURL(code);
            URL url = new URL(authURL);
            try {
                String result = readURL(url);
                String accessToken = null;
                Integer expires = null;
                String[] pairs = result.split("&");
                for (String pair : pairs) {
                    String[] kv = pair.split("=");
                    if (kv.length != 2) {
                        throw new RuntimeException("Unexpected auth
response");
                    } else {
                        if (kv[0].equals("access_token")) {
                            accessToken = kv[1];
                        }
                        if (kv[0].equals("expires")) {
                            expires = Integer.valueOf(kv[1]);
                        }
                    }
                }
                if (accessToken != null && expires != null)
{
                    HttpSession ses = req.getSession();
                    ses.setAttribute("authToken", accessToken);


                } else {
                    throw new RuntimeException(
                            "Access token and expires not found");
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
    private String readURL(URL url) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream is = url.openStream();
        int r;
        while ((r = is.read()) != -1) {
            baos.write(r);
        }
        return new String(baos.toByteArray());
    }

    public void destroy() {
    }
}





In the doFilter method, I have done all the redirects required and gotten
the authentication token(accesstoken). At this point, I store the value as
an attribute to the httpSession which i have gotten by
ServleteRequest.getSession();

I want to acess this token in a wicket page class so that I use RestFB to
create a FaceBook Client etc. This is what I have done in the wicket page
class:

HttpSession session = null;

    if (RequestCycle.get()!= null)
        session = ((WebRequest) RequestCycle.get().getRequest())
                .getHttpServletRequest().getSession();

    if (session != null) {

        System.out.print("################# AuthToken: "
                + session.getAttribute("authToken"));

    }


However, this is the output I get and I am totally stuck:

################# AuthToken: null


Where am i going wrong?

Re: wicket facebook application

Posted by Josh Kamau <jo...@gmail.com>.
Thanks Peter. I hadnt thought about that.  I will do it that way.

Josh.

On Mon, May 2, 2011 at 7:10 PM, Peter Karich <pe...@yahoo.de> wrote:

>  Why not doing this directly the wicket-way with PageParameters and your
> own Session?
>
> FBPage(PageParameters params) {
>   // do something with: params.getString("xy");
>
>   // construct a new MySession object within the WicketApp.init method:
>   // @Override Session newSession(Request request, Response response) {
>   //       return new MySession(request); }
>
>   // No need for complicated stuff just call 'getSession()' and get or
> set the token:
>   MySession session = (MySession)getSession();
>   // session.getToken();
> }
>
> I've done similar things in jetwick for twitter authentication.
>
> Regards,
> Peter.
>
>
>
> --
> http://jetwick.com open twitter search
>
>
> > I am doing a facebook application in wicket. To get facebook
> authentication
> > token, I have written a wicket filter as below:
> >
> >
> >
> > package com.mycompany.myapp.config;
> >
> > import java.io.ByteArrayOutputStream;
> > import java.io.IOException;
> > import java.io.InputStream;
> > import java.net.URL;
> >
> > import javax.servlet.Filter;
> > import javax.servlet.FilterChain;
> > import javax.servlet.FilterConfig;
> > import javax.servlet.ServletException;
> > import javax.servlet.ServletRequest;
> > import javax.servlet.ServletResponse;
> > import javax.servlet.http.HttpServletRequest;
> > import javax.servlet.http.HttpServletResponse;
> > import javax.servlet.http.HttpSession;
> >
> > import com.visural.common.StringUtil;
> >
> > public class FBOAuth implements Filter {
> >     public void init(FilterConfig fc) throws ServletException {
> >     }
> >
> >     public void doFilter(ServletRequest sr, ServletResponse sr1,
> FilterChain
> > fc)
> >             throws IOException, ServletException {
> >         HttpServletRequest req = (HttpServletRequest) sr;
> >         HttpServletResponse res = (HttpServletResponse) sr1;
> >         res.sendRedirect(FaceBook.getLoginRedirectURL());
> >         String code = sr.getParameter("code");
> >         if (StringUtil.isNotBlankStr(code)) {
> >             String authURL = FaceBook.getAuthURL(code);
> >             URL url = new URL(authURL);
> >             try {
> >                 String result = readURL(url);
> >                 String accessToken = null;
> >                 Integer expires = null;
> >                 String[] pairs = result.split("&");
> >                 for (String pair : pairs) {
> >                     String[] kv = pair.split("=");
> >                     if (kv.length != 2) {
> >                         throw new RuntimeException("Unexpected auth
> > response");
> >                     } else {
> >                         if (kv[0].equals("access_token")) {
> >                             accessToken = kv[1];
> >                         }
> >                         if (kv[0].equals("expires")) {
> >                             expires = Integer.valueOf(kv[1]);
> >                         }
> >                     }
> >                 }
> >                 if (accessToken != null && expires != null)
> > {
> >                     HttpSession ses = req.getSession();
> >                     ses.setAttribute("authToken", accessToken);
> >
> >
> >                 } else {
> >                     throw new RuntimeException(
> >                             "Access token and expires not found");
> >                 }
> >             } catch (IOException e) {
> >                 throw new RuntimeException(e);
> >             }
> >
> >         }
> >     }
> >     private String readURL(URL url) throws IOException {
> >         ByteArrayOutputStream baos = new ByteArrayOutputStream();
> >         InputStream is = url.openStream();
> >         int r;
> >         while ((r = is.read()) != -1) {
> >             baos.write(r);
> >         }
> >         return new String(baos.toByteArray());
> >     }
> >
> >     public void destroy() {
> >     }
> > }
> >
> >
> >
> >
> >
> > In the doFilter method, I have done all the redirects required and gotten
> > the authentication token(accesstoken). At this point, I store the value
> as
> > an attribute to the httpSession which i have gotten by
> > ServleteRequest.getSession();
> >
> > I want to acess this token in a wicket page class so that I use RestFB to
> > create a FaceBook Client etc. This is what I have done in the wicket page
> > class:
> >
> > HttpSession session = null;
> >
> >     if (RequestCycle.get()!= null)
> >         session = ((WebRequest) RequestCycle.get().getRequest())
> >                 .getHttpServletRequest().getSession();
> >
> >     if (session != null) {
> >
> >         System.out.print("################# AuthToken: "
> >                 + session.getAttribute("authToken"));
> >
> >     }
> >
> >
> > However, this is the output I get and I am totally stuck:
> >
> > ################# AuthToken: null
> >
> >
> > Where am i going wrong?
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: wicket facebook application

Posted by Peter Karich <pe...@yahoo.de>.
 Why not doing this directly the wicket-way with PageParameters and your
own Session?

FBPage(PageParameters params) {
   // do something with: params.getString("xy");

   // construct a new MySession object within the WicketApp.init method:
   // @Override Session newSession(Request request, Response response) {
   //       return new MySession(request); }

   // No need for complicated stuff just call 'getSession()' and get or
set the token:
   MySession session = (MySession)getSession();
   // session.getToken();
}

I've done similar things in jetwick for twitter authentication.

Regards,
Peter.



-- 
http://jetwick.com open twitter search


> I am doing a facebook application in wicket. To get facebook authentication
> token, I have written a wicket filter as below:
>
>
>
> package com.mycompany.myapp.config;
>
> import java.io.ByteArrayOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.net.URL;
>
> import javax.servlet.Filter;
> import javax.servlet.FilterChain;
> import javax.servlet.FilterConfig;
> import javax.servlet.ServletException;
> import javax.servlet.ServletRequest;
> import javax.servlet.ServletResponse;
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
> import javax.servlet.http.HttpSession;
>
> import com.visural.common.StringUtil;
>
> public class FBOAuth implements Filter {
>     public void init(FilterConfig fc) throws ServletException {
>     }
>
>     public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain
> fc)
>             throws IOException, ServletException {
>         HttpServletRequest req = (HttpServletRequest) sr;
>         HttpServletResponse res = (HttpServletResponse) sr1;
>         res.sendRedirect(FaceBook.getLoginRedirectURL());
>         String code = sr.getParameter("code");
>         if (StringUtil.isNotBlankStr(code)) {
>             String authURL = FaceBook.getAuthURL(code);
>             URL url = new URL(authURL);
>             try {
>                 String result = readURL(url);
>                 String accessToken = null;
>                 Integer expires = null;
>                 String[] pairs = result.split("&");
>                 for (String pair : pairs) {
>                     String[] kv = pair.split("=");
>                     if (kv.length != 2) {
>                         throw new RuntimeException("Unexpected auth
> response");
>                     } else {
>                         if (kv[0].equals("access_token")) {
>                             accessToken = kv[1];
>                         }
>                         if (kv[0].equals("expires")) {
>                             expires = Integer.valueOf(kv[1]);
>                         }
>                     }
>                 }
>                 if (accessToken != null && expires != null)
> {
>                     HttpSession ses = req.getSession();
>                     ses.setAttribute("authToken", accessToken);
>
>
>                 } else {
>                     throw new RuntimeException(
>                             "Access token and expires not found");
>                 }
>             } catch (IOException e) {
>                 throw new RuntimeException(e);
>             }
>
>         }
>     }
>     private String readURL(URL url) throws IOException {
>         ByteArrayOutputStream baos = new ByteArrayOutputStream();
>         InputStream is = url.openStream();
>         int r;
>         while ((r = is.read()) != -1) {
>             baos.write(r);
>         }
>         return new String(baos.toByteArray());
>     }
>
>     public void destroy() {
>     }
> }
>
>
>
>
>
> In the doFilter method, I have done all the redirects required and gotten
> the authentication token(accesstoken). At this point, I store the value as
> an attribute to the httpSession which i have gotten by
> ServleteRequest.getSession();
>
> I want to acess this token in a wicket page class so that I use RestFB to
> create a FaceBook Client etc. This is what I have done in the wicket page
> class:
>
> HttpSession session = null;
>
>     if (RequestCycle.get()!= null)
>         session = ((WebRequest) RequestCycle.get().getRequest())
>                 .getHttpServletRequest().getSession();
>
>     if (session != null) {
>
>         System.out.print("################# AuthToken: "
>                 + session.getAttribute("authToken"));
>
>     }
>
>
> However, this is the output I get and I am totally stuck:
>
> ################# AuthToken: null
>
>
> Where am i going wrong?
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: wicket facebook application

Posted by mnadeem <co...@gmail.com>.
Look into this one

https://reachmnadeem.wordpress.com/2012/04/09/wicket-facebook/

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/wicket-facebook-application-tp3490139p4546617.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org