如何将 RestClient 与 Oauth 2.0 和客户端 + TLS 证书一起使用?



我是OAuth2.0和所有证书的新手,所以请饶了我;-)

今天,我正在尝试与银行的REST API接口。它们提供了一个很好的解释,在这里可以找到 https://developer.ing.com/openbanking/get-started 但是这些都是为Web开发而编写的(我认为),所有OAuth的东西对我来说都是新的。我创建了一个新的测试应用程序,该应用程序应连接到银行的沙盒环境。但是,我不清楚将证书放在哪里。我有四个文件;客户端证书 + 密钥和 TLS 证书 + 密钥。除此之外,我还注册了我的应用程序并收到了一个客户ID。但是,当使用沙盒环境进行测试时,它们会提供供公众使用的 ClientID(与证书相同)

这是我到目前为止的代码:

'Example: https://developer.ing.com/openbanking/get-started
'OAuth2.0 explained: https://developer.ing.com/api-marketplace/marketplace/2d00fd5f-88cd-4416-bbca-f309ebb03bfe/documentation#introduction
'Create New RestClient
Dim ClientIDSandBox As String = "e77d776b-90af-4684-bebc-521e5b2614dd"
Dim client As New RestClient("https://api.sandbox.ing.com/")
'Some code i found here: https://forums.xamarin.com/discussion/139799/call-to-tls-1-2-server-with-restsharp-works-in-console-app-not-in-xamarin-forms
ServicePointManager.Expect100Continue = True
ServicePointManager.DefaultConnectionLimit = 9999
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
client.Proxy = New WebProxy
'Create new certificate collection containing the Client and TLS certificate
Dim certColl As New X509CertificateCollection
certColl.Add(New X509Certificate("../../ING/Sandbox/example_client_tls.cer")) 'A TLS connection certificate used for setting up a mutual TLS connection
certColl.Add(New X509Certificate("../../ING/Sandbox/example_client_signing.cer")) 'A HTTP Signature certificate used for signing the token request in accordance with the Signature Header RFC
client.ClientCertificates = certColl
'Create new request
Dim request As New RestRequest("/oauth2/token", DataFormat.None)
'Add custom headers to request
request.AddHeader("Content-Type", "application/x-www-form-urlencoded")
request.AddHeader("Date", Now.ToUniversalTime().ToString("r"))
request.AddHeader("Digest", "SHA-256=47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=")
request.AddHeader("Authorization", $"Signature keyId={ClientIDSandBox}, algorithm=""rsa-sha256"", headers=""(request-target) Date digest"", signature=""{File.ReadAllText("../../ING/Sandbox/example_client_signing.cer")}""")
request.AddParameter("grant_type=client_credentials&scope=greetings%3view", ParameterType.RequestBody)
'POST the request and retrieve the server response
Dim response As IRestResponse = client.Post(request)
'Printout the response
Debug.Print(response.ErrorMessage)
Debug.Print(response.StatusCode.ToString)
Debug.Print(response.Content)

当我使用它时,我得到一个错误:Specified value contains invalid CRLF characters. Parameter name: value

试试这个 取代

request.AddParameter("grant_type=client_credentials&scope=greetings%3view", ParameterType.RequestBody)

request.AddBody("grant_type=client_credentials&scope=greetings%3view", "application/x-www-form-urlencoded")

最新更新