RestAPi, Clientsecret, ClientId med Basic Auth
Hej
Jag ska prata med ett API, beskrivningen jag har är följande:
The Customer uses HTTP Authorization header to pass authentication information to all endpoints. The Authorization header has the following format:
Authorization: Basic ClientID:ClientSecret (base64token encoded)
The HTTP request containing an Authorization header is shown below:
GET https://api.*****se/bananer/v1/health HTTP/1.1
Authorization Basic *****************************
Detta borde vara enkelt och är det säkert för en som jobbar med sånt här, men jag får det ej att fungera. (det blir en timeout)
Jag vill ha lösningen i C# eller Powershell, jag försökte med C# och javascript.
C# försök, kod baserad på https://gist.github.com/bryanbarnard/8102915#file-simplehttpc...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
namespace HTTP_Test
{
class program
{
static void Main()
{
Task t = new Task(HTTP_GET);
t.Start();
Console.ReadLine();
}
static async void HTTP_GET()
{
var TARGETURL = "https://api.*****.se/bananer/v1/health";
/// The client information used to get the OAuth Access Token from the server.
string clientId = "***********";
string clientSecret = "************";
Console.WriteLine("GET: + " + TARGETURL);
// ... Use HttpClient.
HttpClient client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes(clientId + ":" + clientSecret);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpResponseMessage response = await client.GetAsync(TARGETURL);
HttpContent content = response.Content;
// ... Check Status Code
Console.WriteLine("Response StatusCode: " + (int)response.StatusCode);
// ... Read the string.
string result = await content.ReadAsStringAsync();
// ... Display the result.
if (result != null &&
result.Length >= 50)
{
Console.WriteLine(result.Substring(0, 50) + "...");
}
}
}
}
Javascript försök
<div>
<div id="response">
</div>
<input type="button" class="btn btn-primary" value="Call Web API" onclick="javascript:CallWebAPI();" />
<script>
function CallWebAPI() {
// New XMLHTTPRequest
var request = new XMLHttpRequest();
request.open("GET", "https://api.*****.se/bananer/v1/health", false);
var clientId = '*************';
var clientSecret = '*************';
var encodedData = window.btoa(clientId + ':' + clientSecret);
var authorizationHeaderString = 'Authorization: Basic ' + encodedData;
alert(authorizationHeaderString);
request.setRequestHeader("Authorization", authorizationHeaderString);
request.send();
// view request status
alert(request.status);
response.innerHTML = request.responseText;
}
</script>
Någon som har något tips.. Det hade varit enkelt om jag hade kunnat lämnat ut lösenord, men livet är inte alltid enkelt..