Getting an account by it's username
When having an instance of MastodonHttpService
, you can get an account by it's ID via the Accounts
endpoint, and then either of the Lookup
or LookupAsync
methods.
If the request is successful, an instance of MastodonAccountResponse
returned representing the overall response. The Body
property then holds an instance of MastodonAccount
representing the response body.
Notice that the username may be with our without a leading @
or even a full Webfinger username like @umbraco@umbracocommunity.social
.
@using Skybrud.Social.Mastodon.Exceptions
@using Skybrud.Social.Mastodon
@using Skybrud.Social.Mastodon.Models.Accounts
@using Skybrud.Social.Mastodon.Responses.Accounts
@{
// Initialize a new HTTP service (basically the API wrapper)
MastodonHttpService mastodon = MastodonHttpService
.CreateFromDomain("umbracocommunity.social");
try {
// Make the request to the Mastodon API
MastodonAccountResponse response = await mastodon.Accounts
.LookupAsync("umbraco");
// Get the account from the response body
MastodonAccount account = response.Body;
<pre>ID: @account.Id</pre>
<pre>Username: @account.Username</pre>
<pre>Display Name: @account.DisplayName</pre>
} catch (MastodonHttpException ex) {
<pre>@ex</pre>
<pre>@ex.StatusCode</pre>
<pre>@ex.Error</pre>
<pre>@ex.Response.Body</pre>
return;
} catch (Exception ex) {
<pre>@ex</pre>
return;
}
}
Notice that requests to the Mastodon API may fail, so it's recommended to wrap the request in a try/catch block.