关于在 Plaid API 上使用 .net 客户端调用 c#:Exchange_Token 失败并出现异常

Exchange_Token call with .net client on Plaid API failing with exception

我正在尝试调用 Plaid 测试环境以使用 C# 客户端将 Stripe 与 Plaid 集成。

但是我无法交换令牌,而我可以使用 curl 客户端成功完成。代码超级简单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    public async Task<string> ExchangeToken(string publicToken, string accountId)
    {
        string bankAccountToken = null;

        try
        {
            var parameters = new Dictionary<string, string>()
            {
                {"client_id", this.clientId},
                {"secret", this.clientSecret},
                {"public_token", publicToken},
                {"account_id", accountId}
            };

            using (var client = new HttpClient())
            {
                var request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(this.BaseAddress,"exchange_token"),
                    Method = HttpMethod.Post,
                    Content = new FormUrlEncodedContent(parameters)

                };


                var requestResult = await client.SendAsync(request);
                var stringResponse = requestResult.Content.ReadAsStringAsync();
            }



        }
        catch (Exception ex)
        {
            Trace.TraceError("PlaidHelper::ExchangeToken() hit error" + ex.Message);
        }

        return bankAccountToken;
    }

调用失败,IO异常表示远程方关闭了连接,内部异常消息是"Authentication failed because the remote party has closed the transport stream."

有人有什么想法吗?我准备把我的头发拉出来..

这是成功的相应 curl 调用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ curl -v https://tartan.plaid.com/exchange_token -d client_id="XXXXX" -d
secret="XXXXXXXXXXXXXXXX" -d public_token="XXXXXXXXXXX" -d account_id="XXXXX"

> POST /exchange_token HTTP/1.1
> Host: tartan.plaid.com
> User-Agent: curl/7.50.3
> Accept: */*
> Content-Length: 263
> Content-Type: application/x-www-form-urlencoded
>
} [263 bytes data]
* upload completely sent off: 263 out of 263 bytes
{ [5 bytes data]
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Date: Thu, 27 Oct 2016 04:55:53 GMT
< Vary: X-HTTP-Method-Override, Accept-Encoding
< X-Request-Id: 65af9ef1009c46f5b9f484b5c6aee975
< Content-Length: 295
< Connection: keep-alive
<
{ [295 bytes data]
* Curl_http_done: called premature == 0
100   558  100   295  100   263    262    233  0:00:01  0:00:01 --:--:--   273{
 "access_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
 "account_id":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
 "stripe_bank_account_token":"btok_XXXXXXXXXXX"
}
* Connection #0 to host tartan.plaid.com left intact


您可能正在使用 .NET 4.5,其中默认未启用 TLS 1.2,因此您需要在 C# 代码中为出站连接显式启用 TLS 1.2。你可以这样做:

1
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls

(来源:https://stackoverflow.com/a/35321007/5307473)