Alright… I’ve put in some work on this today but I’m still running into that pesky 401 error. It says “signature invalid” in the ‘respString’ variable. I’ve tried adapting the “CB-ACCESS-SIGN” header into a form that is acceptable, as is more or less recommended here: Proper REST signatures; however, it is not working.
Here’s my code:
public async Task BuyBitcoin()
{
HttpResponseMessage resp = await _client.GetAsync("https://api.coinbase.com/v2/time");
string timestamp = await resp.Content.ReadAsStringAsync();
TimeRespJson json = JsonConvert.DeserializeObject<TimeRespJson>(timestamp);
using (var request = new HttpRequestMessage(HttpMethod.Post, "https://api.exchange.coinbase.com/orders"))
{
object json2 = new { name = "John Doe", age = 33 };
string jsonStringified = JsonConvert.SerializeObject(json2);
var signature = json.data.epoch.ToString() + "POST" + "/orders" + jsonStringified;
request.Headers.Add("CB-ACCESS-KEY", "XXXXXXXXXXXXXXXX");
request.Headers.Add("CB-ACCESS-TIMESTAMP", json.data.epoch.ToString());
request.Headers.Add("CB-ACCESS-SIGN", signature);
request.Headers.Add("CB-ACCESS-PASSPHRASE", "XXXXXXXXXXXXXXX");
request.Headers.Add("User-Agent", "request");
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //ACCEPT header
request.Content = new StringContent(jsonStringified, Encoding.UTF8, "application/json"); //CONTENT-TYPE header
HttpResponseMessage orderResponse = await _client.SendAsync(request);
string respString = await orderResponse.Content.ReadAsStringAsync();
}
Keep in mind that “_client” is an HttpClient object instance.