我们正在使用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中获取订阅的到期日期?”
要获得订阅后的订阅到期日期,需要执行以下步骤。
步骤1: 首先从以下api Google Publisher API获取“ mRefreshToken”
步骤2: 接下来需要使用api以下的params(“ mRefreshToken”,“ client_id”和“ client_secret”)获取“ access_tokon”。
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; }};
步骤3: 您从上面的api获得了“ accessToken”,然后使用下面的api和params获取了过期的json
字符串url =“ https://www.googleapis.com/androidpublisher/v2/applications/ ” + AppController.getInstance()。getPackageName()+“ / purchases / subscriptions /” + mSubscriptionId +“ /” +“令牌/” + 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; }};
有关更多参考: Google的正式文档网址“ https://developers.google.com/android-publisher/api-ref/purchases/subscriptions/get ”和“ https://developers.google.com/android-publisher/授权 ”