Kontakte mit xapi löschen / update

o.steiner

Silver Partner
Basic Certified
Mitglied seit
23. Oktober 2023
Beiträge
8
Hallo,
vielleicht hat ja einer von euch einen Tipp für mich. Ich will Kontakte im Telefonbuch per API aktualisieren. Die Abfrage der aktuellen Inhalte klappt. Das Anlegen von neuen Kontakten klappt auch. Ich schaffe es nicht einzelne Kontakte zu aktualisieren, oder zu löschen. Hat jemand eine Quelle für eine Dokumentation? Eine komplette API Dokumentation gibt es meines Wissens nach nicht.

Die Client ID steht auf Default und Systemeigentümer. Ich habe auch schon Systemweit probiert, alles leider ohne Erfolg.

Folgende Endpoints habe ich versucht:
PATCH https://{{PBX_FQDN}}/xapi/v1/Contacts/27 > 404 Fehler
DELETE https://{{PBX_FQDN}}/xapi/v1/Contacts/27 > 404 Fehler
POST https://{{PBX_FQDN}}/xapi/v1/DeleteContactById / ID als JSON im Body > 405 Fehler
POST https://{{PBX_FQDN}}/xapi/v1/DeleteContact / ID als JSON im Body > 405 Fehler

Ich habe noch viele andere Kombinationen versucht immer mit 404 oder 405 Fehler.

Danke für jeden Tipp

Oliver
 
Zuletzt bearbeitet:
Am einfachsten mit den Browser Entwicklungstools (Register Netzwerk) die Vorgänge beobachten
PATCH /xapi/v1/Contacts(2)
POST /xapi/v1/Contacts/Pbx.BatchContactsDelete mit den IDs im Payload
 
So mal als Beispiel zum drüber nachdenken zum Löschen eines einzelnen Kontaktes, z.B. für den Aufbau einer Schleife usw.,
3cx-powershell-xapi-contacts-delete-single.ps1

Code:
$tcxurl = "mypbx.mydomain.de"   # needs port if not 443

$jsonauthbody = (@{
    Username="100"              # 3CX user, maybe email address
    Password="Super_Secret0815" # 3CX user password
    SecurityCode=""
} | ConvertTo-Json)

$LoginResponse = Invoke-RestMethod -Uri "https://$tcxurl/webclient/api/Login/GetAccessToken" -Body $jsonauthbody -Method POST -ContentType "application/json"

if( $LoginResponse.Status -ne "AuthSuccess" ) {
    Write-Host "abort: authentication not successful"
    exit -1
}

$headers = @{Authorization = "Bearer $($LoginResponse.Token.access_token)"}

# get all contacts ...
# $tcxcontacts = (Invoke-RestMethod -uri "https://$tcxurl/xapi/v1/Contacts" -headers $headers -Method GET -ContentType "application/json").Value

# get specific contact ID - without using 3CX searching
$tcxcontactid = ((Invoke-RestMethod -uri "https://$tcxurl/xapi/v1/Contacts" -headers $headers -Method GET -ContentType "application/json").Value | where-object { $_.LastName -eq "Mustermann"}).Id

# remove single contact with ID
Invoke-RestMethod -uri "https://$tcxurl/xapi/v1/Contacts($tcxcontactid)" -headers $headers -Method DELETE -ContentType "application/json"

Ändern eines bestehenden Kontaktes wenn die ID bekannt ist (s.o.):
Code:
$patch='[ { "FirstName": "Max", "LastName": "Mustermann", "PhoneNumber": "0123456789", "CompanyName": "", "Tag": "", "Mobile2": "", "Email": "", "Home": "", "Business": "", "Other": "", "Business2": "", "Title": "", "BusinessFax": "", "Department": "", "Pager": "", "ContactType": "3CX" }, { "FirstName": "Erika", "LastName": "Mustermann", "PhoneNumber": "0987654321", "CompanyName": "", "Tag": "", "Mobile2": "", "Email": "", "Home": "", "Business": "", "Other": "", "Business2": "", "Title": "", "BusinessFax": "", "Department": "", "Pager": "", "ContactType": "3CX" }]' |convertto-json
Invoke-RestMethod -uri "https://$tcxurl/xapi/v1/Contacts($tcxcontactid)" -headers $headers -Method PATCH -body $patch -ContentType "application/json"

Das ist ausbaufähig ...
 
Zuletzt bearbeitet:
Vielen Dank für die Hinweise. Ich hab den Codeblock nochmal neu erstellt. Jetzt funktionierts.

Grüße
Oliver
 

Statistik des Forums

Themen
44.412
Beiträge
232.707
Mitglieder
78.328
Neuestes Mitglied
as7h