Skip to content

Instantly share code, notes, and snippets.

@elchele
Last active November 23, 2020 06:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elchele/e4be253a38e2223add1fdc642fb4277e to your computer and use it in GitHub Desktop.
Save elchele/e4be253a38e2223add1fdc642fb4277e to your computer and use it in GitHub Desktop.
Base C# class for interacting with Sugar 7 REST v10 API
/* SugarCRM v7 REST API Wrapper
*
* Date: May 13, 2014
*
* Author: Angel Magaña
*
* Contact: cheleguanaco[at]cheleguanaco.com
* cheleguanaco.com
*
*
* Description: Simplifies communication with REST API.
* Translates more common .NET calls to "Sugar-ese."
*
* Copyright 2009 - 2014 Angel Magaña
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Net;
namespace CandyWrapper7
{
public class CW7
{
public string sBaseURL = string.Empty;
public Dictionary<string, string> doLogin(Dictionary<string, string> diParams)
{
//Request an OAuth Token from Sugar
Dictionary<string, string> diReturn = new Dictionary<string,string>();
//Build the JSON
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonTextWriter writer = new JsonTextWriter(sw);
writer.WriteStartObject();
//Iterate through keys and values in dictionary
foreach (KeyValuePair<string, string> item in diParams)
{
writer.WritePropertyName(item.Key);
writer.WriteValue(item.Value);
}
writer.WriteEndObject();
string sJSON = sb.ToString();
//Prepare parameters for request
diParams.Add("verb", "POST");
diParams.Add("endpoint", "/oauth2/token");
diParams.Add("body", sJSON);
//Send request
string sJSONReturn = doExecRequest(diParams);
//Handle return
diReturn = JsonConvert.DeserializeObject<Dictionary<string, string>>(sJSONReturn);
return diReturn;
}
public Dictionary<string, string> doLogout(Dictionary<string, string> diParams)
{
diParams.Add("verb", "POST");
diParams.Add("endpoint", "/oauth2/logout");
string sJSONReturn = doExecRequest(diParams);
Dictionary<string, string> diReturn = JsonConvert.DeserializeObject<Dictionary<string, string>>(sJSONReturn);
return diReturn;
}
public Dictionary<string, object> doCreateRecord(Dictionary<string, string> diParams)
{
string sModule = "/" + diParams["module"];
//Build the JSON
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonTextWriter writer = new JsonTextWriter(sw);
writer.WriteStartObject();
//Iterate through keys and values in dictionary
foreach (KeyValuePair<string, string> item in diParams)
{
writer.WritePropertyName(item.Key);
writer.WriteValue(item.Value);
}
writer.WriteEndObject();
string sJSON = sb.ToString();
diParams.Add("verb", "POST");
diParams.Add("endpoint", sModule);
diParams.Add("body", sJSON);
string sJSONReturn = doExecRequest(diParams);
//Handle return
Dictionary<string, object> diReturn = JsonConvert.DeserializeObject<Dictionary<string, object>>(sJSONReturn);
return diReturn;
}
private string doExecRequest(Dictionary<string, string> diParams)
{
string sReturn = string.Empty;
string sVerb = string.Empty;
string sEndPoint = string.Empty;
string sBody = string.Empty;
string sOAuthToken = string.Empty;
//Set the params
sVerb = diParams["verb"];
sEndPoint = sBaseURL + diParams["endpoint"];
//Check if there is a body. Not all endpoints require one
if (diParams.ContainsKey("body"))
{
sBody = diParams["body"];
}
//Prepare and execute the request
Byte[] byteArray = Encoding.UTF8.GetBytes(sBody);
WebRequest wRequest = WebRequest.Create(sEndPoint);
//Define headers and other request params
if (diParams.ContainsKey("oauthtoken"))
{
sOAuthToken = diParams["oauthtoken"];
wRequest.Headers.Add("OAuth-Token", sOAuthToken);
}
wRequest.Method = sVerb;
wRequest.ContentLength = byteArray.Length;
wRequest.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
StreamWriter wWriter = new StreamWriter(wRequest.GetRequestStream());
wWriter.Write(sBody);
wWriter.Close();
WebResponse wResponse = wRequest.GetResponse();
StreamReader reader = new StreamReader(wResponse.GetResponseStream());
sReturn = reader.ReadToEnd();
return sReturn;
}
}
}
@islam-ashraful
Copy link

islam-ashraful commented Jan 10, 2017

Hi,

I have tried your code but can't login. Getting 404 "not found".
Call login method as below.
sBaseURL = "http://******.trial.sugarcrm.eu/rest/v10";

Dictionary<string, string> sugarCred = new Dictionary<string, string>();
sugarCred.Add("grant_type", "password");
sugarCred.Add("client_id", "sugar");
sugarCred.Add("client_secret", "");
sugarCred.Add("username", "
");
sugarCred.Add("password", "
*****");
sugarCred.Add("platform", "custom_api");

        CW7 sugar = new CW7();
        sugar.doLogin(sugarCred);

Can you point me what I did wrong.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment