3

I'm trying to call on the Plaid test environment to integrate Stripe with Plaid using a C# client.

However I'm unable to get the token exchanged, whereas I can successfully do so with a curl client. The code is super simple.

    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;
    }

The call fails with an IO exception saying the remote party closed the connection, the inner exception message is "Authentication failed because the remote party has closed the transport stream."

Anyone have any ideas? I'm about ready to pull my hair out..

Here's the corresponding curl call that succeeds.

$ 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
Sandeep Phadke
  • 862
  • 9
  • 23
  • 1
    You might need to explicitly enable TLS 1.2 for the outbound connection to Plaid. This StackOverflow answer should help: http://stackoverflow.com/a/35321007/5307473 – Ywain Oct 27 '16 at 07:54
  • 1
    You were right! Please add that as the answer and I'll mark it so. – Sandeep Phadke Oct 28 '16 at 05:29
  • I would suggest to use Plaid.net by Tyler Jensen (search it in the NuGet Package Manager) so that you can get the same result with less lines of code. You can also see it here: https://www.nuget.org/packages/Plaid.Net/ – joalcego Feb 15 '17 at 15:23

1 Answers1

4

You're probably using .NET 4.5, in which TLS 1.2 wasn't enabled by default, so you need to explicitly enable TLS 1.2 for outbound connections in your C# code. You can do this with:

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

(Source: https://stackoverflow.com/a/35321007/5307473)

Community
  • 1
  • 1
Ywain
  • 16,854
  • 4
  • 51
  • 67