java解析facebook的android app直投下的Referral URL

背景:

在facebook的应用推广中,一般使用两种方式,一种是app直投,一种是w2a。

app直投就是用户点击广告直接跳转到应用商店进行下载应用

w2a就是通过落地页方式引导用户进行应用下载

在w2a模式下,可以通过落地页链接传递很多信息,丢失率极低,在android的app直投中,facebook是通过集成Google Play Install Referrer来实现广告元数据的透传的。

android可以通过fb的sdk拿到Google Play Install Referrer的数据如下:

url 复制代码
utm_source=apps.facebook.com
&utm_campaign=fb4a
&utm_content=%7B%22app%22%117412253453453%2C%22t%22%3A1723086075%2C%22source%22%3A%7B%22data%22%3A%22767b16f17139f42928f097aa5dcd0c682584aca809fa42a795ac3b1f9537a5eb375587ff8dfe9c53f26465c6ec1c06d665eb365acdd4de51338b8c68dc65bb52e1a57bd74f17c46f8ab6e45a801ae9a6d60b8f434817044ab5c1e8c45a4de86747e1e165c9bcb0bc354dc0571fefecaeabdc343eb26a81aa65b70455620004b54654f985f625829efc87eba3470d74c174befa3e5267b441418854ced55efe89335af17816b49b3bbc890579abcfc83d4e71cbabd06d932e7832fb80d59c1379302d6e76226440c204c1e0ba8c8d2a612ba648f0f13814fef9e347c5eb562dafb5c0e668ee2b6efa4eb0290f3b1901f4f2c469852728f53fe824774a708d749dee56241a0fc46c15e64dba20f3c1132d0318adb66c68dcc5569131cccdcd7b2a2dc6a2bc0eb0eda2bd2a8ac3c6a15ea88163aac41e2e7167b764da6f7302a786fda4c7fe768003242f91c95c2e1a8a24a5a99f155f87d546793e89a7b254222f5216e0504db1880aa69515043d584ba4056bdf93159b0a433f234150653da8bc293b0dcd7d495946fe170a314348390dbbd79a69d8d8dfe58d44d3e3291d3f913ad4e7caff375b114580430745aee778ef9078861a87868f7d99d6f8c788ae%22%2C%22nonce%22%3A%22824936fdaa16b355c76c0b1d%22%7D%7D
json 复制代码
utm_content中的data信息就是广告的元数据信息,那这个如何去解呢?
{"app":117412253453453,
   "t":1723086075,
   "source":{
		  "data":"767b16f17139f42928f097aa5dcd0c682584aca809fa42a795ac3b1f9537a5eb375587ff8dfe9c53f26465c6ec1c06d665eb365acdd4de51338b8c68dc65bb52e1a57bd74f17c46f8ab6e45a801ae9a6d60b8f434817044ab5c1e8c45a4de86747e1e165c9bcb0bc354dc0571fefecaeabdc343eb26a81aa65b70455620004b54654f985f625829efc87eba3470d74c174befa3e5267b441418854ced55efe89335af17816b49b3bbc890579abcfc83d4e71cbabd06d932e7832fb80d59c1379302d6e76226440c204c1e0ba8c8d2a612ba648f0f13814fef9e347c5eb562dafb5c0e668ee2b6efa4eb0290f3b1901f4f2c469852728f53fe824774a708d749dee56241a0fc46c15e64dba20f3c1132d0318adb66c68dcc5569131cccdcd7b2a2dc6a2bc0eb0eda2bd2a8ac3c6a15ea88163aac41e2e7167b764da6f7302a786fda4c7fe768003242f91c95c2e1a8a24a5a99f155f87d546793e89a7b254222f5216e0504db1880aa69515043d584ba4056bdf93159b0a433f234150653da8bc293b0dcd7d495946fe170a314348390dbbd79a69d8d8dfe58d44d3e3291d3f913ad4e7caff375b114580430745aee778ef9078861a87868f7d99d6f8c788ae"
		  ,"nonce":"824936fdaa16b355c76c0b1d"
	 }
}

需要使用facebook应用中的安装引荐来源解密密钥

那如何来解密呢?

jsx 复制代码
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.modes.GCMBlockCipher;
import org.bouncycastle.crypto.params.AEADParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.dromara.hutool.core.codec.binary.HexUtil;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;

public class AESGCMExample {
    private static final int GCM_NONCE_LENGTH = 12; // GCM 计数器大小
    private static final int GCM_TAG_LENGTH = 16; // GCM 验证标签大小 (128位)
    
