9

我们正在使用 android 中的应用内购买来实现订阅,我们正在获取如下格式的购买时间戳

'{
   "orderId":"GPA.1234-5678-9012-34567",
   "packageName":"com.example.app",
   "productId":"exampleSku",
   "purchaseTime":1345678900000,
   "purchaseState":0,
   "developerPayload":"bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ",
   "purchaseToken":"opaque-token-up-to-1000-characters"
 }'

但是我们需要在应用程序 UI 中显示到期日期,并且我们想从 Play 商店获取确切的到期日期。我们假设如果我们手动计算到期日期,则可能与 Play 商店到期日期冲突。谁能解释一下“如何在android中获取订阅的到期日期?”

4

3 回答 3

8

要在订阅后获取订阅到期日期,需要按照以下步骤操作。

第 1 步:首先从以下 api Google Publisher API 获取“mRefreshToken”

第 2 步: 接下来需要使用 params("mRefreshToken", "client_id" and "client_secret") 和下面的 api 获取 "access_tokon"。

final StringRequest mStringRequest = new StringRequest(Request.Method.POST, "https://accounts.google.com/o/oauth2/token",
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            [From response -get access_tokon]
        }
    }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
   }}) {
@Override
protected Map<String, String> getParams() {

    Map<String, String> params = new HashMap<>();
    params.put("grant_type", "refresh_token");
    params.put("client_id", "your_client_id");
    params.put("client_secret", "your_client_secret");
    params.put("refresh_token", mRefreshToken);

    return params;
}};

第 3 步: 您从上面的 api 获得“accessToken”,然后使用下面的 api 和参数获取到期 json

字符串 url = " https://www.googleapis.com/androidpublisher/v2/applications/ " + AppController.getInstance().getPackageName() + "/purchases/subscriptions/" + mSubscriptionId + "/" + "tokens/" + mPurchaseToken;

 final StringRequest mStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            [From response -get expiry detail json]
        }
    }, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
    error.printStackTrace();
}}) {

@Override
public Map<String, String> getHeaders() throws AuthFailureError {

    Map<String, String> params = new HashMap<>();
    params.put("Authorization", "Bearer " + accessToken);
    return params;
}};

更多参考: 来自谷歌的官方文档网址“ https://developers.google.com/android-publisher/api-ref/purchases/subscriptions/get ”和“ https://developers.google.com/android-publisher/授权

于 2018-06-19T09:56:01.317 回答
0

有两种方法可以获取订阅到期日期。第一种方式,您可以实现 google APIs 以获取到期日期第二种方式 - 当您的交易成功时,您将获得这些参数的订阅期和购买时间,您可以轻松获得日期而不会出现任何错误

private String getSubscriptionRenewingDate3(long purchaseTime, 

String setSubscriptionPeriod) {

            Date purchaseDate = new Date(purchaseTime);

            Calendar calendar = Calendar.getInstance();

            calendar.setTime(purchaseDate);

            Date now = new Date();

            while (calendar.getTime().before(now)) {

                switch (setSubscriptionPeriod) {

                    case "P1W":
                        calendar.add(Calendar.HOUR, 7 * 24);
                        break;
                    case "P1M":
                        calendar.add(Calendar.MONTH, 1);
                        break;
                    case "P3M":
                        calendar.add(Calendar.MONTH, 3);
                        break;
                    case "P6M":
                        calendar.add(Calendar.MONTH, 6);
                        break;
                    case "P1Y":
                        calendar.add(Calendar.YEAR, 1);
                        break;
                }
            }

            String name = String.valueOf(calendar.getTime());
            Toast.makeText(requireContext(), "Dateeeeee" + name, Toast.LENGTH_SHORT).show();
            return name;
    //    }

       // return null;
    }
于 2021-08-28T10:54:31.267 回答
-1

我尝试使用此 API “ https://www.googleapis.com/androidpublisher/v2/applications/ ” + AppController.getInstance().getPackageName() + “/purchases/subscriptions/” + mSubscriptionId + “/” + “tokens /" + mPurchaseToken;

使用正确的有效数据,但我发现了这个错误:

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

请告诉我是否有任何解决方案。我按照所有步骤完成所有游戏控制台和谷歌云控制台帐户。

于 2020-02-06T12:38:51.407 回答