3

I wanted to get some data via API from this url https://api.hooktheory.com/v1/users/auth

I can enter it with a key, but as soon as I want to request some data it won't work, because of the following error.

I already downloaded pip via https://bootstrap.pypa.io/get-pip.py and imported requests, I also tried it via conda install pip. Nothing works out and the problem still occurs. I already searched for some solutions in here, but its. not a duplicate. FYI: I work on a Mac OS X with visual studio.

import requests
import time

login = {"Accept": "application/json", 
      "Content-Type": "application/json",
      "username":"huks",
      "password": "XXXX"}


url = "https://api.hooktheory.com/v1/users/auth"
r = requests.post(url, data=login)
print(r.json())

time.sleep(5)

activkey = 'XXXX'
header = {"Authorization": "Bearer " + activkey}

r = requests.get(url+'trends/songs', headers=header)
r.json()

r = requests.get(url+'trends/nodes?cp=4', headers=header)
r.json()

Here is the traceback + error message:

File "/Users/marius/Desktop/INNOLAB/tempCodeRunnerFile.py", line 20, in <module>
    r.json()
  File "/Users/marius/anaconda3/lib/python3.7/site-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/Users/marius/anaconda3/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/Users/marius/anaconda3/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/marius/anaconda3/lib/python3.7/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)
hux0
  • 207
  • 1
  • 4
  • 17
  • 1
    Check out the `r.status_code` and `r.text`. It's probably because it's something wrong with your request and response is not a JSON. – pawelbylina Aug 29 '19 at 06:37
  • Check your API link, I think you got a problem here: `.../authtrends/songs` when you add: `url+'trends/songs'` – Lê Tư Thành Aug 29 '19 at 06:39
  • Accept & Content-Type are headers not payload!? – Kris Aug 29 '19 at 06:53
  • @pako for r.text occurs TypeError: 'str' object is not callable. and r.status_code is 200 – hux0 Aug 29 '19 at 07:18
  • @LêTưThành the API Link and the manual is mentioned there: https://www.hooktheory.com/api/trends/docs – hux0 Aug 29 '19 at 07:23
  • @kris is correct but doesn't help anything – hux0 Aug 29 '19 at 07:23
  • I don't see you re-assign `url='hooktheory.com/api/'` – Lê Tư Thành Aug 29 '19 at 07:25
  • Please see my answer here https://stackoverflow.com/a/57686175/995052 – Kris Aug 29 '19 at 07:30
  • @LêTưThành what do you mean by this? Sorry, am pretty new to programming. Where and why should I re-assign it? – hux0 Aug 29 '19 at 07:30
  • Possible duplicate of [json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) python](https://stackoverflow.com/questions/57685622/json-decoder-jsondecodeerror-expecting-value-line-1-column-1-char-0-python) – Kris Aug 29 '19 at 07:31
  • Your current `url` is `"https://api.hooktheory.com/v1/users/auth"`. Then you append `trends/songs` by `url+'trends/songs'`=>`.../users/authtrends/songs` – Lê Tư Thành Aug 29 '19 at 07:34
  • @Kris thanks for the link, but I think it's different. The API supports xml and json, so I don't have to convert it – hux0 Aug 29 '19 at 07:36
  • @hux0 did you do any debugging on what is the response you obtained rather than just saying its not working ? – Kris Aug 29 '19 at 07:37
  • i suspect it is an issue of conda. Not having this problem with requests from pip in a virtual env without conda... Any solution ? – user305883 Dec 31 '19 at 15:07

1 Answers1

1
import requests
import time

login = {"Accept": "application/json", 
      "Content-Type": "application/json",
      "username":"huks",
      "password": "XXXX"}


url = "https://api.hooktheory.com/v1/users/auth"
r = requests.post(url, data=login)
print(r.json())

time.sleep(5)

activkey = 'XXXX'
header = {"Authorization": "Bearer " + activkey}

r = requests.get(url+'/trends/songs', headers=header)
r.json()

r = requests.get(url+'/trends/nodes?cp=4', headers=header)
print(r.text) #this will print what is the response you got!
if r.status_code == 200:
    print(r.json()) #this will work only if response is JSON

Hope the comments makes sense!

Kris
  • 8,680
  • 4
  • 39
  • 67
  • thanks! I did debugging and also tried your code just right now, but the same error is occurring. – hux0 Aug 29 '19 at 07:55
  • what is the response text you receive? – Kris Aug 29 '19 at 10:16
  • same as before: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) – hux0 Aug 29 '19 at 12:04
  • @hux dude you are not getting the point. response text is different. you are talking about the error. What does print(r.text) print? – Kris Aug 30 '19 at 04:45