Instantly share code, notes, and snippets.

@ToJans /Program.cs Secret
Last active May 17, 2017

Embed
What would you like to do?
Example console application that downloads leads from vslweb and dumps it to the console
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Text;
namespace VSL.Examples
{
public class Program
{
public static void Main(string[] args)
{
var client = new AuthorizedHttpClient(username: "a@b.com", password: "xyz");
// request leads for
var storage = "vsl";
var tenant = "vsl";
var from = "20170401"; // YYYYMMDD
var to = "20170430"; // YYYYMMDD
var url =
"https://vslweb.azurewebsites.net/r/" +
storage + "/" + tenant +
"/nl/reporting/data/details?From=" +
from + "&To=" + to;
var json = client.GetStringAsync(url).Result;
Console.WriteLine(json);
Console.ReadKey();
}
/// <summary>
/// A http client where the default headers contain the necessary JSON Web token (JWT) in the header.
/// </summary>
public class AuthorizedHttpClient : HttpClient
{
public AuthorizedHttpClient(string username, string password)
{
// always accept JSON format
base.DefaultRequestHeaders.Add("Accept", "application/json");
var tokenRequestUrl = "https://virtualsaleslab.eu.auth0.com/oauth/ro";
var tokenRequestJSON = JsonConvert.SerializeObject(new
{
client_id = "2NGDZHG76TdaNxeupbPZhU4HgPTDBmO0",
username = username,
password = password,
connection = "Username-Password-Authentication",
scope = "openid app_metadata"
});
var tokenRequestContent = new StringContent(tokenRequestJSON, Encoding.UTF8, "application/json");
// request a JWT (JSON Web Token)
var tokenResponseMessage = base.PostAsync(tokenRequestUrl, tokenRequestContent).Result;
// parse the json response text
string tokenResponseText = tokenResponseMessage.Content.ReadAsStringAsync().Result;
var tokenResponse = JObject.Parse(tokenResponseText);
// extract the token from the response
var jwtToken = tokenResponse["id_token"].ToString(); ;
// add the authorization header with the token to the default request headers
base.DefaultRequestHeaders.Add("Authorization", "Bearer " + jwtToken);
}
}
}
}
#!/bin/bash
username="a@b.com"
password="pwd"
storage="vsl"
tenant="vsl"
from="20170401"
to="20170430"
data='{"client_id":"2NGDZHG76TdaNxeupbPZhU4HgPTDBmO0", "username":"'
data=$data$username'", "password":"'
data=$data$password'", "connection":"Username-Password-Authentication", "scope":"openid app_metadata"}'
token=$(curl \
--request POST \
--url 'https://virtualsaleslab.eu.auth0.com/oauth/ro' \
--header 'content-type: application/json' \
--data "$data" \
| grep -Po '"id_token":"[^\"]*' \
| grep -Po '[^\"]*$' )
authheader="Authorization: Bearer $token"
url="https://vslweb.azurewebsites.net/r/$storage/$tenant/nl/reporting/data/details?From=$from&To=$to"
curl -H "$authheader" "$url"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment