Splunk Dev

Python Splunk API example?

obviouslygene
Explorer

Working on using the python rest api example, I get an error on step 3

myhttp.add_credentials(username, password)
servicessearchstatusstr = '/services/search/jobs/%s/' % sid

For this step, I have defined the password above and have used for a curl syntax and it works.
But when I run the code, it will always return an Unauthorized response.

I tried changing password to 'actualpass'
eg.
myhttp.add_credentials(username, 'actual pass')
...and it runs.

i also tried printing the variable password above that line and it stills shows me the correct password.
I went one step further and tested with another variable above it
eg.
pwd = 'actual pass'
myhttp.add_credentials(username, pwd)
and it works.

if I write
eg.
pwd = password
myhttp.add_credentials(username, password)
it does not run.

I apologise if this has been covered before, but I have searched and searched but to no avail.
I am just starting out but I hope to learn more.

Regards,
Eugene

Tags (3)
0 Karma

obviouslygene
Explorer

I was pulling the password from a credentials.csv and I realized that even nothing worked, It kept telling me that I was unauthorized.
I switched the first part of the example to run by curl and it passed the password variable through fine.
Step 2 worked as well, but Step 3 I couldn't get it to work.
I then found out why, there was a return carriage in the .csv file some how, even though it didnt look like it.

So this was what I did to solve it. The code looked something like this (bear with me I'm pretty new to coding.)

f = open('login.csv', 'rb')
for field in f.readlines():
    fsc = field.rstrip()
    password = fsc.split(',')[1]
f.close()

That solved the issue.

ChrisG
Splunk Employee
Splunk Employee

Can you please provide the URL for the example page you are looking at?

0 Karma

obviouslygene
Explorer

Hey Chris,
Thanks for the reply!
The URL is http://blogs.splunk.com/2011/08/02/splunk-rest-api-is-easy-to-use/
But I found out one of the reasons why its not working.
There was a return carriage hidden when a the login credential file was created.

Sorry for the trouble, but it sure taught me a ton about checking those stuff first...

0 Karma

jplumsdaine22
Influencer

@obviouslygene, glad you figured it out! Would you mind putting that down as an answer below and accepting it? If someone in the future has the same problem it will really help them out!

obviouslygene
Explorer

ok will post down below.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Data Management Digest – May 2026

Welcome to the May 2026 edition of Data Management Digest!   As your trusted partner in data innovation, the ...

Index This | What is feather-light but cannot be held long?

May 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

.conf26 Registration is Live: Secure Your Early Bird Pass Now

  Lock in Your Spot: Registration Open for .conf26 in Denver Hello Splunkers, I have exciting news! Your ...