+52 55 4336 6356, +52 55 4336 6368 contacto@semantiq.com.mx

Ejemplo de intregración con API REST de SugarCRM codificado en C#. Este ejemplo consta de 2 pasos:

Autentificación

string urlAPISugarCRM = "https://instanciasugarcrm.sugarondemand.com/rest/v11";
string id = "22ec0852-3b70-11e9-afbc-065aa0ec81fc"; // ID SugarCRM Orden de Servicio
string username = "username";
string password = "username_password";

string auth_url = urlAPISugarCRM + "/oauth2/token";
Oauth2_Token_Arguments oauth2_token_arguments = new Oauth2_Token_Arguments();
oauth2_token_arguments.grant_type = "password";
oauth2_token_arguments.client_id = "sugar"; 
oauth2_token_arguments.client_secret = "";
oauth2_token_arguments.username = username;
oauth2_token_arguments.password = password;
oauth2_token_arguments.platform = "semantiq"; 

string data = new JavaScriptSerializer().Serialize(oauth2_token_arguments);
 
WebRequest myReq = WebRequest.Create(auth_url);
myReq.Method = "POST";
myReq.ContentLength = data.Length;
myReq.ContentType = "application/json; charset=UTF-8";
UTF8Encoding enc = new UTF8Encoding();
myReq.Headers.Remove("auth-token");
using (Stream ds = myReq.GetRequestStream())
{
    ds.Write(enc.GetBytes(data), 0, data.Length);
}            
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();

// Esta forma de obtener el token se puede mejorar ;)
string[] aux = content.Split(',');
string auth2 =  aux[0].Split(':')[1].Replace ("\"", ""); 

Actualización de un registro del modódulo A001_ordenservicio

OrdenServicio os = new OrdenServicio();

os.id = id;
os.name = "15-R2-Validacion_Tecnica-Aquarium Seafood SA de CV";
os.description = "Vidrios traseros con falla...";
os.os_orden_servicio_number = 15;

data = new JavaScriptSerializer().Serialize(os);

myReq = WebRequest.Create(urlAPISugarCRM + "/A001_ordenservicio/" + id);
myReq.Method = "PUT";
myReq.ContentLength = data.Length;
myReq.ContentType = "application/json; charset=UTF-8;";
myReq.Headers.Add("oauth-token", auth2);

enc = new UTF8Encoding();
myReq.Headers.Remove("auth-token");
using (Stream ds = myReq.GetRequestStream())
{
    ds.Write(enc.GetBytes(data), 0, data.Length);
}            
wr = myReq.GetResponse();
receiveStream = wr.GetResponseStream();
reader = new StreamReader(receiveStream, Encoding.UTF8);
content = reader.ReadToEnd();  

Código completo

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

namespace demotest
{
    public class Oauth2_Token_Arguments
    {
        public string grant_type;
  public string client_id; 
  public string client_secret;
  public string username;
  public string password;
  public string platform;         
    }

    public class OrdenServicio
    {

  public string id;
  public string name;
  public string description;
  public int os_orden_servicio_number;

    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(""); 
            Console.WriteLine("SugarCRM : Prueba de actualización de un registro");

            string urlAPISugarCRM = "https://instanciasugarcrm.sugarondemand.com/rest/v11";
            string id = "22ec0852-3b70-11e9-afbc-065aa0ec81fc"; // ID SugarCRM Orden de Servicio
            string username = "username";
            string password = "username_password";

            string auth_url = urlAPISugarCRM + "/oauth2/token";
     Oauth2_Token_Arguments oauth2_token_arguments = new Oauth2_Token_Arguments();
     oauth2_token_arguments.grant_type = "password";
     oauth2_token_arguments.client_id = "sugar"; 
     oauth2_token_arguments.client_secret = "";
     oauth2_token_arguments.username = username;
     oauth2_token_arguments.password = password;
     oauth2_token_arguments.platform = "semantiq"; 

     string data = new JavaScriptSerializer().Serialize(oauth2_token_arguments);
   
            WebRequest myReq = WebRequest.Create(auth_url);
            myReq.Method = "POST";
            myReq.ContentLength = data.Length;
            myReq.ContentType = "application/json; charset=UTF-8";
            UTF8Encoding enc = new UTF8Encoding();
            myReq.Headers.Remove("auth-token");
            using (Stream ds = myReq.GetRequestStream())
            {
                ds.Write(enc.GetBytes(data), 0, data.Length);
            }            
            WebResponse wr = myReq.GetResponse();
            Stream receiveStream = wr.GetResponseStream();
            StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
            string content = reader.ReadToEnd();


            string[] aux = content.Split(',');
     string auth2 =  aux[0].Split(':')[1].Replace ("\"", "");


      Console.WriteLine("");
            Console.WriteLine("AUTENTIFICACIÓN - Response:\n" + content);
      Console.WriteLine("");
      Console.WriteLine("access_token: " + auth2);


      Console.WriteLine("");
     Console.WriteLine("ACTUALIZACIÓN ORDEN SERVICIO: " + id);

     OrdenServicio os = new OrdenServicio();

     os.id = id;
            os.name = "15-R2-Validacion_Tecnica-Aquarium Seafood SA de CV";
            os.description = "Vidrios traseros con falla...";
            os.os_orden_servicio_number = 15;

     data = new JavaScriptSerializer().Serialize(os);

       myReq = WebRequest.Create(urlAPISugarCRM + "/A001_ordenservicio/" + id);
            myReq.Method = "PUT";
            myReq.ContentLength = data.Length;
            myReq.ContentType = "application/json; charset=UTF-8;";
     myReq.Headers.Add("oauth-token", auth2);

            enc = new UTF8Encoding();
            myReq.Headers.Remove("auth-token");
            using (Stream ds = myReq.GetRequestStream())
            {
                ds.Write(enc.GetBytes(data), 0, data.Length);
            }            
            wr = myReq.GetResponse();
            receiveStream = wr.GetResponseStream();
            reader = new StreamReader(receiveStream, Encoding.UTF8);
            content = reader.ReadToEnd();

      Console.WriteLine("");
            Console.WriteLine("ORDEN SERVICIO " + id + " - Response:\n" + content);

        }
    }
}

Tambien contamos con el Ejemplo API REST Phyton Flask