Code Monkey home page Code Monkey logo

mojangsharp's Introduction

MojangSharp

MojangSharp is a C# wrapper for the Mojang API and Mojang Authentication API.

Features

  • Asynchronous API
  • All error and response types handled
  • Really easy to use

Getting started

GitHub release GitHub issues   NuGet NuGet downloads

Installation

You will need to install MojangSharp by downloading it or installing it from NuGet with MS> Install-Package Hawezo.MojangSharp.

Usage

MojangSharp contains a Endpoints namespace which contains all of the possible actions. See the few examples below to understand their usage:

ApiStatus

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

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.

Location

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.

Skins

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 }

Blocked servers

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 }

Statistics

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 }

Dependencies

MojangSharp uses Newtonsoft's JSON to parse Mojang's API responses.

To-do

mojangsharp's People

Contributors

deeprobin avatar innocenzi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

mojangsharp's Issues

Some issues with strong name/sign app

When using MojangSharp in a signed app, the build fail with this message :
System.IO.FileLoadException : 'Could not load file or assembly 'MojangSharp, Version=0.3.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)'
This seems to come during the digital signature of our application, I think that if the MojangSharp lib is not signed and we try to sign our application we get this error.

So I ask if MojangSharp is signed or not?

Error on try to authenticate

I am trying to use this lib on my project but i am having this erro on authenticate:
image
(Error message: The path has an invalid format)

This is the code:

using MojangSharp.Endpoints;
using MojangSharp.Responses;
using System;
using System.Windows.Forms;

namespace AreaZ12_Launcher
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            AuthenticateResponse auth = new Authenticate(new Credentials() { Username = "[email protected]", Password = "Aaaa1111" }).PerformRequestAsync().Result;
            if (auth.IsSuccess)
            {
                label1.Text = "Sucesso: " + auth.AccessToken;
            } else
            {
                label1.Text = "Erro: ";
            }
        }
    }
}

The password its an example!
I need to change something?

Session ID Validator

I'm sure theres a way in the mojang api to check if a session ID is valid, there are plenty of work arounds for this for me, but I would be a nice feature.

What determines the URL of the uploaded skin?

First of all, thank you so much for making this. It makes my life far easier when trying to interact with the API.
I have a bit of a question. I'm attempting to create a program that, while being used, changes the user's skin a few times, and it very specifically needs the minecraft.net URL that contains the new skin.
Currently, I have to submit a skin change request, then a profile request to get its Properties.SkinUri. However, requesting profiles is more heavily rate-limited according to http://wiki.vg/Mojang_API, and I've noticed that it is indeed quite slow.
Some experimentation led me to discover that uploading two identical skins produced the same URL, so that led me to assume one of two situations is happening:

  • The server searches every uploaded skin looking for a copy and sets your skin to that URL to prevent duplicates (seems very slow and wasteful)
  • Some sort of hash is generated from the image that determines the URL to resolve duplicates (seems likely).

If the latter is true, there must be a way to find the destination URL of a source skin without having to ask the API where it was placed. Do you know how this can be done?
Thanks again.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.