Support One
Arc Framework Documentation
Domain Checker
Archived
Archived Software
MassMail
HOSTEdit
Past Media
GUID Plus
TransACTION
XP Hidden Application Interface
PowerShell Script Dump
Csharp-code-exampes
C# Code Examples
Sophos Coud API C#
Sidder 2.6.0.0
Software
Shodan Basic
Arc Framework
Azure Blob Downloader
Blackjack
Zebra Calibration Tool
Login
Sophos Coud API C#
Below you can find and example of how to authenticate and connect using C# to the Sophos API and pulll down a device list.
namespace Sophos.Example { using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using MeawOps.Entities.Helpers; using Newtonsoft.Json; using Newtonsoft.Json.Linq; /// <summary> /// Sophos API. /// </summary> public static class SophosAPI { private static string bearer = string.Empty; private static string tenantID = string.Empty; private static string apiurl = string.Empty; /// <summary> /// Update Sophos devices. /// </summary> public static bool GetData() { GetBearer(); if (apiurl != string.Empty) { using var client = new HttpClient(); client.BaseAddress = new Uri(apiurl); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearer); client.DefaultRequestHeaders.Add("X-Tenant-ID", tenantID); HttpResponseMessage response = client.GetAsync("/endpoint/v1/endpoints?pageSize=500").Result; var token = response.Content.ReadAsStringAsync().Result; var data = (JObject)JsonConvert.DeserializeObject(token); foreach (JObject obj in data["items"]) { // Process data here string hostname = obj["hostname"].Value<string>().Trim().ToUpper(); } } } /// <summary> /// Get Sophos bearer token. /// </summary> private static void GetBearer() { if (bearer == string.Empty) { using var httpClient = new HttpClient(); using var request = new HttpRequestMessage(new HttpMethod("POST"), "https://id.sophos.com/api/v2/oauth2/token"); // set own client id and client secret here. request.Content = new StringContent("grant_type=client_credentials&client_id={SophosClientID}&client_secret={SophosClientSecret}&scope=token"); request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded"); var response = httpClient.Send(request); var token = response.Content.ReadAsStringAsync().Result; var data = (JObject)JsonConvert.DeserializeObject(token); bearer = data["access_token"].Value<string>(); GetTenant(); } } /// <summary> /// Get Sophos tenant and url. /// </summary> private static void GetTenant() { if (tenantID == string.Empty) { using var client = new HttpClient(); client.BaseAddress = new Uri("https://api.central.sophos.com/"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearer); HttpResponseMessage response = client.GetAsync("whoami/v1").Result; var token = response.Content.ReadAsStringAsync().Result; var data = (JObject)JsonConvert.DeserializeObject(token); tenantID = data["id"].Value<string>(); apiurl = data["apiHosts"]["dataRegion"].Value<string>(); } } } }