伸手系列-脸书facebook登录回验-java

伸手系列-脸书facebook登录回验-java

伸手系列-脸书facebook登录回验-java

记得修改自己在fb后台配置的ACCESS_TOKEN

public class FaceBookLogin {

    protected static Logger logger= LoggerFactory.getLogger(FaceBookLogin.class);
    private final static String ACCESS_TOKEN ="393451732344860%7C6cc038bdf81153129676c3defc7af1d9";
    private final static String API_URL = "https://graph.facebook.com/debug_token?access_token=";
//    private static HttpClient httpClient = Http.createHttpClient(200, 200, 2000, 2000, 2000);

    public static Boolean check(String token,String  userId)
    {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        JSonObject resultJson = new JSonObject();
        Boolean result=false;
        try {

            String url=API_URL+ACCESS_TOKEN+"&input_token="+token;
            HttpGet httpPost = new HttpGet(url);
//            RequestConfig config=RequestConfig.custom()
//                    .set 增加静态方法 配置 超时时间
            CloseableHttpResponse response = httpclient.execute(httpPost);
            try {

                HttpEntity entity = response.getEntity();
                if (response.getStatusLine().getStatusCode() == 200) {
                    String resp = EntityUtils.toString(entity);
                    JSonObject data = JSONObject.parseObject(resp).getJSonObject("data");
                    result=data.getBoolean("is_valid");
                    if(data.getString("user_id").equals(userId))
                    {
                        result=true;
                    }
                    else
                        result=false;
                } else {
                    resultJson.put("facebook login code", response.getStatusLine().getStatusCode());
                    logger.error("[facebook login错误码] :" + response.getStatusLine().getStatusCode());
                    logger.error("[facebook login请求地址] :" + url);
                    result=false;
                }

            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            logger.error("[异常] :", e.getMessage());
        } catch (IOException e) {
            logger.error("[异常] :", e.getMessage());
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                logger.error("[httpclient 关闭异常] : ", e.getMessage());
            }
        }
        return result;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5563446.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存