1

I'm currently working with Spreedly REST API which works with basic http authentication.

At first, I set my credentials this way:

webRequest.Credentials = new System.Net.NetworkCredential(user, password);

It worked well for many of the API resources including POSTing new gateways, GETing gateways, PUTing data on a gateway and even redacting a gateway.

I hit a wall when I tried to POST a new payment_method at their /v1/payment_methods.<format> resource.

The error I'm getting:

HTTP 422
{
  "errors": [
    {
      "key": "errors.environment_key_parameter_required",
      "message": "You must specify an environment_key parameter."
    }
  ]
}

After verifying a lot of things, I tried it using curl based on their examples and it worked.

The issue seems to be in the way I set my credentials in the HttpWebRequest even though it's the same code I use for the other working resources.

I tried using the CredentialCache because I could specify "Basic" and it failed the same way:

CredentialCache cache = new CredentialCache();
cache.Add(webRequest.RequestUri, "Basic", new System.Net.NetworkCredential(user, password));
webRequest.Credentials = cache;

1 Answer 1

1

Here's what worked:

I created my own authorization HTTP header as suggested in https://stackoverflow.com/a/13956730/5869109 and it worked well:

String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1")
.GetBytes(user + ":" + password));
webRequest.Headers.Add("Authorization", "Basic " + encoded);
Sign up to request clarification or add additional context in comments.

1 Comment

Would be great to know what's different with webRequest.Credentials compared to manually setting base64 encoded header. Nice one ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.