Bitrix24 Rest API post 请求使用 ASP.NET

Bitrix24 Rest API post request using ASP.NET

Bitrix24 CRM have webhook functional 将潜在客户(客户)添加到 CRM。所有文档都写在php上,但我想用ASP.NET。以下是他们在 php 上的做法:

$queryUrl  = 'https://restapi.bitrix24.ru/rest/1/31uhq2q855fk1foj/crm.lead.add.json';
$queryData = http_build_query(array(
    'fields' => array(
        "TITLE" => "NEW LEAD"
    ),
    'params' => array(
        "REGISTER_SONET_EVENT" => "Y"
    )
));

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_SSL_VERIFYPEER => 0,
    CURLOPT_POST => 1,
    CURLOPT_HEADER => 0,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $queryUrl,
    CURLOPT_POSTFIELDS => $queryData
));

$result = curl_exec($curl);
curl_close($curl);

$result = json_decode($result, 1);

我正在尝试使用 ASP.NET 做同样的事情,但收到错误 400 作为响应。我几乎可以肯定问题出在请求参数中,第 const string data = @"[{""fields"":{""title"":""Test""}}]"; 行。我尝试了很多组合,但没有任何效果。

const string url = @ "https://companyname.bitrix24.ru/rest/14/31uhq2q855fk1foj/crm.lead.add.json";
const string data = @"[{""fields"":{""title"":""Test""}}]";

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
byte[] cred = Encoding.UTF8.GetBytes("email:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

HttpContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage messge = client.PostAsync(url, content).Result;
string description;
if (messge.IsSuccessStatusCode) {
    string result = messge.Content.ReadAsStringAsync().Result;
    description = result;
}

Bitrix24 支持非常 "happy" 和 php 并且不了解其他语言 :( 经过一些调查,我找到了使用匿名对象和 json.net 的方式。你的样本应该看起来像:

var data = new {
  fields = new {
    TITLE = "NEW LEAD"
  },
  @params = new {
    REGISTER_SONET_EVENT = "Y"
  }
};
var contentText = JsonConvert.SerializeObject(data);

var content = new StringContent(contentText, Encoding.UTF8, "application/json");
// and so on with HttpClient

12 月 13 日更新:

有时您不能(或不想)将字段名称直接放入匿名对象中。所以,可以使用字典:

var data = new
{
    ID = someId,
    FIELDS = new Dictionary<string, object>()
    {
        [options.SomeFieldName] = fieldValue,
    },
};

我用这段代码解决了问题:

public void CreateLead( string title, decimal opportunity, string contactName, string phoneNumber, string email )
    {
        try
        {                
            string accessToken = GetNewAccessToken();

            string url = string.Format( "https://{0}/rest/crm.lead.add.json", portal_name );

            var data = new
            {
                fields = new
                {
                    TITLE = title,
                    CURRENCY_ID = "RUB",
                    STATUS_ID = "NEW",
                    OPENED = "Y",
                    OPPORTUNITY = opportunity,
                    ASSIGNED_BY_ID = 46,
                    COMPANY_TITLE = contactName,
                    PHONE =  new List<PHONE>() { new PHONE() { VALUE_TYPE = "WORK", TYPE_ID = "PHONE", VALUE = phoneNumber } }.ToArray(),
                    EMAIL = new List<EMAIL>() { new EMAIL() { VALUE_TYPE = "WORK", TYPE_ID = "EMAIL", VALUE = email } }.ToArray()
                },
                @params = new
                {
                    REGISTER_SONET_EVENT = "Y"
                }
            };

            BitrixLead lead = new BitrixLead();

            lead.TITLE = title;
            lead.CURRENCY_ID = "RUB";
            lead.STATUS_ID = "NEW";
            lead.OPENED = "Y";
            lead.OPPORTUNITY = opportunity.ToString();                

            if (!string.IsNullOrEmpty( contactName ))
                lead.COMPANY_TITLE = contactName;

            if (!string.IsNullOrEmpty( phoneNumber ))
                lead.PHONE = new List<PHONE>() { new PHONE() { VALUE_TYPE="WORK", TYPE_ID="PHONE", VALUE = phoneNumber }}.ToArray();

            if (!string.IsNullOrEmpty( email ))
                lead.EMAIL = new List<EMAIL>() { new EMAIL() { VALUE_TYPE = "WORK", TYPE_ID = "EMAIL", VALUE = email } }.ToArray();


            PostToAPI( url, accessToken, data );
        }
        catch (Exception exc)
        {
        }
    }

private void PostToAPI( string url, string token, object data )
    {
        string json = Newtonsoft.Json.JsonConvert.SerializeObject( data );

        var http = (HttpWebRequest)WebRequest.Create( new Uri( url ) );
        http.Accept = "application/json; charset=utf-8";
        http.ContentType = "application/json; charset=utf-8";
        http.Method = "POST";            
        http.Headers.Add( "Authorization", "Bearer " + token );

        UTF8Encoding encoding = new UTF8Encoding();
        Byte[] bytes = encoding.GetBytes( json );

        Stream newStream = http.GetRequestStream();
        newStream.Write( bytes, 0, bytes.Length );
        newStream.Close();

        var response = http.GetResponse();            
    }
Public Sub webServicesDatos(params As String, ws As String, idusuario As Integer, accion As String, catalogo As String,
                                Idcampo As String, registro As Integer, idregistro As Integer, pagina As String,
                                 cat As String, nomcampo As String, bd As String, campoUpdate As String, SistemasNom As String, IdTransaccion As Integer,
                                Proceso As Integer, Evento As Integer, Usuarioadd As String)
        qryLogBitrix = ""
        request = WebRequest.Create(ws & params)
        postData = params
        Dim data As Byte() = Encoding.UTF8.GetBytes(postData)

        request.Method = "POST"
        request.ContentType = "application/json"
        request.ContentLength = data.Length

        Dim stream As Stream = request.GetRequestStream()
        stream.Write(data, 0, data.Length)
        stream.Close()

        response = request.GetResponse()
        Using reader As New StreamReader(response.GetResponseStream())
            SR2 = reader.ReadToEnd()
        End Using

        Dim ValidaResult As Object = New JavaScriptSerializer().Deserialize(Of Object)(SR2)
End Sub


Dim consumir As ConsumirRestApi = New ConsumirRestApi
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim builder As StringBuilder = New StringBuilder()
    If nombre <> "" Or ap_paterno <> "" Or ap_materno <> "" Then
        parametros = HttpUtility.UrlEncode("FIELDS[TITLE]") + "=" + HttpUtility.UrlEncode(nombre) + " " + HttpUtility.UrlEncode(ap_paterno) + " " + HttpUtility.UrlEncode(ap_materno)
        builder.Append(parametros)
    End If
    If nombre <> "" Or nombre <> Nothing Then
        parametros = "&" + HttpUtility.UrlEncode("FIELDS[NAME]") + "=" + HttpUtility.UrlEncode(nombre)
        builder.Append(parametros)
    End If
    If idExpediente <> "0" Or idExpediente <> Nothing Or idExpediente <> "" Then
        parametros = "&" + HttpUtility.UrlEncode("FIELDS[UF_ID_EXPEDIENTE]") + "=" + HttpUtility.UrlEncode(idExpediente)
        builder.Append(parametros)                                     
    End If

    If accion = "INSERT" Then
        carpeta = "crm.lead.add.json?"                                  
    ElseIf accion = "UPDATE" Then
        carpeta = "crm.lead.update.json?"
    ElseIf accion = "DELETE" Then
        carpeta = "crm.lead.delete.json?"
    End If

    consumir.webServicesDatos(builder.ToString, "https://" & ip & "" & carpeta & "", idUsuario, accion, procesos, Id_formulario, id_bitrix, idCiclo, ruta, cat, nomcamp, bdinter, nomcampo, sistemanom, idCiclo, pro, evento, "")

End Sub