android-Goggle后端支付验证流程 - Go语言中文社区

android-Goggle后端支付验证流程



title: android-Goggle后端支付验证流程
categories: Android
tags: [android, GooglePlay, 支付, 后端]
date: 2019-08-08 19:44:00
comments: false

参考链接: https://blog.csdn.net/u013334392/article/details/94602284


整个开发背景是前端在调用完google play支付流程后,需要后台验证支付结果以及在自己的服务生成订单相关信息。

由此着手对google后台验证的调研,首先官方流程:
官方流程 - https://developers.google.com/android-publisher/authorization


整个操作流程如下:

  1. 在开发者后台中启用 Google Play Android Developer 项目, 点击会跳转到 api 页面 https://console.developers.google.com/apis

  2. 在Google APIS 中选择 Google Play Android Developer项目–【凭据】–【OAuth同意屏幕】

    只要填入信息即可,无需验证,但一定要添加【域名(已获授权的网域)】主域名地址

    example.com
    
  3. 创建 OAuth 客户端凭据

    填入网址

    https://www.example.com
    

    创建好后, 点进去, 获取到 client_id, client_secret, redirect_uri

  4. 创建好Oauth客户端ID后管理者用项目最高权限的GP账号登录,访问:

    https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=https://www.example.com&client_id=123122121

    复制链接后将下面的两个参数 value 修改成分配的正确值即可

    • redirect_uri=已授权的重定向URI
    • client_id=google分配的客户端ID

    以上链接登录成功后,会在浏览器的地址栏中显示 code,只要拿出 code 的值即可:

    注:有链接会被转义,比如/会被转成%2F,就如以上参数值,copy下来后手动改一下:
    Code=4/bAEGOfkpDnG5hhtv8E7FSkKPp-oFVQpTPeg8l_jWjKasasdasdasdasd

  5. 获取到 code 后发送 post 请求,以下 POST 请求实例以 postman 工具 (下载地址: https://www.getpostman.com/downloads/) 来进行操作

    注:一定要按步骤来才能到发 post 请求这一步

    请求头Content-Type = application/x-www-form-urlencoded 以表单的形式传参

    请求以下接口获得 refresh_token

    https://accounts.google.com/o/oauth2/token

    参数:

    
    grant_type=authorization_code 为固定值
    code=4步中获取到的 code 值,
    client_id=3步中获取到的 客户端ID
    client_secret=3步中获取到的 客户端密钥
    redirect_url=3步中获取到的 重定向链接(https://www.example.com)
    

    注:只有在第一次请求时会返回 refresh_token, 之后将不会返回了,所以在获得返回的 refresh_token 后,请妥善保存此token。以后的请求中都不会再出现,对于当前创建的凭证账号是唯一且永久有效的,可以存放到配置文件(或者写到数据库),以备后用

    返回结果中的expires_in 是指access_token的时效,为3600秒

    第一次请求会获得refresh_token,妥善保存

  6. 携带 refresh_token 可获取 access_token,POST 请求访问

    请求头Content-Type = application/x-www-form-urlencoded 以表单的形式传参

    再次请求以下接口时只会获得access_token:

    https://accounts.google.com/o/oauth2/token

    参数:

    grant_type=refresh_token 为固定值,
    client_id=3步中获取到的 客户端ID
    client_secret=3步中获取到的 客户端密钥
    refresh_token=5步中获取到的refresh_token值,
    

  7. 验证:GET请求

    请求以下接口时查询获得商品信息:(文档地址: https://developers.google.com/android-publisher/api-ref/purchases/products/get)

    https://www.googleapis.com/androidpublisher/v3/applications/packageName/purchases/products/productId/tokens/token

    上面的http请求中的参数信息需要修改为以下内容:

    packageName=需要查询的应用ID(包名、gradle中的applicationId )
    productId=开发者后台中创建好的商品ID
    purchaseToken=订单中purchaseToken, 客户端提供
    access_token=6步中获得的access_token
    

    返回数据:(数据结构: https://developers.google.com/android-publisher/api-ref/purchases/products#resource)

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/yangxuan0261/article/details/99014566
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-08-27 23:29:10
  • 阅读 ( 1895 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