Geting error using python 3.5 : a_token = r.json() mention below

Ned Batchelder ned at nedbatchelder.com
Tue Dec 20 10:37:48 EST 2016


On Tuesday, December 20, 2016 at 7:28:47 AM UTC-5, Akbar Shaikh wrote:
> import io
> import csv
> import glob
> import os.path
> import requests
> import subprocess
> import urllib.request
> import json
> import time
> import xlwt
> import xlrd
> import datetime
> from datetime import date, timedelta
> 
> def main():
>         """ Run the whole toolchain for all accounts. """
>         _clean()
> 
>         payload = {'client_id':'xxxx' ,
>                     'client_secret':'xxxx',
>                     'grant_type':'client_credentials'}
>         headers1 = {
>                 'Content-Type':'application/json'
>                 }
>         
>         r = requests.post('https://auth.smaato.com/v2/auth/token/ HTTP/1.1',params=payload,auth=('username', 'password'),headers = headers1,)
> 
>         a_token = r.json()
>         
>         ACCESSTOKEN = a_token['access_token']
>         print ('Bearer ' + ACCESSTOKEN)
> 
>         content2 = {
>                     'client_id':'xxxx' ,
>                     'client_secret':'xxxx',
>                     'grant_type':'client_credentials',
>                     'Authorization': 'Bearer' + ACCESSTOKEN,
>                     'POST':'https://api.smaato.com/v1/reporting/ HTTP/1.1',
>                     'Content-Type':'application/json',
>                     'Host': 'api.smaato.com',
>                     'criteria':{"dimension":"ApplicationId","child":"null"},
>                     'kpi': {"clicks : true"},
>                     'period':{"period_type":"fixed","start_date":"2016-12-7","end_date":"2016-12-7"}
>                     }
>         
>         headers2 = {
>                 'client_id':'xxxx' ,
>                 'client_secret':'xxxx',
>                 'grant_type':'client_credentials',
>                 'Authorization': 'Bearer' + ACCESSTOKEN,
>                 'Username':'Username',
>                 'Password':'Password',
>                 'Content-Type': 'application/json',
>                 'Host': 'api.smaato.com',
>                 }
>         s = requests.post('https://api.smaato.com/v1/reporting/',params=content2,auth=('username', 'password'), headers = headers2)
>         print(s.content)
>               
> def _clean():
>         """ Cleans old data files.
>         """
>         for f in glob.glob('./Numbers *.csv'):
>                 os.remove(f)
>                 
> if '__main__' == __name__:
>         main()
> -------------------------------------------------------------------------
> Error:  Traceback (most recent call last):
>   File "C:\Users\xxxx\Desktop\Smaato Akbar.py", line 66, in <module>
>     main()
>   File "C:\Users\xxxx\Desktop\Smaato Akbar.py", line 28, in main
>     a_token = r.json()
>   File "C:\Users\xxx\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\models.py", line 850, in json
>     return complexjson.loads(self.text, **kwargs)
>   File "C:\Users\xxxx\AppData\Local\Programs\Python\Python35-32\lib\site-packages\simplejson\__init__.py", line 516, in loads
>     return _default_decoder.decode(s)
>   File "C:\Users\xxxx\AppData\Local\Programs\Python\Python35-32\lib\site-packages\simplejson\decoder.py", line 374, in decode
>     obj, end = self.raw_decode(s)
>   File "C:\Users\xxxx\AppData\Local\Programs\Python\Python35-32\lib\site-packages\simplejson\decoder.py", line 404, in raw_decode
>     return self.scan_once(s, idx=_w(s, idx).end())
> simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)


That error means you are trying to decode an empty string. Most likely,
your request failed, and there's a status code that will give you a clue
as to why it failed.  You should check the success of the request before
trying to use the results.

>From the docs (http://docs.python-requests.org/en/master/user/quickstart/#json-response-content):

> To check that a request is successful, use r.raise_for_status() or check
> r.status_code is what you expect.

--Ned.


More information about the Python-list mailing list