I’m getting invalide key from jwt.io debugger. Here is my code:
$keyName = ‘organizations/b2a5{redacted}38fff39’;
$keySecret = “-----BEGIN EC PRIVATE KEY-----\nMHcC{redacted}gC82xck8\nS+g8kHtVR0YYxm1bcOaOPYdF6udsn9lPzA==\n-----END EC PRIVATE KEY-----\n”;$requestMethod = “GET”;
$requestHost = “api.coinbase.com”;
$requestPath = “/api/v3/brokerage/accounts”;function buildJwt($uri, $keyName, $keySecret) {
$privateKeyResource = openssl_pkey_get_private($keySecret);
if (!$privateKeyResource) {
throw new Exception(‘Private key is not valid’);
}$time = time(); $nonce = bin2hex(random_bytes(16)); // Generate a 32-character hexadecimal nonce $jwtPayload = [ 'aud' => ["retail_rest_api_proxy"], 'sub' => $keyName, 'iss' => 'coinbase-cloud', 'iat' => $time, 'nbf' => $time, 'exp' => $time + 120, // Token valid for 120 seconds from now 'uri' => $uri, ]; $headers = [ 'typ' => 'JWT', 'alg' => 'ES256', 'kid' => $keyName, // Key ID header for JWT 'nonce' => $nonce // Nonce included in headers for added security ]; // Note: 'kid' is passed separately for compatibility with the library's method signature $jwtToken = JWT::encode($jwtPayload, $privateKeyResource, 'ES256', $keyName, $headers); return $jwtToken;
}
function main() {
global $requestMethod, $requestHost, $requestPath, $keyName, $keySecret;$uri = "{$requestMethod} {$requestHost}{$requestPath}"; $jwtToken = buildJwt($uri, $keyName, $keySecret); echo $jwtToken;
}