Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

Something wrong with OneDrive api

Open underthestars-zhy opened this issue 4 years ago • 2 comments

import msal
from office365.graph_client import GraphClient


def acquire_token_func():
    """
    Acquire token via MSAL
    """
    authority_url = 'https://login.microsoftonline.com/xxxx'
    app = msal.ConfidentialClientApplication(
        authority=authority_url,
        client_id='xxxx',
        client_credential='xxxxx'
    )
    token = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
    return token


print(acquire_token_func())
tenant_name = "contoso.onmicrosoft.com"
client = GraphClient(acquire_token_func)
drives = client.drives.get().execute_query()
for drive in drives:
    print("Drive url: {0}".format(drive.web_url))

I can get the token, but then something is wrong.

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/office365/runtime/client_request.py", line 94, in execute_query response.raise_for_status() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/requests/models.py", line 943, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://graph.microsoft.com/v1.0/drives

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/zhuhaoyumbp15/Desktop/UTS/OneDrive/main.py", line 22, in drives = client.drives.get().execute_query() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/office365/runtime/client_object.py", line 41, in execute_query self.context.execute_query() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/office365/runtime/client_runtime_context.py", line 132, in execute_query self.pending_request().execute_query() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/office365/runtime/client_request.py", line 98, in execute_query raise ClientRequestException(*e.args, response=e.response) office365.runtime.client_request_exception.ClientRequestException: ('AccessDenied', 'Either scp or roles claim need to be present in the token.', '403 Client Error: Forbidden for url: https://graph.microsoft.com/v1.0/drives')

underthestars-zhy avatar Nov 18 '21 12:11 underthestars-zhy

I'm having the same issue. Eager to see if this gets resolved.

peraltae avatar Nov 29 '21 20:11 peraltae

Greetings,

most likely application has not been configured properly.

A few things to verify:

JWT token for client credentials could also be verified in terms if it contains roles property by examining it, for instance, via jwt.io

vgrem avatar Dec 06 '21 19:12 vgrem