You need to obtain a ClientId and a ClientSecret for your app, directly from the client, on the target Mastodon instance.
Call the CreateApp
method :
var instance = "example.org";
var authClient = new AuthenticationClient(instance);
var appRegistration = await authClient.CreateApp("Your app name", Scope.Read | Scope.Write | Scope.Follow);
The appRegistration
object must be saved.
Now you can connect the user (not recommended, prefer OAuth when you can) :
var auth = await authClient.ConnectWithPassword("email", "password");
The recommended way to login is to use OAuth. You open a web browser and let the user log himself on his instance.
var url = authClient.OAuthUrl();
OpenBrowser(url);
You can either embed a WebView in your app, or open an external browser. When the user allowed your app to access their account, he is redirected to a web page with an auth code.
You have several option to get the code :
- Ask the user to copy and paste it in your app (easy for you, but not user-friendly)
- If you have embedded a WebView in your app, you can read the final page. The code is in the url, and in the webpage embedded in a
<code>
tag
If you are in a web context, you can set the final page url, and the user will be redirected directly to your server with the code. Just add your url to the OAuthUrl
method.
var url = authClient.OAuthUrl(myRedirectPage);
Now this code will let you get the access token for the user
var auth = await authClient.ConnectWithCode(authCode);
When you have the access token, you should save it in the app, and use it every time you restart the app. You just need to add it to the MastodonClient constructor.
var accessToken = auth.AccessToken;
var client = new MastodonClient(instance, accessToken);
Now you can call all the API methods. Mastonet API Mastodon API overview
You can use the TimelineStreaming
to be notified for every status, notification and deletion on a timeline.
var client = new MastodonClient("instance", appRegistration, auth);
var streaming = client.GetUserStreaming();
// Register events
streaming.OnUpdate = OnStatusReceived;
streaming.OnNotification = OnNotificationReceived;
streaming.OnDelete = OnDeleteReceived;
// Start streaming
streaming.Start();
// ...
// Stop streaming
streaming.Stop();
You should share a single instance of HttpClient in your entire app (c.f. You're using HttpClient wrong and it is destabilizing your software). If you have HttpClient in other parts of your app, you can inject it from constructors:
var httpClient = new HttpClient();
var authClient = new AuthenticationClient(instance, httpClient);
var mastodonClient = new MastodonClient(instance, accessToken, httpClient);