Skip to content

Instantly share code, notes, and snippets.

@ixtk
Last active December 10, 2024 10:37
Show Gist options
  • Save ixtk/cef7d07e45e1978bff0afac9de18d2b2 to your computer and use it in GitHub Desktop.
Save ixtk/cef7d07e45e1978bff0afac9de18d2b2 to your computer and use it in GitHub Desktop.
Python code example of OSU! API v2 usage
import requests
from pprint import pprint
from os import getenv
API_URL = 'https://osu.ppy.sh/api/v2'
TOKEN_URL = 'https://osu.ppy.sh/oauth/token'
def get_token():
data = {
'client_id': getenv('CLIENT_ID'),
'client_secret': getenv('CLIENT_SECRET'),
'grant_type': 'client_credentials',
'scope': 'public'
}
response = requests.post(TOKEN_URL, data=data)
return response.json().get('access_token')
def main():
token = get_token()
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {token}'
}
params = {
'mode': 'osu',
'limit': 5
}
response = requests.get(f'{API_URL}/users/15810304/scores/best', params=params, headers=headers)
beatmapset_data = response.json()[0].get('beatmapset')
pprint(beatmapset_data, indent=2)
if __name__ == '__main__':
main()
@d1zeasecoder
Copy link

Thank you for making this example, I used it and it worked perfectly.

@XboXoffc
Copy link

XboXoffc commented Dec 2, 2024

thank you for this example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment