Is there a python script to generate Blizzard API Access Token?

Viewed 1621

I'm fairly new to coding but I'm trying to work on a bot that pulls some data from the blizzard api. The bot I have works fine but I'm having to go in and manually change the url for the json data every time the access token changes. I understand the token changes every 24 hours but i can't imagine developers have to constantly go to the console and pull up the new url with updated access token every day. I've been looking around through the blizzard OAuth documentation and I found the following code someone had posted for python:

import requests
import json
from requests.auth import HTTPBasicAuth


def create_access_token(client_id, client_secret, region = 'us'):
    url = "https://%s.battle.net/oauth/token" % region
    body = {"grant_type": 'client_credentials'}
    auth = HTTPBasicAuth(client_id, client_secret)
    response = requests.post(url, data=body, auth=auth)
    return response.json()
create_access_token()

but when i pass my client id and my client secret to this, I don't get any information. Any help would be awesome. Thanks.

1 Answers

You can use an online CURL to Python Requests ansible tool like this to get the correct requests format from the documentation CURL example of the client credential flow:

curl

curl -u {client_id}:{client_secret} -d grant_type=client_credentials https://us.battle.net/oauth/token

python

import requests

def create_access_token(client_id, client_secret, region = 'us'):
    data = { 'grant_type': 'client_credentials' }
    response = requests.post('https://%s.battle.net/oauth/token' % region, data=data, auth=(client_id, client_secret))
    return response.json()

response = create_access_token(YOUR_CLIENT_ID, YOUR_CLIENT_SECRET)
print(response)
Related