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

将C#的HMAC-SHA256授权代码转为NodeJS后无法调用外部API求助

Troubleshooting Node.js AMX Authorization Header Conversion from C#

I get it—converting auth code between languages can be tricky, especially when the external API is rigid and you can’t tweak it to match your Node.js implementation. Let’s walk through the most likely culprits that could be breaking your request, since the original C# code works fine.

First, here’s the Node.js snippet you shared (I’ll assume there’s more to it, but this gives us a starting point):

var request = require('request');
var uuid = require('node-uuid');
var CryptoJS = require('crypto-js');

// ... rest of your authorization header logic and request code

Key Areas to Audit

These are the most common mismatches between C# and Node.js auth implementations:

  • Encryption/Hash Algorithm Alignment
    Double-check that you’re using the exact same algorithm, key encoding, and padding as the C# code. For example:

    • If C# uses HMACSHA256 with a UTF-8 encoded secret, make sure CryptoJS isn’t defaulting to ASCII or another encoding.
    • C# might specify a padding mode (like PaddingMode.PKCS7) that CryptoJS handles differently—you may need to explicitly set this in Node.js if the API is strict.
  • Authorization Header Structure
    APIs often expect auth headers in a precise format (e.g., AMX <nonce>:<timestamp>:<signature> with specific casing or delimiters). Compare the header string your Node.js code generates to the one from C#—even a misplaced colon or lowercase letter could cause a rejection.

  • Nonce & Timestamp Consistency

    • node-uuid generates UUIDs, but confirm the C# code uses the same UUID version (v4 is common) and format (hyphens included or stripped). Some APIs are picky about nonce structure.
    • Timestamps are a frequent pain point: does C# use Unix seconds or milliseconds? Node.js’s Date.now() returns milliseconds, so if C# uses seconds, you’ll need to do Math.floor(Date.now() / 1000) to match. Also, ensure both are using UTC to avoid timezone mismatches.
  • Base64 Encoding Differences
    C#’s Convert.ToBase64String() and CryptoJS’s Base64 methods can handle padding or line breaks differently. If the API expects a strict Base64 format, make sure your Node.js output has no extra line breaks and uses correct padding (= characters at the end).

  • Secret Key Handling
    If your auth uses a Base64-encoded secret key, C# might decode it to a byte array before using it—make sure you’re doing the same in Node.js. Passing the raw Base64 string as the key to CryptoJS will produce a different signature than using the decoded byte array.

  • Signed Payload Exactness
    Even a single extra space or missing query parameter in the payload you’re signing will make the signature invalid. Log the exact string being signed in both C# and Node.js, then compare them character-for-character. This is often the root cause of auth failures.

To Help Debug Further

Could you share a bit more info?

  1. The full working C# code that generates the AMX header
  2. Your complete Node.js code (including how you build the header and send the request)
  3. Any error messages or HTTP status codes the API returns (e.g., 401 Unauthorized with a specific message)
  4. The expected format of the AMX authorization header (if you have docs for it)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:47:27