开发者

Struts 2 and a simple Facebook API

开发者 https://www.devze.com 2023-02-12 11:44 出处:网络
I am trying to make a webapp in java that is capable of posting to a users facebook status, and I am having some problems with the authorization process.Basically, I have a struts2 action that redirec

I am trying to make a webapp in java that is capable of posting to a users facebook status, and I am having some problems with the authorization process. Basically, I have a struts2 action that redirects the user to the facebook login page, which then redirects back to me with a "code". I then use this code to access another facebook UR开发者_运维技巧L (to trade it for an access token).

The problem is, and I'm likely just missing something simple, that this second facebook url doesn't redirect to an action, it instead just returns a page with the access token on it. So, my question is, how would I access that token to put into my database, preferably without showing the access token to the end user?

TLDR; Any idea how I could call a request from an action in struts2 to an external url and parse the response without showing it to the user? Thanks!

Cheers, Lukas Rezek


So basically you would want 3 pieces:

  1. Interceptor
  2. authRequest action
  3. authResponse action

So the flow is:

  1. Interceptor would check if current user has auth token, if not - it will redirect to authRequest action
  2. authRequest action will fill up your app details - create url graph.facebook.com/oauth/authorize?client_id=xxx&redirect_uri=yyy etc Please note that redirect_uri is exactly is authResponse action. so after user authorizes your app - browser willo redirect to authResponse action
  3. authResponse action is the plave where you retreive the token, save it to DB and probably set it to session. after you done with that - you are done with login and you can redirect to your application home action


To access an external web site within a struts action you have two choices. Create a socket and connect to the external site and send your request via the socket.outputstream and parse the reponse via the socket.inputStream or use a browser emulator such as Apache HttpComponents or HTMLUnit.

I wouldn't let the user of my web-app anywhere near the Facebook site - I would have my own page that asked for the FB login and would handle the interaction with the facebook site using one of the two methods above. I would favour using HTMLUnit as that is designed to look like a browser to the remote site.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号