<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Why is access to Splunk API via Python generating an &amp;quot;Unauthorized&amp;quot; error? in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345830#M5410</link>
    <description>&lt;P&gt;Using some of the python code provided by Splunk:&lt;/P&gt;

&lt;P&gt;Step 3: Get the search status&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;httplib2.Http(disable_ssl_certificate_validation=True).add_credentials(username, password)
servicessearchstatusstr = '/services/search/jobs/%s/' % sid
isnotdone = True
while isnotdone:
    searchstatus = httplib2.Http(disable_ssl_certificate_validation=True).request(baseurl + servicessearchstatusstr, 'GET')[1]
    print searchstatus
    isdonestatus = re.compile('isDone"&amp;gt;(0|1)')
    isdonestatus = isdonestatus.search(searchstatus).groups()[0]
    if (isdonestatus == '1'):
        isnotdone = False
print "====&amp;gt;search status:  %s  &amp;lt;====" % isdonestatus

Error message produced:

====&amp;gt;search result:  [{"messages":[{"type":"ERROR","text":"Unauthorized"}]}]  &amp;lt;====
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I have read many questions about people having the same issue and have tried all of the solutions but the error still persists. I've tried changing the password to something that's only alphanumeric (no special characters) so nothing needs to be escaped. I've changed the password from default, I've made a new account with admin access for api calls and I've even tried printing out the password and username in the script at multiple points so that I can see that it doesn't change values etc. &lt;/P&gt;

&lt;P&gt;Any POST requests I make (running the search for example) will work fine, it's as soon as it comes to retrieving the information (GET requests) that there is a problem! Can anyone please provide any additional solutions that may help with this issue?&lt;/P&gt;</description>
    <pubDate>Mon, 05 Feb 2018 13:40:32 GMT</pubDate>
    <dc:creator>Robbie1194</dc:creator>
    <dc:date>2018-02-05T13:40:32Z</dc:date>
    <item>
      <title>Why is access to Splunk API via Python generating an "Unauthorized" error?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345830#M5410</link>
      <description>&lt;P&gt;Using some of the python code provided by Splunk:&lt;/P&gt;

&lt;P&gt;Step 3: Get the search status&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;httplib2.Http(disable_ssl_certificate_validation=True).add_credentials(username, password)
servicessearchstatusstr = '/services/search/jobs/%s/' % sid
isnotdone = True
while isnotdone:
    searchstatus = httplib2.Http(disable_ssl_certificate_validation=True).request(baseurl + servicessearchstatusstr, 'GET')[1]
    print searchstatus
    isdonestatus = re.compile('isDone"&amp;gt;(0|1)')
    isdonestatus = isdonestatus.search(searchstatus).groups()[0]
    if (isdonestatus == '1'):
        isnotdone = False
print "====&amp;gt;search status:  %s  &amp;lt;====" % isdonestatus

Error message produced:

====&amp;gt;search result:  [{"messages":[{"type":"ERROR","text":"Unauthorized"}]}]  &amp;lt;====
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I have read many questions about people having the same issue and have tried all of the solutions but the error still persists. I've tried changing the password to something that's only alphanumeric (no special characters) so nothing needs to be escaped. I've changed the password from default, I've made a new account with admin access for api calls and I've even tried printing out the password and username in the script at multiple points so that I can see that it doesn't change values etc. &lt;/P&gt;

