uncategorized
pete_onepagecrm at June 3rd, 2014 04:06 — #1
Continuing the discussion from Welcome to the OnePageCRM Developer Forum:
pete_onepagecrm at June 3rd, 2014 04:16 — #2
Hi @divakarshukla20 thanks for getting in touch.
Your user ID and API key are returned after you do a post
to the login.json
endpoint of the API with your login credentials. The response is something like this
{
"status": 0,
"message": "OK",
"timestamp": 1401783346,
"data": {
"user_id": "52xxxbeb899234e4xxxxxxe",
"auth_key": "JNcZSixxxx/Le51ksfsdoVesdfaxxxx8Iu7YFxxxg=",
...
}
Check out the documentation on developer.onepagecrm.com, in particular the authentication and signing process parts to get started.
There's a C# sample available under the signing process part.
Please let me know if you have any more specific questions as you go forward.
We're not .NET experts here but perhaps you and @rjhillyer might be able to help each other out, with support from our team of course.
rjhillyer at June 3rd, 2014 12:28 — #3
I have some code here for .Net. It is the best help I can offer. It took LOTS of trial and error to get it to this point, So anything past this, will probably just be more trial and error again. We can both be .Net Pioneers!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;`
private string make_api_call(string url, string http_method, string[] post_data, string protocol = "", string uid = "", string key = "")
{
string full_url = "https://app.onepagecrm.com/api/" + url;
UTF8Encoding encoding = new UTF8Encoding();
byte[] bData = { };
string[] auth_data;
if (http_method == "POST" || http_method == "PUT")
{
auth_data = new string[5];
bData = encoding.GetBytes(queryBuilder(post_data, protocol));
}
else
{
auth_data = new string[4];
}
if (uid != "")
{
auth_data[0] = uid;
auth_data[1] = getUnixTime();
auth_data[2] = http_method;
auth_data[3] = makeSH1String(full_url);
if (http_method == "POST" || http_method == "PUT")
{
auth_data[4] = makeSH1String(queryBuilder(post_data, protocol));
}
}
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(full_url);
httpWReq.Headers.Add("X-OnePageCRM-UID", auth_data[0]);
httpWReq.Headers.Add("X-OnePageCRM-TS", auth_data[1]);
httpWReq.Headers.Add("X-OnePageCRM-Auth", makeSHA1256(implode(auth_data), key));
httpWReq.Method = http_method;
httpWReq.ContentType = protocol;
httpWReq.ContentLength = bData.Length;
//fake the SSL certificate
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;//Can pick one or the other if it doesn't work. SSL3 alone doesn't seem to work well.
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(AllwaysGoodCertificate);
string responseString = "";
if (http_method == "POST" || http_method == "PUT")
{
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(bData, 0, bData.Length);
}
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString;
}
private string implode(string[] arr)
{
string result = "";
for (int i = 0; i < arr.Length; i++)
{
result += (i == 0) ? arr[i] : "." + arr[i];
}
return result;
}
private string makeSHA1256(string strData, string key)
{
byte[] keyByte = System.Convert.FromBase64String(key);
var vMidway = new System.Security.Cryptography.HMACSHA256(keyByte);
vMidway.ComputeHash(Encoding.UTF8.GetBytes(strData));
string result = "";
foreach (byte bTemp in vMidway.Hash)
{
result += bTemp.ToString("X2").ToLower();
}
return result;
}
private string makeSH1String(string strData)
{
byte[] midwayByte = System.Security.Cryptography.SHA1.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(strData));
string result = "";
foreach (byte bTemp in midwayByte)
{
result += bTemp.ToString("X2").ToLower();
}
return result;
}
private string queryBuilder(string[] queryArray, string protocol)
{
string result = "";
if (protocol != "application/x-www-form-urlencoded")
{
//json
result = "{";
for (int i = 0; i < queryArray.Length; i += 2)
{
if (queryArray[i + 1] != "")
{
if (!result.Contains(":"))
{
result += "\"" + queryArray[i] + "\": \"" + queryArray[i + 1] + "\"";
}
else
{
result += "," + "\"" + queryArray[i] + "\": \"" + queryArray[i + 1] + "\"";
}
}
}
result += "}";
}
else
{
//http
for (int i = 0; i < queryArray.Length; i += 2)
{
if (queryArray[i + 1] != "")
{
if (!result.Contains("="))
{
result += queryArray[i] + "=" + HttpUtility.UrlEncode(queryArray[i + 1]);
}
else
{
result += "&" + queryArray[i] + "=" + HttpUtility.UrlEncode(queryArray[i + 1]);
}
}
}
}
return result;
}
private string getUnixTime()
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = DateTime.UtcNow - origin;
return Convert.ToString(Math.Floor(diff.TotalSeconds));
}
public string[] OPlogin(string staffUsername="", string staffPassword="")
{
//Login
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("https://app.onepagecrm.com/api/auth/login.json");
//string[] parameters = { "login", _Username, "password", _Password };
if (staffUsername == "") { staffUsername = Some_Default_Username; }
if (staffPassword == "") { staffPassword = Some_Default__Password; }
string[] parameters = { "login", staffUsername, "password", staffPassword };
string result = make_api_call("auth/login.json", "POST", parameters, "application/x-www-form-urlencoded");
dynamic dJason = JsonConvert.DeserializeObject(result);
string strUID = "";
string strKEY = "";
foreach (var elements in dJason["data"])
{
if ((elements).Name == "uid")
{ strUID = (elements).Value.ToString(); }
if ((elements).Name == "key")
{
strKEY = (elements).Value.ToString();
break;
}
};
string[] keys = { strUID, strKEY };
CreateLogFiles createLogFiles = new CreateLogFiles();
createLogFiles.ErrorLog(HttpContext.Current.Server.MapPath("~/log/OnePageAPI/keys/"), "Key: " + keys[1].ToString());
createLogFiles = null;
return keys;
}
public void createOPNote(string CID, string UserName, string Password, string strNote, string strDate)
{
try
{
string[] OPkeys = OPlogin(UserName, Password);
string[] TicketArr = { "cid", CID, "body", strNote, "date", strDate };//strDate like "15.03.2013", CID can be found in the URL in OnePage, or found by API, and stored in a table
string result = make_api_call("notes.json", "POST", TicketArr, "application/x-www-form-urlencoded", OPkeys[0], OPkeys[1]);
}
catch (Exception ex)
{
}
}
public void createOPUser(int userID)
{
try
{
string[] OPkeys = OPlogin();
string[] dataArray = GetInfoFromUserID(userID);//or other way of getting info. Like {"firstname","John","lastname","Doe","emails", "home|johndoe@site.com"}
string result = make_api_call("contacts.json", "POST", dataArray, "application/x-www-form-urlencoded", OPkeys[0], OPkeys[1]);
string CID = "";
dynamic dJason = JsonConvert.DeserializeObject(result);
int status = Convert.ToInt32(dJason["status"].Value);
if (status != 0)
{
//error handling code
}
foreach (var elements in dJason["data"])
{
if ((elements).Name == "id")
{
CID = (elements).Value.ToString();
break;
}
}
CodeToStoreCID(userID, CID);
}
catch (Exception ex)
{
}
}
public void NextAction(int userID, string strAction, string strDate)
{
string[] OPkeys = OPlogin();
string CID = FindCID(userID);
string[] dataArr = { "cid", CID, "date", strDate, "name", strAction };
string result = make_api_call("actions.json", "POST", dataArr, "application/x-www-form-urlencoded", OPkeys[0], OPkeys[1]);
}
private static bool AllwaysGoodCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors)
{
return true;
}
pete_onepagecrm at June 5th, 2014 04:08 — #4
Thanks for that Bob. @divakarshukla20 please note that Bob is using version 2 of our API - you'll have to make some changes to his code to use version 3.
In particular, change https://app.onepagecrm.com/api/auth/login.json
to https://app.onepagecrm.com/api/v3/login.json
and change /api/
to /api/v3
in any resource url.
divakarshukla20 at June 9th, 2014 09:40 — #5
Thnaks for reply , kindly tell me how to create deal, i am not able to create deal from c#
.
pete_onepagecrm at June 9th, 2014 12:25 — #6
You'll need to make a POST
request to https://app.onepagecrm.com/api/v3/contacts/<contact_id>/deals.json
with body similar to this data:
{ "amount": 5130.0,
"author": "liam k.",
"date": "2013-09-14",
"expected_close_date": "2013-10-15",
"months": 1,
"name": "September deal",
"stage": 50,
"status": "pending",
"text": "Sent him estimate today.. fingers crossed!\n",
}
This is the relevant part of our documentation site - http://developer.onepagecrm.com/#deals
divakarshukla20 at June 10th, 2014 03:39 — #7
this is giving 400 bad request
i use this formet: https://app.onepagecrm.com/api/v3/contacts/5396b644aa76207d40000005/deals.json
and json is :
{ "amount": 5130.0,
"close_date": null,
"date": "2013-09-14",
"name": "September deal",
}
what is issue, please reply
liam at June 10th, 2014 07:27 — #8
@divakarshukla20,
The close_date
field is incorrect and shouldn't be included in your Post body as shown in the documentation. You probably meant closed_deal
but this is a read only attribute and is set automatically when a deal is marked as won using the status
field.
Incase you are still having problems, ensure that you are able to create a deal within the app as it is possible that a trial account can reach it's upper limit of contacts and deals quite quickly when testing out an API integration.
When creating an API integration I strongly urge you to look at the errors we send for Failed Requests. I'm not a c#/.net coder so I'm not familiar with how http request exceptions are caught and displayed, but this StackOverflow question should lead you in the right direction.
I hope this helps,
-Liam
pete_onepagecrm at June 11th, 2014 04:40 — #9
Sorry @divakarshukla20, my bad. Thanks Liam
You can suppress the HTTP response status if required by adding a suppress_http_status=1
parameter to the end of your call. This will return a HTTP status of 200
, but the response body will contain response codes and error detail.
I think this could be useful for you.
Powered by Discourse, best viewed with JavaScript enabled