Skip to content

Commit ffe4f68

Browse files
author
Jonathan Holvey
committed
Merge branch 'release-1.2.0'
2 parents 2a30a6a + fba3d24 commit ffe4f68

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed
File renamed without changes.

readme.md renamed to README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ s = sharepy.connect("example.sharepoint.com")
1919

2020
You will be prompted to enter your username and password, which are used to request a security token from Microsoft. An access cookie and request digest token are then retrieved and saved to properties for later use. The digest token will be refreshed automatically as it expires.
2121

22+
A username and password can also be provided as arguments of the `connect` function, if prompts are not desirable.
23+
2224
## Make an API call:
2325

2426
```python

session.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
}
1515

1616

17-
def connect(site):
18-
return SharePointSession(site)
17+
def connect(site, username=None, password=None):
18+
return SharePointSession(site, username, password)
1919

2020

2121
def load(filename="sp-session.pkl"):
2222
"""Load and return saved session object"""
2323
session = SharePointSession()
2424
session.__dict__.update(pickle.load(open(filename, "rb")))
2525
if session._redigest() or session._spauth():
26-
print("Connected to {} as {}\n".format(session.site, session.username))
26+
print("Connected to {} as {}".format(session.site, session.username))
2727
# Re-save session to prevent it going stale
2828
try:
2929
session.save(filename)
@@ -45,15 +45,15 @@ class SharePointSession(requests.Session):
4545
<Response [200]>
4646
"""
4747

48-
def __init__(self, site=None):
48+
def __init__(self, site=None, username=None, password=None):
4949
super().__init__()
50-
self.password = None
5150

5251
if site is not None:
5352
self.site = re.sub(r"^https?://", "", site)
5453
self.expire = datetime.now()
5554
# Request credentials from user
56-
self.username = input("Enter your username: ")
55+
self.username = username or input("Enter your username: ")
56+
self.password = password
5757

5858
if self._spauth():
5959
self._redigest()
@@ -75,18 +75,18 @@ def _spauth(self):
7575
site=self.site)
7676

7777
# Request security token from Microsoft Online
78-
print("Requesting security token...")
78+
print("Requesting security token...\r", end="")
7979
response = requests.post("https://login.microsoftonline.com/extSTS.srf", data=saml)
8080
# Parse and extract token from returned XML
8181
try:
8282
root = et.fromstring(response.text)
8383
token = root.find(".//wsse:BinarySecurityToken", ns).text
8484
except:
85-
print("Token request failed. Check your username and password\n")
85+
print("Token request failed. Check your username and password")
8686
return
8787

8888
# Request access token from sharepoint site
89-
print("Requesting access cookie...")
89+
print("Requesting access cookie... \r", end="")
9090
response = requests.post("https://" + self.site + "/_forms/default.aspx?wa=wsignin1.0",
9191
data=token, headers={"Host": self.site})
9292

@@ -98,10 +98,10 @@ def _spauth(self):
9898
if response.status_code == requests.codes.ok:
9999
self.headers.update({"Cookie": cookie})
100100
self.cookie = cookie
101-
print("Authentication successful\n")
101+
print("Authentication successful ")
102102
return True
103103
else:
104-
print("Authentication failed\n")
104+
print("Authentication failed ")
105105

106106
def _redigest(self):
107107
"""Check and refresh site's request form digest"""

0 commit comments

Comments
 (0)