Skip to content

Commit

Permalink
Merge branch 'release-1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Holvey committed Feb 23, 2018
2 parents 2a30a6a + fba3d24 commit ffe4f68
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
File renamed without changes.
2 changes: 2 additions & 0 deletions readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ s = sharepy.connect("example.sharepoint.com")

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.

A username and password can also be provided as arguments of the `connect` function, if prompts are not desirable.

## Make an API call:

```python
Expand Down
22 changes: 11 additions & 11 deletions session.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
}


def connect(site):
return SharePointSession(site)
def connect(site, username=None, password=None):
return SharePointSession(site, username, password)


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

def __init__(self, site=None):
def __init__(self, site=None, username=None, password=None):
super().__init__()
self.password = None

if site is not None:
self.site = re.sub(r"^https?://", "", site)
self.expire = datetime.now()
# Request credentials from user
self.username = input("Enter your username: ")
self.username = username or input("Enter your username: ")
self.password = password

if self._spauth():
self._redigest()
Expand All @@ -75,18 +75,18 @@ def _spauth(self):
site=self.site)

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

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

Expand All @@ -98,10 +98,10 @@ def _spauth(self):
if response.status_code == requests.codes.ok:
self.headers.update({"Cookie": cookie})
self.cookie = cookie
print("Authentication successful\n")
print("Authentication successful ")
return True
else:
print("Authentication failed\n")
print("Authentication failed ")

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

0 comments on commit ffe4f68

Please sign in to comment.