From what am i doing i am stuck [python]

Hi I am trying get a battle net user token in python so far i got to this

def getBattleNetToken(battleNetCode):
    url = "http://eu.battle.net/oauth/token?grant_type=authorization_code&code=" + battleNetCode + "&redirect_uri=" + Globals.redirect_URL
    c = requests.post(url)

but I know I need to add things I just don’t know how or what.
I would like it if you could just fill in what is missing.
Good day.

You have to send your client_credentials as HTTP basic authorization and the other parameters are part of the request body.

You can modify this snippet to include the other parameters. Just add code, redirect_uri and scope to the body.

ok thank you very much now i have this Q what am i supposed to change here:

def getBattleTag(BattleNetToken, region):
    url = "https://%s.battle.net/oauth/userinfo" % region
    body = {"grant_type": 'client_credentials', "code": f"{BattleNetToken}", "redirect_uri": f"{Globals.redirect_URL}"}
    auth = HTTPBasicAuth(Globals.clientID, Globals.clientSecret)
    response = requests.get(url, data=body, auth=auth)
    return response.json()

You are using the wrong URL here:

should be:

https://%s.battle.net/oauth/token

not working

def getBattleTag(BattleNetToken, region):
    url = "https://%s.battle.net/oauth/token" % region
    body = {"grant_type": 'client_credentials', "code": f"{BattleNetToken}", "redirect_uri": f"{Globals.redirect_URL}"}
    auth = HTTPBasicAuth(Globals.clientID, Globals.clientSecret)
    response = requests.get(url, data=body, auth=auth)
    return response.json()

errors:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
StopIteration: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\yotam\Documents\GitHub\OverwatchIsraelDIscordBot\PythonPart\webApp\main.py", line 31, in login
    print(getBattleTag(battleNetCode, "eu"))
  File "C:\Users\yotam\Documents\GitHub\OverwatchIsraelDIscordBot\PythonPart\webApp\main.py", line 23, in getBattleTag
    return response.json()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\requests\models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Yep, sorry I just noticed this is another function.

It is actually simpler. Once you get the access_token you can just add it to the endpoint url as a queryString like this:

def getBattleTag(BattleNetToken, region):
    url = f"https://{region}.battle.net/oauth/userinfo?access_token={BattleNetToken}"
    response = requests.get(url)
    return response.json()
1 Like

With this code you can get your token in Python

URL = "https://YOURCLIENTID:CLIENTSECRET@eu.battle.net/oauth/token"

  # defining a params dict for the parameters to be sent to the API

  PARAMS = {'grant_type': 'client_credentials'}

  # sending get request and saving the response as response object

  r = requests.get(url = URL, params = PARAMS)

  # extracting data in json format

  data = r.json()

  access_token = data["access_token"]
1 Like

Thank you for this!! After getting my access token I didn’t know how to format the calls I needed in order to actually do something with the token. This helped me a lot! Now I’m able to get the data that I want and parse it. I’m still learning to work with requests and api calls. My goal for now is to get my discord bot to spit out information connected to api calls. I really wanted to do it for Call of Duty Warzone stats but it appears that there isn’t an API for that yet. So I’m going to work with Starcraft since that is one of my favorite games! Anyways your contribution has helped me progress. So Big thanks!! also to all that contributed in here as well.

I learned that what I wanted to do was a 2 legged oauth that pretty much handles everything within the app. And most of the explanations on the web or youtube deal with the 3 legged oauth. Somebody on stackoverflow pointed it out to me and I googled the difference and it completely shed light on what I was looking for. And your solution hit it home.

1 Like