GooglePay退款Api接入
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