使用此代码,标题中出现错误(请参阅下面的完整追溯)。我正在使用网站API进行身份验证,然后尝试发布JSON对象来创建广告系列,但我无法弄清楚自己在做什么错。
我是一个初学者,所以也欢迎您对我的代码提出批评。这是我第一次处理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)
答案 0 :(得分:2)
服务器似乎没有返回有效的JSON。尝试检查响应的代码和.text
,以查看您的请求是否成功以及服务器响应了什么。