Topics

Sample code to integrate with .NET #important #api

Nadeem Shehzad
 

Does anyone have a sample .NET code to call basic API endpoints such as authentication, creating group etc.

Greatly appreciate if someone can share the code sample

Christopher Warrington
 

On 2020-04-08 at 9:02:55 AM, Nadeem Shehzad <nshehzad1@...> wrote:

Does anyone have a sample .NET code to call basic API endpoints such as
authentication, creating group etc.
I've just uploaded a simple app that sets a user's location using the API
from C# at https://github.com/chwarr/gio-api-example.

.NET's HttpClient will automatically handle the auth cookies that Groups.io
uses, so it's pretty simple to use the API. Typically you just POST to the
right URI with null for the content.

QueryHelpers.AddQueryString from the Microsoft.AspNetCore.WebUtilities is
helpful to build these URIs.

I've been able to use System.Text.Json to parse the responses that I've
needed so far. I had to write a little snake_case naming policy to convert
between C#'s prevailing UpperCamelCase style and the lower_snake_case used
by Groups.io.

Here's an example that calls login:

var parameters = new Dictionary<string, string>()
{
["email"] = email,
["password"] = password,
// 2FA is shown in the full example, linked above
};

string loginUri = QueryHelpers.AddQueryString(
"https://groups.io/api/v1/login",
parameters);

HttpResponseMessage response = await _httpClient.PostAsync(
loginUri,
content: null);

[1]: https://www.nuget.org/packages/Microsoft.AspNetCore.WebUtilities/

--
Christopher W. <lists@...>