I don't know much of anything about SharePoint, but was tasked to use Python to download a SharePoint list. SharePoint is Office365 and is accessed with Single Sign-on. I found the below sample on how to connect to SharePoint:
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
ctx = ClientContext('https://<site>.sharepoint.com').with_credentials(UserCredential('domain\\user', 'password'))
web = ctx.web
ctx.load(web)
ctx.execute_query()
print(web.properties["Url"])
When executing this code I receive the following error:
Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf
KeyError: 'FedAuth'
Can anyone point me in the right direction?
Recently when I was searching for similar kind of solution, I found something useful, you can check below code on github.
https://github.com/vgrem/Office365-REST-Python-Client
Related
I am trying to connect to sharepoint from python, to upload files. Right now the problem is that I am not sure if I am connecting correctly.
I am following this code : https://github.com/bashamsc/sharepoint_python/blob/main/sharepoint_read_file_python.py
#pip install Office365-REST-Python-Client
#Importing required libraries
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
#Constrtucting SharePoint URL and credentials
sharepoint_base_url = 'https://mycompany.sharepoint.com/sites/sharepointname/'
sharepoint_user = 'user#anothercompany.com'
sharepoint_password = 'pwd'
folder_in_sharepoint = '/sites/sharepointname/Shared%20Documents/YourFolderName/'
#Constructing Details For Authenticating SharePoint
auth = AuthenticationContext(sharepoint_base_url)
auth.acquire_token_for_user(sharepoint_user, sharepoint_password)
but in the executing the last line in jupyter notebook,I got:
An error occurred while retrieving auth cookies from
https://mycompany.sharepoint.com/_forms/default.aspx?wa=wsignin1.0
Following that link, it tells me, in the last line:
Issue Type: Guest user is signing-in but external sharing is disabled.
I dont know what to do, to have a successful connection list and upload files in the SharePoint folder.
I might have a permission problem, in Change the organization-level external sharing setting, the error message looks like "only people in your organization" from https://learn.microsoft.com/en-us/sharepoint/turn-external-sharing-on-or-off . The email I am using does not belong to mycompany.com (who owns the SharePoint), it comes from another company.
Is there anyway around this? Am I correct to think that something have to be change in the SharePoint permission to be able to connect?
I have python code to connect to sharepoint and download files to dbfs.
The code was working fine till yesterday, but as of today I am getting 'List Index out of range Error'
The error occurs while retrieving the authentication token from sharepoint.
Below is the code that was working fine till today
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
`ctx_auth = AuthenticationContext(url_shrpt)
if ctx_auth.acquire_token_for_user(username_shrpt, password_shrpt):
ctx = ClientContext(url_shrpt, ctx_auth)
I had the same issue when I first tried.
The problem I had was that my credentials were not correct.
Double check your username and password
The credentials are correct ,we have a code in place to download from sharepoint using the same account and it is working fine.We are using basic authentication to connect to outlook
We are trying to access sharepoint and we are succeeding depending on whether or not the specific sharepoint section is protected with "SecureAuth".
The program works fine if the url is not protected with "SecureAuth", but if this URL is protected with "SecureAuth" it returns this error. Is there any way to fix it?
We are using:
from shareplum import Site
from shareplum import Office365
from shareplum.site import Version
authcookie = Office365(sharepoint_address, username=sharepoint_user,\
password=sharepoint_user_pw).GetCookies()
site = Site(f"https://myWeb.com/sites/{sharepoint_site}/", version=Version.v365, authcookie=authcookie)
folder = site.Folder(sharepoint_folder) # here come the error
Depending of {sharepoint_site} its working or not.
Its the same error but its not related to that topic
It's not clear if the SharePoint that you need to access is OnPremises or Online (Office 365), but I'll share all possibilities and respective approaches:
If the SharePoint site address cotains the format https://[tenant].sharepoint.com/sites/BusinessAreaOfCompany, where [tenant] is a string that suggests reference of the company. This site hosted in an Office 365 environment; than, the code example above that you share (AND the user needs the minimal required permissions), is like this:
from shareplum import Site
from shareplum import Office365
authcookie = Office365('https://[tenant].sharepoint.com', username='username#[tenantdomain].com', password='password').GetCookies()
site = Site('https://[tenant].sharepoint.com/sites/BusinessAreaOfCompany', authcookie=authcookie)
Else, if the SharePoint Site url uses any company domain different of the TENANT address, for example https://xyz.company.com/sites/BusinessAreaOfCompany, the SharePoint is an OnPremises Environment, and uses NTLM or Kerberos for authenticate. The appropriate code approach example is (REMEMBER: the user needs the minimal required permissions):
from shareplum import Site
from requests_ntlm import HttpNtlmAuth
cred = HttpNtlmAuth('Username', 'Password')
site = Site('https://xyz.company.com/sites/BusinessAreaOfCompany', auth=cred)
Reference: SharePlum: Python + SharePoint
For me it was a language issue. Our sharepoint-filesystem is in german, so I changed from
'Shared Documents' to 'Freigegebene Dokumente'
in the folder path and everything works fine.
I am trying to get access to a Sharepoint site from Python and it seems like AADSTS has a conditional access policy that’s preventing me from getting a token.
Has anyone used the Office365 Python Rest Client to successful connect to a Sharepoint Rest Endpoint?
This is the actual error I am seeing
"C:\Python\envs\dev\python.exe c:\Users\xxxxxx.vscode\extensions\ms-python.python-2020.8.105369\pythonFiles\lib\python\debugpy\launcher 52801 -- c:\xxxx\Code\PythonSharepointUpdate\readsharepoint.py " An error occurred while retrieving token from XML response: AADSTS53003: Access has been blocked by Conditional Access policies. The access policy does not allow token issuance. An error occurred while retrieving auth cookies from https://microsoft.sharepoint.com/_vti_bin/idcrl.svc/
I am trying to execute this code from the Sample
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.authentication_context import AuthenticationContext
site_url = 'https://microsoft.sharepoint.com/teams/'
ctx = ClientContext(site_url).with_credentials(UserCredential("user#domain.com", "My Complex Password”))
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web title: {0}".format(web.properties['Title']))
Also – is there a better way to authenticate to Sharepoint – I hate to have to type my Password in clear text in code ☹
Has anyone use ClientCredential? Is there a seperate setup on the Sharepoint Site to enable ClientCredential? Does our Sharepoint let us do that?
The trick is to use ClientCredentials and not UserCredentials.
One will have to create a new app on the Sharepoint site with a new CliendID and Client Secret. After creating a new app, the newly created app has to be given permissions on the site. Once the permissions are set, you will have to Trust the app.
The full explanation and the steps are documented here https://github.com/vgrem/Office365-REST-Python-Client/wiki/How-to-connect-to-SharePoint-Online-and-and-SharePoint-2013-2016-2019-on-premises--with-app-principal
Seek your guidance, I am new #Python and in learning phase.
Problem Statement: I want to connect & Download the xlsx file from my office SharePoint 2013 site.
Steps so far:
import sharepy
s = sharepy.connect("https://office.XXX.com/sites/pkdyns")
r = s.getfile("https://office.XXX.com/sites/pkdyns/Shared%20Documents/YYYY.xlsx")
r = s.getfile("https://office.XXX.com/sites/pkdyns/Shared%20Documents/YYYY.xlsx", filename="/PYTHON/YYYY.xlsx")
After this I dont see the downloaded file, not sure if it ran or not, using Jupyter Notebook but it didnt throw up any error. Please suggest.
sharepy library seems to only support sharepoint online.
SharePy - Simple SharePoint Online authentication for Python
Below is a sample about how to download file from sharepoint, you may take a reference( it uses another library):
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File
ctx = ClientContext.connect_with_credentials(url,UserCredential(username, password))
web = ctx.web
ctx.load(web)
ctx.execute_query()
print "Web title: {0}".format(web.properties['Title'])
path = "../../tests/data/SharePoint User Guide.docx"
response = File.open_binary(context, "Shared Documents/SharePoint User Guide.docx")
response.raise_for_status()
with open(path, "wb") as local_file:
local_file.write(response.content)