Struts 2 和简单的 Facebook API
我正在尝试用 java 制作一个能够发布到用户 facebook 状态的 web 应用程序,但我在授权过程中遇到了一些问题。基本上,我有一个 struts2 操作,它将用户重定向到 facebook 登录页面,然后使用“代码”重定向回我。然后,我使用此代码访问另一个 Facebook URL(用它换取访问令牌)。
问题是,我可能只是错过了一些简单的事情,即第二个 Facebook 网址不会重定向到操作,它只是返回一个带有访问令牌的页面。所以,我的问题是,如何访问该令牌以放入我的数据库中,最好不向最终用户显示访问令牌?
太长了;知道如何从 struts2 中的操作调用请求到外部 url 并解析响应而不将其显示给用户吗?谢谢!
干杯, 卢卡斯·雷泽克
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 URL (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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以基本上你需要 3 部分:
所以流程是:
请注意,redirect_uri 正是 authResponse 操作。因此,在用户授权您的应用程序之后 - 浏览器将重定向到 authResponse 操作
So basically you would want 3 pieces:
So the flow is:
Please note that redirect_uri is exactly is authResponse action. so after user authorizes your app - browser willo redirect to authResponse action
要在 struts 操作中访问外部网站,您有两种选择。创建一个套接字并连接到外部站点,通过 socket.outputstream 发送请求,并通过 socket.inputStream 解析响应,或者使用浏览器模拟器,例如 Apache HttpComponents 或 HTMLUnit。
我不会让我的网络应用程序的用户靠近 Facebook 网站 - 我会有自己的页面,要求 FB 登录,并使用上述两种方法之一处理与 Facebook 网站的交互。我倾向于使用 HTMLUnit,因为它的设计看起来就像远程站点的浏览器。
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.