Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Last active April 22, 2016 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atifaziz/5664067 to your computer and use it in GitHub Desktop.
Save atifaziz/5664067 to your computer and use it in GitHub Desktop.
Real pre-authentication with JsonRpcClient from Jayrock against Nutshell CRM
// https://groups.google.com/forum/?fromgroups#!topic/jayrock/FQjHPb_QswU
using System;
using System.Net;
using System.Text;
static class Program
{
static void Main()
{
var client = new JsonRpcClient
{
Url = "https://app01.nutshell.com/api/v1/json",
PreAuthenticate = true,
Credentials = new NetworkCredential("jim@demo.nutshell.com", "43c789d483fd76547b1f157e3cf5e580b95b9d8c"),
};
var lead = client.Invoke(typeof(object), "getLead", new { leadId = 1000 });
Console.WriteLine(lead);
}
}
public class JsonRpcClient : Jayrock.JsonRpc.JsonRpcClient
{
protected override WebRequest GetWebRequest(Uri url)
{
var request = base.GetWebRequest(url);
NetworkCredential creds;
if (PreAuthenticate && null != (creds = Credentials.GetCredential(url, "Basic")))
{
var basicCreds = creds.UserName + ":" + creds.Password;
var credsBytes = Encoding.UTF8.GetBytes(basicCreds);
var encodedCreds = Convert.ToBase64String(credsBytes);
request.Headers["Authorization"] = "Basic " + encodedCreds;
}
return request;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment