cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
cthulhuhala
Engaged Sweeper

Below is my code for my authorization request and my GraphQL query.

 And this is the output of the same code:
"{"data":{"me":{"username":"m.***","profiles":[{"site":{"id":"75812745-2db4-41b8-9a5a-******","name":"***lansweeper"}}]}}}"

How do I get a device name and or other names?

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

class Program
{
// The entry point of the program, marked as asynchronous to allow for async operations.
private static async Task Main(string[] args)
{
// URL of the GraphQL endpoint
string graphqlEndpoint = "https://api.lansweeper.com/api/v2/graphql";
// Authorization token for accessing the API
string token = "lsp.......";

// Define the GraphQL query
var query = new
{
query = @"{
me {
username
profiles {
site {
id
name
}
}
}
}",
// An empty object for variables, used if the query has variables
variables = new { }
};

// Using HttpClient to send the request
using (var client = new HttpClient())
{
// Add the Authorization header with the token
client.DefaultRequestHeaders.Add("Authorization", $"Token {token}");

// Serialize the query object to JSON and create the HTTP content with proper headers
var jsonContent = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(query), Encoding.UTF8, "application/json");

// Create the HTTP request message
var request = new HttpRequestMessage
{
Method = HttpMethod.Post, // HTTP POST method
RequestUri = new Uri(graphqlEndpoint), // The URI of the GraphQL endpoint
Content = jsonContent // The serialized JSON content
};

// Send the HTTP request asynchronously and await the response
var response = await client.SendAsync(request);

// Check if the response is successful
if (response.IsSuccessStatusCode)
{
// Read and output the response content
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
else
{
// Output an error message with the status code if the request failed
Console.WriteLine($"Error: {response.StatusCode}");
}
}
}
}


1 ACCEPTED SOLUTION
EthanBills
Lansweeper Employee
Lansweeper Employee

Hi, it depends on what device names exactly you are referring to, but if you mean like windows devices and such, or the names of your assets, use assetBasicInfo.name. This piece of the documentation goes into more detail: How to get Lansweeper's data | Documentation – Lansweeper.

View solution in original post

1 REPLY 1
EthanBills
Lansweeper Employee
Lansweeper Employee

Hi, it depends on what device names exactly you are referring to, but if you mean like windows devices and such, or the names of your assets, use assetBasicInfo.name. This piece of the documentation goes into more detail: How to get Lansweeper's data | Documentation – Lansweeper.