-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mastodon.cs
44 lines (35 loc) · 1.61 KB
/
Mastodon.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Mastonet;
using Mastonet.Entities;
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace MastodonGitHubBot;
internal static class Mastodon
{
public static async Task<MastodonClient> GetClientAsync(Log log, Server server, HttpClient sharedHttpClient)
{
string accessToken = await GetAccessTokenAsync(log, server, sharedHttpClient);
return new MastodonClient(instance: server.MastodonServer, accessToken, sharedHttpClient);
}
private static async Task<string> GetAccessTokenAsync(Log log, Server server, HttpClient sharedHttpClient)
{
if (string.IsNullOrWhiteSpace(server.MastodonAccessToken))
{
AuthenticationClient authClient = new(instance: server.MastodonServer, sharedHttpClient);
await authClient.CreateApp(appName: server.AppName, scope: GranularScope.Write__Statuses);
string authCode = GetMastodonOAuthCode(log, authClient);
Auth auth = await authClient.ConnectWithCode(authCode);
server.MastodonAccessToken = auth.AccessToken;
}
return server.MastodonAccessToken;
}
private static string GetMastodonOAuthCode(Log log, AuthenticationClient authClient)
{
string url = authClient.OAuthUrl();
log.WriteWarning($"Go to the authorization page '{url}' then paste the authentication code in the console.");
Console.Write("Paste the authentication code: ");
string authCode = Console.ReadLine() ?? throw new NullReferenceException(nameof(authCode));
ArgumentException.ThrowIfNullOrEmpty(authCode);
return authCode;
}
}