Licence Apache 2.0
This library is a C# (netstandard 2.0) implementation of Proton API Client. It is meant for use by C# applications that want to authenticate at Proton server.
The official Python client can be found here.
For installation clone this repository recursively with submodules:
git clone --recurse-submodules -j8 git://github.com/Eppie-io/proton-cs-client
Create new Proton session. For test run use these parameters:
using Tuvi.Proton.Client;
var proton = new Session(
httpClient: new HttpClient(),
host: new Uri("https://example.api.com"))
{
AppVersion = "GithubExample_0.0.1",
UserAgent = "Ubuntu_20.04",
ClientSecret = string.Empty,
RedirectUri = new Uri("https://redirect.api.com")
};
Authenticate by calling LoginAsync
. Provide username and password.
Next, check if two-factor authentication is enabled. If so, provide 2FA code and TOTP with ProvideTwoFactorCodeAsync
.
await proton.LoginAsync(
username: "[email protected]",
password: "password",
cancellationToken: CancellationToken.None);
if (proton.IsTwoFactor && proton.IsTOTP)
{
await proton.ProvideTwoFactorCodeAsync(
code: "123456",
cancellationToken: CancellationToken.None);
}
LogoutAsync
closes the session.
await proton.LogoutAsync(cancellationToken: CancellationToken.None);
RefreshAsync
asynchronously refreshes AccessToken
.
await proton.RefreshAsync(cancellationToken: CancellationToken.None);
If you want to store the session for, call Dump
. The return value will contain JSON data, that can be passed later to Load
and RefreshAsync
.
string protonDump = proton.Dump();
Save(protonDump);
Sample result:
{
"Version":1,
"Uid":"7ry2z7aydqhqir4a3xe7pcyqyqblkzmp",
"AccessToken":"xvth2getrrhfuvvw5lfnkd7k3esfdbz7",
"TokenType":"Bearer",
"RefreshToken":"n4am4teh7htzbkhjyr2rg4fsut35ec46",
"PasswordMode":1,
"Scope":"full self payments keys parent user loggedin nondelinquent mail vpn calendar drive pass verified"
}
To Load
previvously saved session, provide a JSON formatted string created by Dump
:
string protonDump = ExampleLoadProtonDumpMethod();
proton.Load(dump: protonDump);
After successful authentication you are ready to make API calls to Proton server. For more information on available requests refer to official Proton API documentation.
RequestAsync
makes a request asynchronously.
The following demo shows how to count the number of messages in the maillbox. This will return a value of type TotalMessagesContent
that results from deserializing the content as JSON.
Example:
// TotalMessagesContent type
struct TotalMessagesContent
{
public int Code { get; set; }
public string Error { get; set; }
public JsonObject Details { get; set; }
public IList<Folder> Counts { get; set; }
public record Folder
{
public string LabelID { get; set; }
public long Total { get; set; }
public long Unread { get; set; }
}
}
// api request
TotalMessagesContent countResponse = await proton.RequestAsync<TotalMessagesContent>(
endpoint: new Uri("/mail/v4/messages/count", UriKind.Relative),
method: HttpMethod.Get,
headers: null,
cancellationToken: CancellationToken.None);