使用 URLSearchParams 防止百分比编码

sky*_*erl 11 javascript oauth node.js typescript angular

我正在尝试使用 Node URL 和 URLSearchParams API 来使 URL 构建更简单一些。我用它来构建一个像这样的 URL:

https://<ye olde oauth host>/oauth/authorize?response_type=code&client_id=<my client id>&redirect_uri=https://localhost:4200&scopes=scope1%20scope2
Run Code Online (Sandbox Code Playgroud)

但是,我下面的代码创建的 URL 如下所示:

https://<ye olde oauth host>/oauth/authorize?response_type=code&client_id=<my client id>&redirect_uri=https%3A%2F%2Flocalhost%3A4200&scopes=scope1%20scope2
Run Code Online (Sandbox Code Playgroud)

据我了解,URLSearchParams API 将对字符串进行百分比编码,但如果我不希望对它们进行编码(例如 URL)怎么办?这是我的代码:

const loginURL = new URL('https://<ye olde oauth host>');
    url.pathname = 'oauth/authorize';
    url.search = new URLSearchParams({
      response_type: 'code',
      client_id: '<my client id>'
    }).toString();
loginURL.searchParams.append('redirect_uri', redirectURI);
loginURL.searchParams.append('scopes', scopes);
Run Code Online (Sandbox Code Playgroud)

我不希望对它redirect_uri进行百分比编码的原因是接收端的 OAuth API 不知道如何解析它。有什么方法可以使用 URLSearchParams 来阻止它编码吗?

Yav*_*GPT 5

我也有类似的问题。某些百分比符号导致我发送令牌时遇到麻烦(无法正确解析令牌)。最后我只是decodeURIComponent(url)在发送请求之前使用。