Reputation: 455
I am trying to work with a sharepoint site that use my windows authentication. I can use the requests
module to access the site but it requires I explicitly state my windows password.
import requests
from requests_ntlm import HttpNtlmAuth
SITE = "https://sharepointsite.com/"
PASSWORD = "pw"
USERNAME = "domain\\user"
response = requests.get(SITE, auth=HttpNtlmAuth(USERNAME,PASSWORD))
print response.status_code
Is there a way for Python to access the site through windows authentication so I don't have to provide my password? It seems like this might be possible through requests_nltm
but I can't work out how.
Upvotes: 13
Views: 37757
Reputation: 710
The accepted answer still uses a stored password. An option to use integrated authentication via the Windows SSPI interface would be the following:
import requests
from requests_negotiate_sspi import HttpNegotiateAuth
cert = 'path\to\certificate.cer'
response = requests.get(
r'http://mysharepoint.com/_api',
auth=HttpNegotiateAuth(),
verify=cert)
print(response.status_code)
See here for more information.
Upvotes: 3
Reputation: 1599
Have you considered storing your username and password as environmental variable on the machine that is running the script? This will prevent you from having to store the sensitive information within the script itself. Then only the machine's admin can access/modify the sensitive information.
Through cmd prompt
, set the desired variables (below syntax is for a Windows machine):
SET username=domain\\user
SET password=your_password
To ensure you've correctly set the variables, type SET
into the cmd prompt
and see if the variables are listed.
Once correctly set, then use python's os
module to access the variables and use as desired:
import os
import requests
from requests_ntlm import HttpNtlmAuth
username = os.environ.get('username')
password = os.environ.get('password')
SITE = "https://sharepointsite.com/"
response = requests.get(SITE, auth=HttpNtlmAuth(username, password))
IMPORTANT NOTES:
cmd prompt
window, the environment variables you just set will be erased and your script will throw a "I can't find environment variables" error. To avoid this, either always keep the cmd
window open while the script runs, or permanently set the environment variables (instructions here for Windows machines. Note: the instructions refer to changing the PATH
environment variable but you'll get the idea on how to create/modify your own variables).SET
into cmd prompt
).Upvotes: -3
Reputation: 7906
If you don't want to explicitly state your windows password you could use the getpass
module:
import requests
from requests_ntlm import HttpNtlmAuth
import getpass
SITE = "https://sharepointsite.com/"
USERNAME = "domain\\user"
response = requests.get(SITE, auth=HttpNtlmAuth(USERNAME, getpass.getpass()))
print response.status_code
This way you don't have to store you password in plain text.
Looking at the code for the requests_ntlm
there isn't a way to use it without supplying your password or the hash of your password to HttpNtlmAuth
Upvotes: 11