facebook回传

1、引入依赖

首先引入依赖,这里我使用API v14.0:

XML 复制代码
        <dependency>
            <groupId>com.facebook.business.sdk</groupId>
            <artifactId>facebook-java-business-sdk</artifactId>
            <version>14.0.0</version>
        </dependency>

2、修改配置文件

在application.yml或properties中引入 pix_id 和 access_token:

XML 复制代码
facebook.pix.id=xxxxxxx
facebook.access.token=xxxxxxxxxxx

3、Controller文件

编写接口:

java 复制代码
    @PostMapping("/facebook")
    public CommonResult<String> returnToPlat(@RequestBody @Valid InvestParam investParam) {
        service.returnToPlat(investParam);
        return CommonResult.ok();
    }

4、Service文件

java 复制代码
    @Value("${facebook.pix.id}")
    private String pixId;

    @Value("${facebook.access.token}")
    private String accessToken;

    @Override
    public void returnToPlat(InvestParam investParam) {

        
        APIContext context = new APIContext(accessToken).enableDebug(true);
        context.setLogger(System.out);

        UserData userData = new UserData()
                .fbc(investParam.getExposureId());

        BigDecimal decimal = investParam.getFee().divide(new BigDecimal(100));
        Float fee = Float.valueOf(String.valueOf(decimal));

        CustomData customData = new CustomData()
                .currency("usd")
                .value(fee);

        Event purchaseEvent = new Event();
        purchaseEvent.eventName(investParam.getEventType())
                .eventTime(System.currentTimeMillis() / 1000L)
                .userData(userData)
                .customData(customData)
                .actionSource(ActionSource.website);

        EventRequest eventRequest = new EventRequest(pixId, context);
        eventRequest.addDataItem(purchaseEvent);

        try {
            EventResponse response = eventRequest.execute();
            investOperate.setResult(response.toString());
            this.save(investOperate);
            System.out.println(String.format("Standard API response : %s ", response));
        } catch (APIException e) {
            e.printStackTrace();
        }
    }

回传接口API官方文档:

https://developers.facebook.com/docs/marketing-api/conversions-api/using-the-api

相关推荐
q***69771 分钟前
【Spring Boot】统一数据返回
java·spring boot·后端
Hollis Chuang3 分钟前
Spring Boot 4.0 正式发布,人麻了。。。
java·spring boot·后端·spring
Moshow郑锴25 分钟前
实战分享:用 SpringBoot-API-Scheduler 构建 API 监控闭环 —— 从断言验证到智能警报
java·spring boot·后端·任务调度
掘我的金43 分钟前
播放器最怕“首帧黑屏”?我给 LibreTV 加了一套缓冲与预加载策略
java
低客的黑调1 小时前
为你的项目选择一个适合的[垃圾收集器]
java·jvm·算法
雨中飘荡的记忆1 小时前
优惠券系统设计与实现
java
1***t8271 小时前
将 vue3 项目打包后部署在 springboot 项目运行
java·spring boot·后端
芬加达1 小时前
leetcode34
java·数据结构·算法
__万波__1 小时前
二十三种设计模式(三)--抽象工厂模式
java·设计模式·抽象工厂模式
better_liang2 小时前
每日Java面试场景题知识点之-线程池配置与优化
java·性能优化·面试题·线程池·并发编程