Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to login the sharepoint by passing the username and password?

I am using the sharepy and logging to connect the sharepoint. I have used the bellow code to connect

import sharepy
import logging


SPUrl = "https://vvv.sharepoint.com"

username = "[email protected]"
password = "aaa@123"
s = sharepy.connect(SPUrl,username,password)
s.save()

headers = {"accept": "application/json;odata=verbose",
"content-type": "application/x-www-urlencoded; charset=UTF-8"}
fileToUpload = "copyUpload.py"

with open(fileToUpload, 'rb') as read_file:
    content = read_file.read()
p = s.post("https://aaa.sharepoint.com/sites/vvv/_api/web/getfolderbyserverrelativeurl('/sites/aaa/bbb/')/Files/add(url='"+fileToUpload+"',overwrite=true)", data=content, headers=headers)

print(fileToUpload+" Uploaded in SP")
os.remove(fileToUpload)
logging.info("Uploaded file: with response file")

While I am passing the values into the connect function it is throwing the following error

AttributeError: 'SharePointSession' object has no attribute 'cookie'

Suppose, If I didn't pass the value as a argument that time in the terminal it will ask for the username and password after typing the it on the terminal it is working fine.

But how can I make it problematically?

I am facing the following error

Traceback (most recent call last):
File "copyUpload.py", line 18, in <module>
    p = s.post("https://aaa.sharepoint.com/sites/Graphite/_api/web/getfolderbyserverrelativeurl('/sites/aaa/bbb/')/Files/add(url='"+fileToUpload+"',overwrite=true)", data=content, headers=headers)
File "/usr/local/lib/python3.4/dist-packages/sharepy/session.py", line 135, in post
    kwargs["headers"]["Authorization"] = "Bearer " + self._redigest()
File "/usr/local/lib/python3.4/dist-packages/sharepy/session.py", line 111, in _redigest
    data="", headers={"Cookie": self.cookie})
AttributeError: 'SharePointSession' object has no attribute 'cookie'
like image 478
mkHun Avatar asked May 24 '18 11:05

mkHun


People also ask

How do I force SharePoint login?

Please type “Access work or school” in the search box and disconnect the accounts. For your requirement “force users to login”, you could go to Microsoft Azure> Conditional Access and create a policy and set sign-in frequency to make sure users will see the disclaimer.

How do I find user ID in SharePoint?

In SharePoint REST API is quite simple and straightforward, use User ID to get user Title, Email for SharePoint 2013, and apps for SharePoint. use /_api/web/getuserbyid(ID) to get the user field in the response data, we have an “AuthorId” that will get us the user Title, Email, etc.

How do I change my password in SharePoint?

Go to your SharePoint Central Administration site >> Click on Security >> Configure Managed Accounts. Click on “Edit” icon next to the SharePoint Farm account. Tick “Change password now” check box and enter the new password by setting “Set account password to new value” option.


Video Answer


1 Answers

The library sharepy stores a cookie if the authentication is successful. You can check fore example with

if not hasattr(s, 'cookie'):
    print("authentication failed!"); quit()

if the authentication has failed, before you save the SharePoint session.

Common errors

  • wrong username (check that the domain name in your username matches your site)
  • wrong password
  • wrong site url (NO additional characters after sharepoint.com, especially no "/")
like image 104
W. Schmid Avatar answered Sep 27 '22 22:09

W. Schmid