Currently using Office365-REST-Python-Client==2.3.0.1
When I run the following code:
def upload_to_sharepoint():
url = 'https://tenant.sharepoint.com/sites/sitename'
credentials = {
'client_id': os.environ.get('client_id'),
'client_secret': os.environ.get('client_secret'),
}
context_auth = AuthenticationContext(url=url)
context_auth.acquire_token_for_app(client_id=credentials['client_id'],
client_secret=credentials['client_secret'])
ctx = ClientContext(url, context_auth)
web = ctx.web
ctx.load(web)
file_name = f"vat check {datetime.now().strftime('%Y-%m-%d')}"
path = os.path.join("/tmp", file_name)
with open(path, 'rb') as content_file:
file_content = content_file.read()
list_title = "VAT Check"
target_folder = ctx.web.lists.get_by_title(list_title).rootFolder
# name = os.path.basename(path)
target_file = target_folder.upload_file(file_name, file_content)
ctx.execute_query()
print(f"File url: {target_file.serverRelativeUrl}")
I get the following error, this seems like an error internally and not about the auth, or am I missing something?
AttributeError: 'ClientContext' object has no attribute '_auth_context'
That was fast, thanks @vgrem that solves the issue, tested it locally.
Greetings,
thank you for catching it! Turns out the instance of AuthenticationContext
got lost when passing into ClientContext
.
From another hand, since AuthenticationContext.acquire_token_for_app
is considered as _deprecated_ nowadays, another option (_recommended_) would be to switch from:
context_auth = AuthenticationContext(url=url)
context_auth.acquire_token_for_app(client_id=credentials['client_id'],
client_secret=credentials['client_secret'])
ctx = ClientContext(url, context_auth)
into
credentials = ClientCredential(credentials['client_id'], credentials['client_secret'])
ctx = ClientContext(settings['url']).with_credentials(credentials)
i am still getting this issue.
what could be the reason
You probably need to update your package: pip install --upgrade Office365-REST-Python-Client
@spurthikaribasaiah
Hi @erfannariman and @vgrem ,
I tried with above command. I am still getting the same issue.
Please find the code snippet below. I am using user credentials
pip install --upgrade pip
pip install Office365-REST-Python-Client
pip install --upgrade Office365-REST-Python-Client
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
ctx_auth = AuthenticationContext(Sharepoint_SiteUrl)
ctx_auth.acquire_token_for_user(User, Pass)
ctx = ClientContext(Sharepoint_SiteUrl, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Authentication successful")