java - 将 Facebook 登录与 Play Framework 集成的库?

标签 java scala frameworks playframework

<分区>

我正在学习使用 Play Framework并为它做一个演示应用程序。

对于这个应用程序,我还想与 Facebook API 集成,允许用户使用 Facebook ID 登录。知道 Play 是一个无状态框架及其工作方式,有没有推荐的库或模块?

我没能找到任何东西,但为了以防万一,我问一下。

最佳答案

最新的游戏! Framework v1.2.2 包含一个带有 Facebook OAuth2 协议(protocol)的示例。

这是一个带有 OAuth2 身份验证的应用程序 Controller :

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");
    }

}

尽情享受吧!

关于java - 将 Facebook 登录与 Play Framework 集成的库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3478532/

相关文章:

arrays - 为什么 map/filter ... 不适用于无数组?

scalaquery 问题没有隐式 session

macos - osx 10.9.5 代码签名 V2 - 使用 : bundle format is ambiguous 签署框架

swift:导入 CSwiftV 框架

api - OAuthSwift- "No Such Module OAuthSwift"

java - 如何在 Mac 上设置 Java 类路径?

类似于 Python 的 Java 类列表

java - 如何在 OnItemClickListener() 中选中复选框?

java - 用于填充多个表的复选框和 sql 命令的对象名称

java - 斯卡拉 'nothing to override' 错误