I have the following code that I believe would work for most sharepoint sites, but am receiving the following error:
Exception("Check username/password and rootsite")
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
url = 'https://company.sharepoint.com'
username = 'user123#company.com'
password = 'password'
listname = 'Test List'
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
sp_list = ctx.web.lists.get_by_title(listname)
items = sp_list.get_items()
ctx.load(items)
ctx.execute_query()
else:
print(ctx_auth.get_last_error())
How would I manipulate this code to ensure that I can pull data from a SharePoint List into Python?
I would suggest you check Office365-REST-Python-Client
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
sp_list = ctx.web.lists.get_by_title(listname)
items = sp_list.get_items()
ctx.load(items)
ctx.execute_query()
# to do
else:
print ctx_auth.get_last_error()
Related
I am trying to read an excel file from SharePoint to python and I get the following error:
ValueError: Excel file format cannot be determined, you must specify an engine manually.
The python script goes something like that:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import io
import pandas as pd
url = "https://company.sharepoint.com/:x:/r/sites/A_team/"
username = 'myemail#mail.com'
password = 'password'
relative_url = "sites/A_team/Documents_python/Folder/Book.xlsx"
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Authentication successful")
else:
print(ctx_auth.get_last_error())
response = File.open_binary(ctx, relative_url)
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0) #set file object to start
pd.read_excel(bytes_file_obj)
Does anyone have an idea what should I do?
I tried to add an engine, but then I get the following error:
BadZipFile: File is not a zip file
Getting an error when connecting to SharePoint
'Error authenticating against Office 365. Error from Office 365:', "AADSTS50053: The account is locked, you've tried to sign in too many times with an incorrect user ID or password.")
Here is my code, I think I've everything set up right, but the URLs might be wrong, here is the complete URL to the Testing Folder I want to upload too
https://abc.sharepoint.com/teams/TestUpload/Shared%20Documents/Forms/AllItems.aspx?id=%2Fteams%2FTestupload%2FShared%20Documents%2FTesting%20Folder&viewid=e0db9acc%2Dfe4d%2D4acf%2Dbd25%2D66384f546239
from shareplum import Office365
from shareplum import Site
from shareplum.site import Version
#Logging info
server_url = "https://abc.sharepoint.com/"
site_url = server_url + "Teams/Testupload"
Username = 'johnDoe.abc.com'
Password = 'fakepass!'
Sharepoint_folder = 'Shared Documents'
fileName = r'C:\Users\co123abc\OneDrive - abc\Desktop\Files\test\aaa.docx'
def file_upload_to_sharepoint(**context):
authcookie = Office365(server_url, username = Username, password=Password).GetCookies()
site = Site(site_url, version=Version.v365, authcookie=authcookie)
folder = site.Folder(Sharepoint_folder)
with open(fileName, mode='rb') as file:
fileContent = file.read()
folder.upload_file(fileContent, "filename.bin")
file_upload_to_sharepoint()
I have a SP online folder and would like to read all the excel files in that folder. here's my codes. The codes below only able to direct me to the path. But I am stuck on the codes that want to combine all the data inside those files in the folder and read them.
url = 'https://abc.sharepoint.com/sites/GlobalMaterials'
username = 'user'
password = 'password'
folder_url = '/sites/GlobalMaterialss/Shared Documents/abc folder'
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print('Authenticated into sharepoint as: ',web.properties['Title'])
else:
print(ctx_auth.get_last_error())
I need to download a file from sharepoint using user credentials (rather that client credentials).
I've tried this:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
root_url = "https://company-my.sharepoint.com"
full_url = "https://company-my.sharepoint.com/personal/Documents/AB.csv"
ctx = ClientContext(root_url)
ctx.with_user_credentials(<my_email>,
<my_password)
response = File.open_binary(ctx, full_url)
print(response.content)
The response I am getting is
*b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"serverRelativePath\\r\\nParameter
name: Specified value is not supported for the serverRelativePath
parameter."}}}'*
I am trying to access share point site as api using python to download files.
So far I have tried "shareplum" , "office365", and "sharepy" modules in python.
Every time I am getting authentication error due to invalid username or password.
But I can logon to that share point site from web browser using same username and password.
What approach should I follow or what sort of authentication should I use ?
Regards
Santanu
shareplum:
from shareplum import Site, Office365
from shareplum.site import Version
import json
import os
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
config_path = '\\'.join([ROOT_DIR, 'config_1.json'])
with open(config_path) as config_file:
config = json.load(config_file)
config = config['share_point']
USERNAME = config['user']
PASSWORD = config['password']
SHAREPOINT_URL = config['url']
SHAREPOINT_SITE = config['site']
SHAREPOINT_DOC = config['doc_library']
def auth():
authcookie = Office365(SHAREPOINT_URL, username=USERNAME, password=PASSWORD).GetCookies()
print(f"authcookie: {authcookie}")
site = Site(SHAREPOINT_SITE, version=Version.v365, authcookie=authcookie)
print(f"site: {site}")
return site
def connect_folder(folder_name):
auth_site = auth()
print(f"auth_site: {auth_site}")
sharepoint_dir = '/'.join([SHAREPOINT_DOC, folder_name])
print(f"sharepoint_dir: {sharepoint_dir}")
folder = auth_site.Folder(sharepoint_dir)
print(f"folder: {folder}")
return folder
def download_file(file_name, folder_name):
_folder = connect_folder(folder_name)
print(f"_folder: {_folder}")
return _folder.get_file(file_name)
def run():
# set file name
file_name = 'Hardy_ServiceReporting_Aug2021.csv'
# set the folder name
folder_name = 'Problem Management'
# get file
file = download_file(file_name, folder_name)
print(file)
if __name__ == "__main__":
run()
Error: Error from Office 365:', 'AADSTS50126: Error validating credentials due to invalid username or password.'
office365:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import json
import os
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
config_path = '\\'.join([ROOT_DIR, 'config_1.json'])
with open(config_path) as config_file:
config = json.load(config_file)
config = config['share_point']
USERNAME = config['user']
PASSWORD = config['password']
SHAREPOINT_URL = config['url']
SHAREPOINT_SITE = config['site']
SHAREPOINT_DOC = config['doc_library']
FOLDER = "Problem Management"
url = SHAREPOINT_SITE + "/_api/web/GetFolderByServerRelativeUrl('{}/{}')/Files".format(SHAREPOINT_DOC, FOLDER)
print(f"url: {url}")
ctx_auth = AuthenticationContext(url)
print(f"ctx_auth: {ctx_auth}")
ctx_auth.acquire_token_for_user(USERNAME, PASSWORD)
ctx = ClientContext(url, ctx_auth)
print(f"ctx: {ctx}")
Error: 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.