Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'ClientContext' object has no attribute '_auth_context' #302

Closed
erfannariman opened this issue Dec 7, 2020 · 7 comments
Closed
Labels

Comments

@erfannariman
Copy link

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'
@vgrem vgrem added the bug label Dec 7, 2020
@erfannariman
Copy link
Author

That was fast, thanks @vgrem that solves the issue, tested it locally.

@vgrem
Copy link
Owner

vgrem commented Dec 7, 2020

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)

@vgrem vgrem closed this as completed Dec 7, 2020
@spurthikaribasaiah
Copy link

i am still getting this issue.
what could be the reason

@erfannariman
Copy link
Author

You probably need to update your package: pip install --upgrade Office365-REST-Python-Client @spurthikaribasaiah

@spurthikaribasaiah
Copy link

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")

@spurthikaribasaiah
Copy link

image

@hasanradi93
Copy link

Hello @spurthikaribasaiah I have the same problem as you, how you find the solution, can you help me please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants