Python 3 basic auth with pinnaclesports API
Question: i am trying to grab betting lines with python from pinnaclesports using their
API <http://www.pinnaclesports.com/api-xml/manual>

which requires basic authentication (<http://www.pinnaclesports.com/api-
xml/manual#authentication>):

> Authentication
>
> API use HTTP Basic access authentication . Always use HTTPS to access the
> API. You need to send HTTP Request header like this:
>
> Authorization: Basic
>  
>  
>     For example:
>     Authorization: Basic U03MyOT23YbzMDc6d3c3O1DQ1
>  
    
    
    import urllib.request, urllib.parse, urllib.error
    import socket
    import base64
    
    url = 'https://api.pinnaclesports.com/v1//feed?sportid=12&leagueid=6164'
    username = "abc"
    password = "xyz"
    
    base64 = "Basic: " + base64.b64encode('{}:{}'.format(username,password).encode('utf-8')).decode('ascii')
    print (base64)
    
    details = urllib.parse.urlencode({ 'Authorization' : base64 })
    details = details.encode('UTF-8')
    url = urllib.request.Request(url, details)
    url.add_header("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13")    
    responseData = urllib.request.urlopen(url).read().decode('utf8', 'ignore')
    print (responseData)
    

Unfortunately i get a http 500 error. Which from my point means either my
authentication isn't working properly or their API is not working.

Thanks in advance


Answer: As it happens, I don't seem to use the Python version you use, so this has not
been tested using your code, but there is an extraneous colon after "Basic" in
your base64 string. In my own code, adding this colon after "Basic" indeed
yields a http 500 error.

**Edit: Code example using Python 2.7 and urllib2:**

    
    
    import urllib2
    import base64
    
    def get_leagues():
        url = 'https://api.pinnaclesports.com/v1/leagues?sportid=33'
        username = "myusername"
        password = "mypassword"
        b64str = "Basic " + base64.b64encode('{}:{}'.format(username,password).encode('utf-8')).decode('ascii')
        headers = {'Content-length' : '0',
                   'Content-type' : 'application/xml',
                   'Authorization' : b64str}
    
        req = urllib2.Request(url, headers=headers)
        responseData = urllib2.urlopen(req).read()
        ofn = 'api_leagues.txt'
        with open(ofn, 'w') as ofile:
            ofile.write(responseData)
    

