简体   繁体   English

restfb:使用Java编写Facebook应用程序(使用新的图形API)

[英]restfb: Writing a facebook application with java (using the new graph api)

I'm trying to write a facebook application using Java tomcat with RestFB. 我正在尝试使用Java tomcat和RestFB编写一个Facebook应用程序。

the restfb documentation shows the following: restfb文档显示以下内容:

I think that i may be looking at the wrong instructions and this is for a facebook connect or anything else besides an actual facebook application inside apps.facebook.com/app_name. 我认为我可能正在看错指令,这是针对facebook连接或apps.facebook.com/app_name中除实际Facebook应用程序以外的其他任何内容。

I would really appreciate any relevant information regarding the issue. 非常感谢您提供有关此问题的任何相关信息。 I'm simply trying to create a simple facebook application that prints the name of the user. 我只是试图创建一个简单的Facebook应用程序,以打印用户名。

In general after I fetch the acces token of the user i can do the following: 通常,在获取用户的acces令牌后,我可以执行以下操作:

FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN);
User user = facebookClient.fetchObject("me", User.class);
out.println("User name: " + user.getName());

My problem is how do i properly fetch the access token of the user? 我的问题是如何正确获取用户的访问令牌? in the information i showed at the top of the post, it shows that i need to make 2 requests. 在我在帖子顶部显示的信息中,它表明我需要提出2个请求。 the first for the code and the other for the acess token. 第一个用于代码,另一个用于访问令牌。 and the request for the access token actually reveals my app secret key to the user because each time i forward him to a different page the user can easily view the get and set parameters. 访问令牌的请求实际上向用户显示了我的应用密钥,因为每次我将他转发到另一个页面时,用户都可以轻松查看get和set参数。

so i guess i'm pretty lost here. 所以我想我在这里很迷路。

please help! 请帮忙! :) :)

thanks 谢谢

update after comments 评论后更新

with these instructions i need two times to redirect the user's page. 根据这些说明,我需要两次重定向用户页面。 first to get the code and then to get the access token. 首先获取代码,然后获取访问令牌。 the user can see these two redirections and because of that he can easily see the facebook application key and secret key from the get parameters. 用户可以看到这两个重定向,因此,他可以轻松地从get参数中看到facebook应用程序密钥和秘密密钥。 how do i make sure that these steps are hidden from the user? 我如何确保这些步骤对用户隐藏?

As stated in the comments, these are the steps you need to take to access Facebook's graph API. 如评论中所述,这些是访问Facebook图形API所需采取的步骤。 However, to answer your second question: 但是,要回答第二个问题:

"How do I make sure that these steps are hidden from the user?" “如何确保对用户隐藏了这些步骤?”

Only the first request should be performed by the user's browser. 用户的浏览器仅应执行第一个请求。 The purpose being that Facebook wants to make sure it is the sole authorization provide for the user's Facebook identity. Facebook的目的是要确保它是为用户的Facebook身份提供的唯一授权。 Depending on the application you are writing, you would either use the redirect URL to point to the default redirect URL that you specified, or specify a custom url on your website that you will use to retrieve the token. 根据您正在编写的应用程序,您可以使用重定向URL指向您指定的默认重定向URL,或者在您的网站上指定用于检索令牌的自定义URL。 The first approach is typically used by stand-alone applications such as mobile devices that can control how the browser handles redirects. 第一种方法通常由独立应用程序(例如移动设备)使用,它们可以控制浏览器如何处理重定向。 The second approach would be used for a custom web-based application. 第二种方法将用于基于Web的自定义应用程序。 Once you receive the access token, then you would perform the second operation within your code (using your favorite http apis) and not through the browser. 收到访问令牌后,您将在代码中执行第二项操作(使用您喜欢的http api),而不是通过浏览器。 The redirect on the access_token url is compared against the redirect url specified on the authentication-url. 将access_token URL上的重定向与身份验证URL上指定的重定向URL进行比较。 Facebook uses it for validation only and does not perform an actual redirect on the successful completion of the request. Facebook仅将其用于验证,并且不会在成功完成请求后执行实际的重定向。

Here are the high-level steps: 以下是高级步骤:

  1. Redirect user's browser to the authentication-url specifying the appropriate redirect_uri 将用户的浏览器重定向到指定适当的redirect_uri的authentication-url
  2. Retrieve verification token from redirected browser request 从重定向的浏览器请求中检索验证令牌
  3. Perform access_token retrieval using your preferred HTTP framework (no user input required) 使用您首选的HTTP框架执行access_token检索(无需用户输入)
  4. Parse results and retrieve access token 解析结果并检索访问令牌
  5. Initial restfb with token and use as needed 带有令牌的初始restfb并根据需要使用

The REST API has been deprecated. REST API已弃用。 You should look in to the JavaScript and Graph APIs instead - there is a good article on this here: http://ocpsoft.org/opensource/creating-a-facebook-app-setup-and-tool-installation/ (Three part series, very detailed :) 您应该改用JavaScript和Graph API-此处有一篇不错的文章: http : //ocpsoft.org/opensource/creating-a-facebook-app-setup-and-tool-installation/ (三部分系列,非常详细:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM