How to get expiry date for Subscription with client side in Android?

前端 未结 2 706
南方客
南方客 2021-02-09 07:08

We are implementing the subscription using in-app purchase in android, We are getting the purchase timestamp like the below format

\'{
   \"orderId\":\"GPA.1234         


        
2条回答
  •  不要未来只要你来
    2021-02-09 08:00

    To Get Subscription Expiry date after subscription, need to follow below steps.

    Step 1: First Get "mRefreshToken" from following api Google Publisher API

    Step 2: Next need to get "access_tokon" using params("mRefreshToken", "client_id" and "client_secret") and below api.

    final StringRequest mStringRequest = new StringRequest(Request.Method.POST, "https://accounts.google.com/o/oauth2/token",
        new Response.Listener() {
            @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 getParams() {
    
        Map 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;
    }};
    

    Step 3: You have "accessToken" from above api, after that get expiry json using below api and params

    String 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() {
    
            @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 getHeaders() throws AuthFailureError {
    
        Map params = new HashMap<>();
        params.put("Authorization", "Bearer " + accessToken);
        return params;
    }};
    

    For more reference: official document url from Google "https://developers.google.com/android-publisher/api-ref/purchases/subscriptions/get" and "https://developers.google.com/android-publisher/authorization"

提交回复
热议问题