库将Facebook登录与Play Framework集成?
-
28-09-2019 - |
题
我正在学习使用 游戏框架 并为此做一个演示应用程序。
对于此应用程序,我还希望与Facebook API集成,允许用户使用Facebook ID登录。知道游戏是一个无状态的框架及其工作方式,建议使用一些库或模块?
我找不到任何东西,但我要问以防万一。
解决方案
RESTFB 是我的首选。
这 Facebook Java API 也应该这样做。
其他提示
最新剧本! Framework v1.2.2包含一个带有Facebook OAuth2协议的示例。
这是一个具有OAUTH2身份验证的应用程序控制器:
import models.User;
import play.Logger;
import play.libs.OAuth2;
import play.libs.WS;
import play.mvc.Before;
import play.mvc.Controller;
import com.google.gson.JsonObject;
public class Application extends Controller {
// The following keys correspond to a test application
// registered on Facebook, and associated with the loisant.org domain.
// You need to bind loisant.org to your machine with /etc/hosts to
// test the application locally.
public static OAuth2 FACEBOOK = new OAuth2(
"https://graph.facebook.com/oauth/authorize",
"https://graph.facebook.com/oauth/access_token",
"95341411595",
"8eff1b488da7fe3426f9ecaf8de1ba54"
);
public static void index() {
User u = connected();
JsonObject me = null;
if (u != null && u.access_token != null) {
me = WS.url("https://graph.facebook.com/me?access_token=%s", WS.encode(u.access_token)).get().getJson().getAsJsonObject();
}
render(me);
}
public static void auth() {
if (OAuth2.isCodeResponse()) {
User u = connected();
OAuth2.Response response = FACEBOOK.retrieveAccessToken(authURL());
u.access_token = response.accessToken;
u.save();
index();
}
FACEBOOK.retrieveVerificationCode(authURL());
}
@Before
static void setuser() {
User user = null;
if (session.contains("uid")) {
Logger.info("existing user: " + session.get("uid"));
user = User.get(Long.parseLong(session.get("uid")));
}
if (user == null) {
user = User.createNew();
session.put("uid", user.uid);
}
renderArgs.put("user", user);
}
static String authURL() {
return play.mvc.Router.getFullUrl("Application.auth");
}
static User connected() {
return (User)renderArgs.get("user");
}
}
享受!
现在也有一个 fbConnect 在Play网站上列出的模块。
你也可以检查一下 http://code.google.com/p/socialauth/. 。看起来很有希望。
不隶属于 StackOverflow