Authentication

The authentication module contains functions and classes to facilitate interfacing with Mojang’s Yggdrasil authentication service.

Logging In

The most common use for this module in the context of a client will be to log in to a Minecraft account. The first step to doing this is creating an instance of the AuthenticationToken class after which you may use the authenticate method with the user’s username and password in order to make the AuthenticationToken valid.

class minecraft.authentication.AuthenticationToken(username=None, access_token=None, client_token=None)[source]

Represents an authentication token.

See http://wiki.vg/Authentication.

Constructs an AuthenticationToken based on access_token and client_token.

Parameters:
access_token - An str object containing the access_token. client_token - An str object containing the client_token.
Returns:
A AuthenticationToken with access_token and client_token set.
authenticate(username, password)[source]

Authenticates the user against https://authserver.mojang.com using username and password parameters.

Parameters:
username - An str object with the username (unmigrated accounts)
or email address for a Mojang account.

password - An str object with the password.

Returns:
Returns True if successful. Otherwise it will raise an exception.
Raises:
minecraft.exceptions.YggdrasilError

Upon success, the function returns True, on failure a YggdrasilError is raised. This happens, for example if an incorrect username/password is provided or the web request failed.

exception minecraft.authentication.YggdrasilError(message=None, status_code=None, yggdrasil_error=None, yggdrasil_message=None, yggdrasil_cause=None)[source]

Base Exception for the Yggdrasil authentication service.

Parameters:
status_code = None

int or None. The associated HTTP status code. May be set.

yggdrasil_cause = None

str or None. The “cause” field of the Yggdrasil response: a string containing additional information about the error. May be set.

yggdrasil_error = None

str or None. The “error” field of the Yggdrasil response: a short description such as “Method Not Allowed” or “ForbiddenOperationException”. May be set.

yggdrasil_message = None

str or None. The “errorMessage” field of the Yggdrasil response: a longer description such as “Invalid credentials. Invalid username or password.”. May be set.

Arbitary Requests

You may make any arbitary request to the Yggdrasil service with the _make_request method passing in the AUTH_SERVER as the server parameter.

minecraft.authentication.AUTH_SERVER = 'https://authserver.mojang.com'

The base url for Ygdrassil requests

minecraft.authentication._make_request(server, endpoint, data)[source]

Fires a POST with json-packed data to the given endpoint and returns response.

Parameters:
endpoint - An str object with the endpoint, e.g. “authenticate” data - A dict containing the payload data.
Returns:
A requests.Request object.

Example Usage

An example of making an arbitary request can be seen here:

payload = {'username': username,
           'password': password}

authentication._make_request(authentication.AUTH_SERVER, "signout", payload)