Python API : JSONDecodeError: Expecting value: line 1 column 1

Python API : JSONDecodeError: Expecting value: line 1 column 1

我在使用此代码的标题中遇到了错误(请参阅下面的完整回溯)。我正在使用网站 API 进行身份验证,然后尝试 post a JSON object 来创建活动,但我不知道自己做错了什么.

我是初学者,所以也欢迎您对我的代码提出批评。这是我第一次处理 API

import requests
import json

client_key = r'xxxxxxxxxxxxx'
client_secret = r'yyyyyyyyy'

#Password authentication
authRequest = requests.post("https://backstage.site.com/backstage/oauth/token?client_id="+ client_key +"&client_secret="+ client_secret)

data = {
    "name": "Demo Campaign",
    "branding_text": "Pizza",
    "cpc": 0.25,
    "spending_limit": 1000,
    "spending_limit_model": "MONTHLY"
}

token = 'Bearer ' + authRequest.json()['access_token']
print(token)
url = "https://backstage.site.com/backstage/api/1.0/xxxxx/campaigns/"

headers = {
    'authorization': 'Bearer ' + token,
    }

resp = requests.post(url=url, data=json.dumps(data), headers={'Content-Type': 'application/json;charset=utf-8','Authorization':"'" + token + "'"})

print(resp.json())

回溯

C:\Users\home\VisualCodeProjects>^Zcd c:\Users\home\VisualCodeProjects && cmd /C "set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && python c:\Users\home\.vscode\extensions\ms-python.python-2018.9.2\pythonFiles\experimental\ptvsd_launcher.py 57290 e:\Dropbox2\Dropbox\Ubot\Ubot\Python\Taboola-API-playground.py "
C:\Users\home\VisualCodeProjects>cd c:\Users\home\VisualCodeProjects && cmd /C "set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && python c:\Users\home\.vscode\extensions\ms-python.python-2018.9.2\pythonFiles\experimental\ptvsd_launcher.py 57426 e:\Dropbox2\Dropbox\Ubot\Ubot\Python\Taboola-API-playground.py "
Bearer CXwJAAAAAAAAEVDsAhZjgzY2IyZjU0YzM0Zjk1MmYyNDRiZmU0Njg4OTAyZEAC::xxxxx::yyyyyy
Traceback (most recent call last):
  File "c:\Users\home\.vscode\extensions\ms-python.python-2018.9.2\pythonFiles\experimental\ptvsd_launcher.py", line 118, in <module>
    vspd.debug(filename, port_num, '', '', run_as)
  File "c:\Users\home\.vscode\extensions\ms-python.python-2018.9.2\pythonFiles\experimental\ptvsd\ptvsd\debugger.py", line 37, in debug
    run(address, filename, *args, **kwargs)
  File "c:\Users\home\.vscode\extensions\ms-python.python-2018.9.2\pythonFiles\experimental\ptvsd\ptvsd\_local.py", line 79, in run_file
    run(argv, addr, **kwargs)
  File "c:\Users\home\.vscode\extensions\ms-python.python-2018.9.2\pythonFiles\experimental\ptvsd\ptvsd\_local.py", line 140, in _run
    _pydevd.main()
  File "c:\Users\home\.vscode\extensions\ms-python.python-2018.9.2\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1751, in main
    debugger.connect(host, port)
  File "c:\Users\home\.vscode\extensions\ms-python.python-2018.9.2\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1107, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "c:\Users\home\.vscode\extensions\ms-python.python-2018.9.2\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1114, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "c:\Users\home\.vscode\extensions\ms-python.python-2018.9.2\pythonFiles\experimental\ptvsd\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "e:\Dropbox2\Dropbox\Ubot\Ubot\Python\Taboola-API-playground.py", line 34, in <module>
    print(resp.json())
  File "C:\Users\home\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\home\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\home\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\home\AppData\Local\Programs\Python\Python37-32\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)

服务器似乎没有return有效的JSON。尝试检查响应代码和 .text 以查看您的请求是否成功以及服务器响应的内容。