Connecting to Sharepoint from python - python

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?

Related

unable to connect to SharePoint using python error: list index out of range?

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

SecureAuth cause - ShareplumRequestError: Shareplum HTTP Post Failed : 403 Client Error: Forbidden for url

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.

Access Office365 SharePoint with Single Sign-On via Python

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

Accessing Office365 Sharepoint REST EndPoints using Python (Office365 Sharepoint REST client for Python)

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

Uploading file with python returns Request failed with status code', 403, 'Expected one of', <HTTPStatus.OK: 200>

blob.upload_from_filename(source) gives the error
raise exceptions.from_http_status(response.status_code, message, >response=response)
google.api_core.exceptions.Forbidden: 403 POST >https://www.googleapis.com/upload/storage/v1/b/bucket1-newsdata->bluetechsoft/o?uploadType=multipart: ('Request failed with status >code', 403, 'Expected one of', )
I am following the example of google cloud written in python here!
from google.cloud import storage
def upload_blob(bucket, source, des):
client = storage.Client.from_service_account_json('/path')
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket)
blob = bucket.blob(des)
blob.upload_from_filename(source)
I used gsutil to upload files, which is working fine.
Tried to list the bucket names using the python script which is also working fine.
I have necessary permissions and GOOGLE_APPLICATION_CREDENTIALS set.
This whole things wasn't working because I didn't have permission storage admin in the service account that I am using in GCP.
Allowing storage admin to my service account solved my problem.
As other answers have indicated that this is related to the issue of permission, I have found one following command as useful way to create default application credential for currently logged in user.
Assuming, you got this error, while running this code in some machine. Just following steps would be sufficient:
SSH to vm where code is running or will be running. Make sure you are user, who has permission to upload things in google storage.
Run following command:
gcloud auth application-default login
This above command will ask to create token by clicking on url. Generate token and paste in ssh console.
That's it. All your python application started as that user, will use this as default credential for storage buckets interaction.
Happy GCP'ing :)
This question is more appropriate for a support case.
As you are getting a 403, most likely you are missing a permission on IAM, the Google Cloud Platform support team will be able to inspect your resources and configurations.
This is what worked for me when the google documentation didn't work. I was getting the same error with the appropriate permissions.
import pathlib
import google.cloud.storage as gcs
client = gcs.Client()
#set target file to write to
target = pathlib.Path("local_file.txt")
#set file to download
FULL_FILE_PATH = "gs://bucket_name/folder_name/file_name.txt"
#open filestream with write permissions
with target.open(mode="wb") as downloaded_file:
#download and write file locally
client.download_blob_to_file(FULL_FILE_PATH, downloaded_file)

Categories