I am trying to get a lost of files from my onedrive using a python script but it's giving an error and I am using Application permissions only but it is giving an error while getting access token.
Here is my code-
import requests
import json
# Define the necessary variables
client_id = 'my client id'
client_secret = 'my client secret'
tenant_id = 'my tenant id'
# Get an access token using application permission
url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token'
data = {'grant_type': 'client_credentials','client_id': client_id,'client_secret': client_secret, 'scope': 'https://graph.microsoft.com/.default'}
response = requests.post(url, data=data)
access_token = json.loads(response.text)['access_token']
print(access_token)
# Get the list of files in the root directory
url = 'https://graph.microsoft.com/v1.0/drive/root/children'
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(url, headers=headers)
print(response.text)
files = json.loads(response.text)['value']
# Print the file names
for file in files:
print(file['name'])
error-
{'error': {'code': 'BadRequest', 'message': 'Unable to retrieve tenant service info.', 'innerError': {'date': '2023-03-27T23:39:07', 'request-id': 'a04737f4-8614-4841-9a45-1b1dc1d25b16', 'client-request-id': 'a04737f4-8614-4841-9a45-1b1dc1d25b16'}}}