I'm trying to use a bible API in an application. Here is the following Curl Call which I have to make
curl -L -u #{API Token}:X -k https://bibles.org/v2/verses/eng-GNTD:Acts.8.34.xml
- -L option tells cURL to follow redirects.
- -u option to specify a username and password with HTTP Basic authentication. API token is given as the username. The password is ignored by the server, so you can put whatever you want; In our case "X".
- -k option is used to tell cURL to not verify the SSL certificate.
Now the documentation gives us a PHP code for using this API which is as below
<?php
$token = '#{API Token}';
$url = 'https://bibles.org/v2/verses/eng-GNTD:Acts.8.34.xml';
// Set up cURL
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// don't verify SSL certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Return the contents of the response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Set up authentication
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$token:X");
// Do the request
$response = curl_exec($ch);
curl_close($ch);
print($response);
?>
I have tried changing this in C# to the following. However, I get a 401(Unauthorized code). What am i doing wrong?
string apiToken = "#{myAPIToken}";
string url = "https://bibles.org/v2/verses/eng-GNTD:Acts.8.34.xml";
WebRequest myReq = WebRequest.Create(url);
CredentialCache mycache = new CredentialCache();
myReq.Headers["Authorization"] = Convert.ToBase64String(Encoding.ASCII.GetBytes(apiToken + ":X"));
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();