You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visa卡Google Pay推送配置API请求返回404资源不存在问题

Visa Google Pay配置API调用出现404资源不存在错误

我正在为Visa卡实现Google Pay推送配置功能,调用官方提供的API时意外收到404响应,提示请求的资源不存在,具体响应如下:

{
  "errorResponse": {
    "status": 404,
    "reason": "notFound",
    "message": "The requested resource does not exist."
  }
}

用相同的API_KEY和sharedSecret测试helloworld端点时,请求完全正常。当我使用无效的API_KEY、错误路径或无效sharedSecret时,均返回Token验证失败:

{
  "responseStatus": {
    "status": 401,
    "code": "9201",
    "severity": "ERROR",
    "message": "Token Validation Failed",
    "info": ""
  }
}

所有凭证和路径都正确时却抛出资源不存在错误,对此我无法理解。

以下是我发送请求的代码:

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.SignatureException;

public class Main2 {

    public static void main(String[] args) throws Exception {

        // Your apiKey and sharedSecret
        String apiKey = "API_KEY";
        String sharedSecret = "SECRET_KEY";

        // Resource path, query string, and request body
        String resourcePath = "provisioning/cardData/googlePay";
        String queryString = "apiKey=" + apiKey;
        // I'm not sure about this body, I took it just from 
        // the example provided in the API docs
        String requestBody = "{\n" +
                "  \"vCardID\": \"v-123-2448db08-8891-462d-819f-205252733e02\",\n" +
                "  \"deviceID\": \"MTcwOTIwMTMyMDA4NTIwZDZI\",\n" +
                "  \"clientCustomerID\": \"iBzxylvWTDyR8jzZd1Hdlg\",\n" +
                "  \"tokenServiceProvider\": \"V\"\n" +
                "}";

        // Create URL object
        URL url = new URL("https://sandbox.api.visa.com/inapp/provisioning/cardData/googlePay?" + queryString);

        // Open connection
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        // Set request method
        con.setRequestMethod("POST");

        // Set request headers
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Accept", "application/json");

        // Generate x-pay-token
        String xPayToken = generateXpaytoken(resourcePath, queryString, requestBody, sharedSecret);
       // System.out.println("Here is the x pay token" + xPayToken);
        con.setRequestProperty("x-pay-token", xPayToken);

        // Enable output and set request body
        con.setDoOutput(true);
        try (DataOutputStream out = new DataOutputStream(con.getOutputStream())) {
            out.write(requestBody.getBytes(StandardCharsets.UTF_8));
            out.flush();
        }

        // Get response code
        int status = con.getResponseCode();
        // System.out.println("Http Status: " + status);

        // Read response
        try (BufferedReader in = new BufferedReader(new InputStreamReader(
                status == 200 ? con.getInputStream() : con.getErrorStream()))) {
            String inputLine;
            StringBuilder content = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            System.out.println(content.toString());
        }

        // Disconnect
        con.disconnect();
    }

    public static String generateXpaytoken(String resourcePath, String queryString, String requestBody, String sharedSecret) throws SignatureException {
        String timestamp = timeStamp();
        String beforeHash = timestamp + resourcePath + queryString + requestBody;
        String hash = hmacSha256Digest(beforeHash, sharedSecret);
        return "xv2:" + timestamp + ":" + hash;
    }

    private static String timeStamp() {
        return String.valueOf(System.currentTimeMillis() / 1000L);
    }

    private static String hmacSha256Digest(String data, String sharedSecret) throws SignatureException {
        return getDigest("HmacSHA256", sharedSecret, data, true);
    }

    private static String getDigest(String algorithm, String sharedSecret, String data, boolean toLower) throws SignatureException {
        try {
            Mac sha256HMAC = Mac.getInstance(algorithm);
            SecretKeySpec secretKey = new SecretKeySpec(sharedSecret.getBytes(StandardCharsets.UTF_8), algorithm);
            sha256HMAC.init(secretKey);
            byte[] hashByte = sha256HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
            return toLower ? String.format("%032x", new BigInteger(1, hashByte)) : String.format("%032X", new BigInteger(1, hashByte));
        } catch (Exception e) {
            throw new SignatureException(e);
        }
    }
}

内容的提问来源于stack exchange,提问作者kalx11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 17:22:03