我正在尝试将 Spotify PKCE 授权与 Siri Shortcuts 一起使用。不幸的是,我找到的解决方案都不适用于我的具体情况。我有这段代码而且我真的不知道我在做什么。基本上我需要一个字符串的 SHA256 哈希,但这需要按字节与十六进制计算。然后需要对其进行 base64Url 编码。我已经在堆栈上尝试了大多数解决方案,但我似乎无法将最终产品输出到网页上,这是我能够在 iPhone 上本地运行 Java 脚本的主要方式。任何帮助将不胜感激
<script>
function sha256(plain) {
// returns promise ArrayBuffer
const encoder = new TextEncoder();
const data = encoder.encode(plain);
return window.crypto.subtle.digest('SHA-256', data);
}
function base64urlencode(a) {
// Convert the ArrayBuffer to string using Uint8 array.
// btoa takes chars from 0-255 and base64 encodes.
// Then convert the base64 encoded to base64url encoded.
// (replace + with -, replace / with _, trim trailing =)
return btoa(String.fromCharCode.apply(null, new Uint8Array(a)))
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
async function pkce_challenge_from_verifier(v) {
hashed = await sha256(v);
base64encoded = base64urlencode(hashed);
return base64encoded;
}
const code = await pkce_challenge_from_verifier("Zg6klgrnixQJ629GsawRMV8MjWvwRAr-vyvP1MHnB6X8WKZN")
document.getElementById("value").innerHTML = code;
</script>
<body>
<p id="value"></p>
</body>
</html> ```