Search code examples
huawei-mobile-services

"Token is expired or invalid" when trying to server-side validate Huawei subscription


I'm trying to do a server-side verification of Huawei subscription, but currently I'm stuck at verification part

  1. Im successfully obtain access_token from this url
  1. Do a base64encode UTF-8 format of string 'APPAT:my_access_token'

  2. Then I'm trying to get purchaseDetails from https://subscr-drcn.iap.hicloud.com/sub/applications/v2/purchases/get. Im sending subscriptionId and purchaseToken in json format and base64encoded access_token in Authorization ('Basic my_access_token') header and Content-Type=application/json;charset=UTF-8

  3. Every time I get this response:

    { "responseCode": "6", "responseMessage": "Token is expired or invalid" }

Searching for this message didn't help. What can be the problem with it? Is it invalid purchaseToken or invalid accessToken?

Please help me with this, thanks!


Solution

  • Please check as follows:

    1. Check your access_token: obtain the access_token and print it.
    2. When referencing Base64, refer to import org.apache.commons.codec.binary.Base64;
    3. Add to the header correctly:
    * Construct an Authorization field in the request header.
    *
    * @param appAt App-level access token.
    * @return headers Return the request header.
    */
        public static Map<String, String> buildAuthorization(String appAt) {
            String oriString = MessageFormat.format("APPAT:{0}", appAt);
            String authorization =
            MessageFormat.format("Basic {0}", Base64.encodeBase64String(oriString.getBytes(StandardCharsets.UTF_8)));
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", authorization);
            headers.put("Content-Type", "application/json; charset=UTF-8");
            return headers;
        }
        /**
    

    For more details, see docs.