&lt;P&gt;Any POST requests I make (running the search for example) will work fine, it's as soon as it comes to retrieving the information (GET requests) that there is a problem! Can anyone please provide any additional solutions that may help with this issue?&lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 13:40:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345830#M5410</guid>
      <dc:creator>Robbie1194</dc:creator>
      <dc:date>2018-02-05T13:40:32Z</dc:date>
    </item>
    <item>
      <title>Re: Why is access to Splunk API via Python generating an "Unauthorized" error?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345831#M5411</link>
      <description>&lt;P&gt;Can you paste your full script?  It seems you didn't include the section that prints the &lt;CODE&gt;search result&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;I wonder if your call to get the search results is lacking the authentication component, either using the same &lt;CODE&gt;httplib2&lt;/CODE&gt; object that you added credentials to, or by passing the session ID from a previous request.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 13:57:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345831#M5411</guid>
      <dc:creator>micahkemp</dc:creator>
      <dc:date>2018-02-05T13:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: Why is access to Splunk API via Python generating an "Unauthorized" error?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345832#M5412</link>
      <description>&lt;P&gt;I've posted the full script in the answers. &lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 14:02:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345832#M5412</guid>
      <dc:creator>Robbie1194</dc:creator>
      <dc:date>2018-02-05T14:02:13Z</dc:date>
    </item>
    <item>
      <title>Re: Why is access to Splunk API via Python generating an "Unauthorized" error?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345833#M5413</link>
      <description>&lt;P&gt;On lines 32 and 41 you're issuing GET requests but did not include the authorization header like you did on line 23.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 15:08:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345833#M5413</guid>
      <dc:creator>micahkemp</dc:creator>
      <dc:date>2018-02-05T15:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Why is access to Splunk API via Python generating an "Unauthorized" error?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345834#M5414</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;    import urllib
    import httplib2
    import time
    import re
    from time import localtime,strftime
    from xml.dom import minidom
    import json

    baseurl = 'splunkserver:8089'
    username = 'user'
    password = 'password'

   # Step 1: Get a session key
    servercontent = httplib2.Http(disable_ssl_certificate_validation=True).request(baseurl + '/services/auth/login', 'POST',
    headers={}, body=urllib.urlencode({'username':username, 'password':password}))[1]
    sessionkey = minidom.parseString(servercontent).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue
    print "====&amp;gt;sessionkey:  %s  &amp;lt;====" % sessionkey

   # Step 2: Create a search job
    searchquery = 'search index=notable | top limit=5 _time unique_id'

    searchjob = httplib2.Http(disable_ssl_certificate_validation=True).request(baseurl + '/services/search/jobs','POST',
    headers={'Authorization': 'Splunk %s' % sessionkey},body=urllib.urlencode({'search': searchquery}))[1]
    sid = minidom.parseString(searchjob).getElementsByTagName('sid')[0].childNodes[0].nodeValue
    print "====&amp;gt;sid:  %s  &amp;lt;====" % sid

    #Step 3: Get the search status
    httplib2.Http(disable_ssl_certificate_validation=True).add_credentials(username, password)
    servicessearchstatusstr = '/services/search/jobs/%s/' % sid
    isnotdone = True
    while isnotdone:
        searchstatus = httplib2.Http(disable_ssl_certificate_validation=True).request(baseurl + servicessearchstatusstr, 'GET')[1]
        isdonestatus = re.compile('isDone"&amp;gt;(0|1)')
        isdonestatus = isdonestatus.search(searchstatus).groups()[0]
        if (isdonestatus == '1'):
            isnotdone = False
    print "====&amp;gt;search status:  %s  &amp;lt;====" % isdonestatus

   # Step 4: Get the search results
    services_search_results_str = '/services/search/jobs/%s/results?output_mode=json&amp;amp;count=0' % sid
    searchresults = httplib2.Http(disable_ssl_certificate_validation=True).request(baseurl + services_search_results_str, 'GET')[1]
    print "====&amp;gt;search result:  [%s]  &amp;lt;====" % searchresults
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Feb 2018 15:08:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345834#M5414</guid>
      <dc:creator>Robbie1194</dc:creator>
      <dc:date>2018-02-05T15:08:11Z</dc:date>
    </item>
    <item>
      <title>Re: Why is access to Splunk API via Python generating an "Unauthorized" error?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345835#M5415</link>
      <description>&lt;P&gt;Can you format the code using the code button (the 101010 icon) so that it's easier to read?&lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 15:08:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345835#M5415</guid>
      <dc:creator>micahkemp</dc:creator>
      <dc:date>2018-02-05T15:08:12Z</dc:date>
    </item>
    <item>
      <title>Re: Why is access to Splunk API via Python generating an "Unauthorized" error?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345836#M5416</link>
      <description>&lt;P&gt;Okay, done &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 15:08:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345836#M5416</guid>
      <dc:creator>Robbie1194</dc:creator>
      <dc:date>2018-02-05T15:08:13Z</dc:date>
    </item>
    <item>
      <title>Re: Why is access to Splunk API via Python generating an "Unauthorized" error?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345837#M5417</link>
      <description>&lt;P&gt;Yeah i see what you mean! &lt;/P&gt;

&lt;P&gt;I'm new to Python so it might take me a while to work out how but I'll attempt to add the authorisation headers in the same way the POST commands did. &lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 15:24:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345837#M5417</guid>
      <dc:creator>Robbie1194</dc:creator>
      <dc:date>2018-02-05T15:24:05Z</dc:date>
    </item>
    <item>
      <title>Re: Why is access to Splunk API via Python generating an "Unauthorized" error?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345838#M5418</link>
      <description>&lt;P&gt;Although I did think the script looked okay at first because I was of the understanding that you don't typically use headers in get requests. &lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 15:25:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Why-is-access-to-Splunk-API-via-Python-generating-an-quot/m-p/345838#M5418</guid>
      <dc:creator>Robbie1194</dc:creator>
      <dc:date>2018-02-05T15:25:30Z</dc:date>
    </item>
  </channel>
</rss>