        public static void main(String[] args) throws Exception {
				// 密钥(这个是转过hex的,所以要先转回来)
        String keyStr= "使用自己的安装引荐来源解密密钥";
        byte[] decode = HexUtil.decode(keyStr);
        // 随机数
        String nonce ="824936fdaa16b355c76c0b1d";
        SecretKeySpec key = new SecretKeySpec(decode, "AES");// 将 key 转换为SecretKey
				// 密文
        String encryptedText = "767b16f17139f42928f097aa5dcd0c682584aca809fa42a795ac3b1f9537a5eb375587ff8dfe9c53f26465c6ec1c06d665eb365acdd4de51338b8c68dc65bb52e1a57bd74f17c46f8ab6e45a801ae9a6d60b8f434817044ab5c1e8c45a4de86747e1e165c9bcb0bc354dc0571fefecaeabdc343eb26a81aa65b70455620004b54654f985f625829efc87eba3470d74c174befa3e5267b441418854ced55efe89335af17816b49b3bbc890579abcfc83d4e71cbabd06d932e7832fb80d59c1379302d6e76226440c204c1e0ba8c8d2a612ba648f0f13814fef9e347c5eb562dafb5c0e668ee2b6efa4eb0290f3b1901f4f2c469852728f53fe824774a708d749dee56241a0fc46c15e64dba20f3c1132d0318adb66c68dcc5569131cccdcd7b2a2dc6a2bc0eb0eda2bd2a8ac3c6a15ea88163aac41e2e7167b764da6f7302a786fda4c7fe768003242f91c95c2e1a8a24a5a99f155f87d546793e89a7b254222f5216e0504db1880aa69515043d584ba4056bdf93159b0a433f234150653da8bc293b0dcd7d495946fe170a314348390dbbd79a69d8d8dfe58d44d3e3291d3f913ad4e7caff375b114580430745aee778ef9078861a87868f7d99d6f8c788ae";
        byte[] decrypt = decrypt(HexUtil.decode(encryptedText), HexUtil.decode(keyStr), HexUtil.decode(nonce));
        System.out.println("Decrypted Text: " + new String(decrypt));
    }
    
    public static byte[] decrypt(byte[] EncryptedText, byte[] key, byte[] iv) {
        try {
            GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
            AEADParameters parameters = new AEADParameters(new KeyParameter(key), MacBitSize, iv, null);

            cipher.init(false, parameters);
            byte[] plainBytes = new byte[cipher.getOutputSize(EncryptedText.length)];
            int retLen = cipher.processBytes(EncryptedText, 0, EncryptedText.length, plainBytes, 0);
            cipher.doFinal(plainBytes, retLen);

            return plainBytes;
        } catch (IllegalArgumentException | IllegalStateException | DataLengthException | InvalidCipherTextException ex) {
            System.out.println(ex.getMessage());
        }

        return null;
    }

}

解密出来的内容

json 复制代码
{
    "ad_id": 广告id,
    "ad_objective_name": "推广目标",
    "adgroup_id": 广告组id,
    "adgroup_name": "广告组名称",
    "campaign_id": 推广系列id,
    "campaign_name": "推广系列名称",
    "campaign_group_id": 未知,
    "campaign_group_name": "未知",
    "account_id": 广告主账户id,
    "is_instagram": false,
    "is_an": false,
    "publisher_platform": "facebook",
    "platform_position": null
}

官方只提供了php和python的写法,我这里是写的是在java服务端的解法

关于Facebook App Ads Referral URL

https://developers.facebook.com/docs/app-ads/install-referrer/

facebook应用管理地址

https://developers.facebook.com/apps/?show_reminder=true

相关推荐
Viktor_Ye13 分钟前
高效集成易快报与金蝶应付单的方案
java·前端·数据库
hummhumm16 分钟前
第 25 章 - Golang 项目结构
java·开发语言·前端·后端·python·elasticsearch·golang
一二小选手20 分钟前
【Maven】IDEA创建Maven项目 Maven配置
java·maven
J老熊26 分钟前
JavaFX:简介、使用场景、常见问题及对比其他框架分析
java·开发语言·后端·面试·系统架构·软件工程
猿java30 分钟前
什么是 Hystrix?它的工作原理是什么?
java·微服务·面试
AuroraI'ncoding32 分钟前
时间请求参数、响应
java·后端·spring
所待.3831 小时前
JavaEE之线程初阶(上)
java·java-ee
Winston Wood1 小时前
Java线程池详解
java·线程池·多线程·性能
手握风云-1 小时前
数据结构(Java版)第二期:包装类和泛型
java·开发语言·数据结构
喵叔哟1 小时前
重构代码中引入外部方法和引入本地扩展的区别
java·开发语言·重构