google play 内购数据拉取

拉取 google play console后台内购数据收入。

1:查看官方文档:

 https://support.google.com/googleplay/android-developer/answer/6135870?visit_id=638180953496505768-3845517287&p=stats_export&rd=1#export

这里采用 从Google Cloud Storage 下载报表-》使用用户端程式库和服务帐户下载报表

 

2:准备工作。

 1; 从文档的步骤开始,新建立项目。注意是google developers console后台,不是google play console后台。新建项目,在 IAM和管理中-》添加一个服务账号-》生成秘钥。下载的json文件保留。

 

 

2;添加权限。需要将这里添加的服务账号邮件添加到 goole play console 后台, 权限需要有能下载报表的权限

    

 

 3:Storage sdk初始化

文档中表示,google play console后台的内购收入会每天往Google Cloud Storage的某个zip包添加数据,

我们需要从storage中下载zip包,解析里面的数据就行。通过上面的关联,我们可以通过新建的google cloud账号api去下载google play每天更新的storage中的zip.

 

在后台开启api权限

 

添加storage SDK 参考:https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-java

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.13.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-storage</artifactId>
  </dependency>

 

storage初始化

    public void initApay() {
        String projectId = "pc-api-8153";//准备工作中那个新建项目的id-- projectId
        InputStream is = AndroidPayHelper.class.getResourceAsStream("/firebaseauth/pay_android_key.json");//下载的json文件
        try {
            GoogleCredentials credentials = GoogleCredentials.fromStream(is)
                    .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/devstorage.read_only"));

            Storage storage = StorageOptions.newBuilder()
                    .setCredentials(credentials)
                    .setProjectId(projectId)
                    .build()
                    .getService();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

 

 4:数据拉取

参考:https://cloud.google.com/storage/docs/downloading-objects?hl=zh-CN#storage-download-object-java

 1,google play 后台查看storage的url和bucket:类似--  gs://pubsite_prod_8153303996/sales/

,

2,得到的是 gs://pubsite_prod_8153303996/sales/ 其中pubsite_prod_8153303996 就是bucketName(注意:这个是复制出来的,跟前面项目的projectId不是一个)。展示一下storage下面的文件

 

     String bucketName = "pubsite_prod_8153303996";
     Page<Blob> buckets = storage.list(bucketName);
     for (Blob bucket : buckets.iterateAll()) {
          System.out.println(bucket.getName());
     }

 

打印出来的会有很多,只需要注意 sales开头的就行:这些就是需要下载的内购数据。

sales/salesreport_202302.zip
sales/salesreport_202303.zip
sales/salesreport_202304.zip

 

3,zip数据下载

        String bucketName = "pubsite_prod_8153303996";
        BlobId blobId = BlobId.of(bucketName, "sales/salesreport_202304.zip");
        Blob blob = storage.get(blobId);
        byte[] content = blob.getContent();

        try {
            byte[] bytes = GzipUtils.unZip(content);
            String body = new String(bytes, StandardCharsets.UTF_8);
            putData(body);
        } catch (Exception e) {
            TaskLoggerUtil.Error(className + " unzip error:", e);
        }

 

4,下载的锁zip包,需要解析:

public static byte[] unZip(byte[] data) throws IOException {
        byte[] b = null;
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        ZipInputStream zip = new ZipInputStream(bis);
        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();

        return b;
    }
View Code

 

5,分析数据

        String cDate = "2023-04-23";//表里的数据是这个月所有数据,如果只需要一天的,需要筛选
        String[] separated = datas.split("\n");//拆分行
        int len = separated.length;
        for (int i = 1; i < len; i++) {
            String[] row = separated[i].split(",");//拆分列,每一行数据是通过,组成的

            //日期
            String date = DateUtils.dateConvertionYs(String.valueOf(row[1]));//这里是将yyyy/MM/dd转为yyyy-MM-dd方便比较
            if (!cDate.equals(date)) continue;

            String packId = row[6];//包名
            //收益
            float proceeds = Float.parseFloat(row[12]);//收入
        }

 

 

5:展示数据

数据跟ios一样,需要自己通过汇率转为一种货币,方便计算。

 

 

参考文档:https://support.google.com/googleplay/android-developer/answer/6135870?visit_id=638180953496505768-3845517287&p=stats_export&rd=1#export

https://cloud.google.com/storage/docs/downloading-objects?hl=zh-CN#storage-download-object-java

posted @ 2023-04-27 11:53  Foto_CShow  阅读(624)  评论(0编辑  收藏  举报