接入GoogePay 退款APi

本文档介绍了GooglePay退款API的接入步骤,包括创建服务账号、添加权限、配置maven依赖,并提供了Java代码示例。在实际操作中遇到了403权限错误,原因是查询了无权限的历史订单。解决方案是确保查询范围内的订单有权限访问,并提醒开发者在对接第三方API时,应从简单的操作开始逐步验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GooglePay退款Api接入准备工作

(1)在GooglePay 主体账户下创建服务账号,保存秘钥(json),并给服务账号添加应用以及应用权限,给服务账号分配账号权限(建议直接给管理员,必须要有财务权限),细节可以参考其他博客,大佬们有详细介绍,我就不累赘了.
注意:申请的服务账号不是立即生效的,官方给的说法是24小时左右,网上说可以通过在应用类新建计费点(随便建一个,后续删除即可)的方式,强制刷新.可以缩短到几分钟生效.

mavne依赖

 <!-- google pay 依赖 -->
        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client</artifactId>
            <version>1.32.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.auth</groupId>
            <artifactId>google-auth-library-oauth2-http</artifactId>
            <version>0.26.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-androidpublisher</artifactId>
            <version>v3-rev20210605-1.32.1</version>
        </dependency>

代码

//你下载的json秘钥的地址
private final static String keyPath =""
//包体在google商店的包名(com开头的)
private final static String packageName =""

public static void main(String args[]) {
        AndroidPublisher androidPublisher = getAndroidPublisher();
        VoidedPurchasesListResponse voidedPurchasesListResponse =null;
        try {
             voidedPurchasesListResponse = androidPublisher.purchases()
                    .voidedpurchases()
                    .list(packageName)
                    .execute();
           Sys
        } catch (IOException e) {
            e.printStackTrace();
        }
         System.out.println(voidedPurchasesListResponse);
    }
    
 public static AndroidPublisher getAndroidPublisher(){
        ClassPathResource classPathResource = new ClassPathResource(keyPath);
        GoogleCredentials credentials = null;
        try {
            credentials = GoogleCredentials.fromStream(classPathResource.getInputStream())
            			//给权限.根据官方文档;https://www.googleapis.com/auth/androidpublisher
                    .createScoped(AndroidPublisherScopes.ANDROIDPUBLISHER);
        } catch (IOException e) {
            e.printStackTrace();
        }
        AndroidPublisher androidPublisher = null;
        try {
            androidPublisher = new AndroidPublisher.Builder(
                    GoogleNetHttpTransport.newTrustedTransport(),
                    GsonFactory.getDefaultInstance(),
                    new HttpCredentialsAdapter(credentials)
            ).build();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return androidPublisher;
    }

遇到的大坑

{ 
  "code": 403,
  "errors": [
    { 
      "domain": "androidpublisher",
      "message": "The current user has insufficient permissions to perform the requested operation.",
      "reason": "forbidden"
    }
  ],
  "message": "The current user has insufficient permissions to perform the requested operation."
}

是由于当时查询退款的时间的时候,手抽,选择的20207月的订单,导致反馈的这个错误,开始一直以为是授权失败的问题,结果是查询范围没有权限.其实权限是通的.给自己的警醒,后续做第三方api时,最开始后的时候从最简单方案的开始.慢慢来.

参考文档

官方文档:
https://developers.google.com/identity/protocols/oauth2
https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.voidedpurchases/list
https://developers.google.com/identity/protocols/oauth2/service-account

各位大佬博客:
https://www.jianshu.com/p/6ce1d630d4be

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值