MojangSharp is a C# wrapper for the Mojang API and Mojang Authentication API.
- Asynchronous API
- All error and response types handled
- Really easy to use
You will need to install MojangSharp by downloading it or installing it from NuGet with MS> Install-Package Hawezo.MojangSharp
.
MojangSharp contains a Endpoints
namespace which contains all of the possible actions. See the few examples below to understand their usage:
First, get Response
object corresponding to the Endpoint
you are using. In the case of ApiStatus
, the Response
object is ApiStatusResponse
.
Then, instantiate the Endpoint
object and call its method asynchronous PerformRequest()
.
ApiStatusResponse status = await new ApiStatus().PerformRequest();
If the request is a success, the boolean value of status.IsSuccess
would be set to true
. Otherwise, the Error
property will indicates where is the issue coming from.
Assuming the request is a success, you can access each property of status
to get the responses you needed:
Console.WriteLine($"Mojang: {status.Mojang}");
Console.WriteLine($"Minecraft: {status.Minecraft}");
Console.WriteLine($"Skins: {status.Skins}");
Console.WriteLine($"Sessions: {status.Sessions}");
//...
Authentication's request type is the same as the other. You will need to instanciate a Credentials
object containing the username and the password of the player you want to authenticate.
Then, you will be able to perform the request and get an access token.
AuthenticateResponse auth = await new Authenticate(new Credentials() { Username = "<mail>/<username>", Password = "<password>" }).PerformRequest();
if (auth.IsSuccess) {
Console.WriteLine($"AccessToken: {auth.AccessToken}");
Console.WriteLine($"ClientToken: {auth.ClientToken}");
} else { // Handle your error }
Note that ClientToken
is an auto-generated token coming from the library. The first time it is used, you can decide to store it somewhere and thus be able to user the Validate
, Invalidate
and the other endpoints of the Authentication API.
You can check after an authentication request if the Client Token is the same as the one stored in Requester.ClientToken
. If not, there is probably an issue with your authentication structure.
Some endpoints use Bearer Authentication to authenticate a player thanks to its access token, which is retrieved thanks to the Authentication
endpoint.
Sometimes, Mojang rejects a requests because it assumes the request is not secured due to its location. In order to determine if Mojang will accept these kind of requests, you will need to use the SecureIP
endpoint.
Response secured = new SecureIP(auth.AccessToken).PerformRequestAsync().Result;
if (secured.IsSuccess)
// Mojang will likely accept requests coming from this IP :)
else
// Mojang will reject further requests.
Warning - Please perform your own tests for all skin-related endpoints, this feature has not been tested (but the requests work so it is likely working).
You can change or reset a skin with MojangSharp. To change a skin, you can either call UploadSkin
endpoint to upload a skin to the Mojang's servers, or call ChangeSkin
with an URL to the skin you want to change to.
Response skinUpload = await new UploadSkin(auth.AccessToken, auth.SelectedProfile.Value, new FileInfo(@"<path>")).PerformRequest();
if (skinUpload.IsSuccess) {
Console.WriteLine("Successfully changed skin.")
} else { // Handle your errors }
Mojang has a list of actually blocked addresses, which are SHA1-hashed. Some of them has been cracked by the community and are listed in MojangSharp.
BlockedServersResponse servers = await new BlockedServers().PerformRequest();
if (servers.IsSuccess) {
Console.WriteLine($"{servers.BlockedServers.Count} blocked servers");
Console.WriteLine($"{servers.BlockedServers.FindAll(x => x.Cracked).Count} cracked");
}
else { // You know what }
Mojang offers an endpoint to get its statistics. Although there is not a lot of interest, it can somehow be useful.
You can combine up to 4 statistics in the Statistics
constructor, in which case the resulting numbers will be added to each other.
StatisticsResponse stats = await new Statistics(Item.MinecraftAccountsSold).PerformRequest();
if (stats.IsSuccess) {
Console.WriteLine($"Total Minecraft accounts sold: {stats.Total}");
Console.WriteLine($"Last 24h: {stats.Last24h}");
Console.WriteLine($"Average sell/s: {stats.SaleVelocity}");
} else { // Handle your errors }
MojangSharp uses Newtonsoft's JSON to parse Mojang's API responses.
- Better error handling?
- Add a request limiter.