Home > Blockchain >  I can't get the url using JSON Python
I can't get the url using JSON Python

Time:10-24

I'm trying to get the url of the minecraft skin through the api using python programming but I can't get the url.

This is the code I'm currently using...

import json
import requests
import base64
    
response = requests.get(f"https://sessionserver.mojang.com/session/minecraft/profile/11f1cc006cc84499a174bc9b7fa1982a")
id = response.json()["properties"][0]["value"]
    
####
msg = f"{id}"
msg_bytes = msg.encode('ascii')
base64_bytes = base64.b64decode(msg_bytes)
base64_msg = base64_bytes.decode('ascii')
   
print(base64_msg)

CodePudding user response:

You will have to convert the string to json first.

import requests
import base64
import json

response = requests.get(
    f"https://sessionserver.mojang.com/session/minecraft/profile/11f1cc006cc84499a174bc9b7fa1982a"
)
msg = response.json()["properties"][0]["value"]


base64_bytes = base64.b64decode(msg)

print(json.loads(base64_bytes)["textures"]["SKIN"]["url"])

CodePudding user response:

You should always check that your HTTP request succeeds.

This is all you need:

from requests import get as GET
from base64 import b64decode as DECODE
from json import loads as LOADS


URL = 'https://sessionserver.mojang.com/session/minecraft/profile/11f1cc006cc84499a174bc9b7fa1982a'

(response := GET(URL)).raise_for_status()

data = response.json()['properties'][0]['value']

sd = LOADS(DECODE(data))

print(sd['textures']['SKIN']['url'])

Output:

http://textures.minecraft.net/texture/516ca747cee2cf895c02e0b4da4f1fe23495a140326538f960debaaa6fd67045

Note:

This code assumes that the JSON structures are as expected and that the keys always exist

  • Related