我正试图使用powershell在私钥的帮助下生成JWT令牌,但出现了上述错误。如果有人能帮我,我将不胜感激。我在下面发布我的代码片段:
$now = (Get-Date).ToUniversalTime()
$createDate = [Math]::Floor([decimal](Get-Date($now) -UFormat "%s"))
$expiryDate = [Math]::Floor([decimal](Get-Date($now.AddHours(1)) -UFormat "%s"))
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("C:\Users\agarni\Desktop\NetsuitSentinelIntegration.pfx","Password@12345")
$jwtheader = [Ordered]@{
alg = "RS256"
typ = "JWT"
kid = "1l68owapqcbNnMf0lc3Q0-KFqRgZi4RwA" } | ConvertTo-Json
$Jwtpayload = [ordered]@{
iss="f01b3163ec8dfd402f32d0ba14fa97da9776b817f1cc7cf08bd952"
scope="Restlets"
iat="$starttime"
exp="$Expirytime"
aud="https://api.netsuit.com/services/rest/auth/oauth2/v2/token"
} | Convertto-JSON
$rsa = get-content C:\privatekey.key -Raw
ConvertFrom-Json -InputObject $payloadJson -ErrorAction Stop | Out-Null } # Validating that the parameter is actually JSON - if not, generate breaking error
catch { throw "The supplied JWT payload is not JSON: $payloadJson" }
#>
$encodedHeader = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($jwtHeader)) -replace '\+','-' -replace '/','_' -replace '='
$encodedPayload = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Jwtpayload)) -replace '\+','-' -replace '/','_' -replace '='
$jwt = $encodedHeader + '.' + $encodedPayload # The first part of the JWT
$toSign = [System.Text.Encoding]::UTF8.GetBytes($jwt)
# Overloads tested with RSACryptoServiceProvider, RSACng, RSAOpenSsl
try { $sig = [Convert]::ToBase64String($rsa.SignData($toSign,[Security.Cryptography.HashAlgorithmName]::SHA256,[Security.Cryptography.RSASignaturePadding]::Pkcs1)) -replace '\+','-' -replace '/','_' -replace '='
$jwt = $jwt + '.' + $sig
return $jwt
Method invocation failed because [System.String] does not contain a method named 'Signdata'.
At line:1 char:1
+ $rsa.Signdata($tosign,[Security.Cryptography.HashAlgorithmName])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound