I developed the following python code below.
On the other hand, in azure, I created a new app where I got the client Id and tenant ID and to later on, I gave the API permission (check picture):
as well as the certificate and secrets by (check picture):
The result I get from the below code is
Access token obtained: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyIsImtpZCI6Ii1LSTNROW5OUjd...... (an extended token)
Authorization failed, status code: 401
So, the code is giving me the token but this is unable to be verified when it goes to the sharepoint. I tried everything but nothing works, I gave up.
I wish you have a solution or another way to verify my account to the specific sharepoint folder.
The folder where I need to check my access is in this one : "https://vestas.sharepoint.com/sites/DEP-TurbineEngineering-VAME/Shared Documents/General/Files for BI"
The code is :
import requests
#replace the following with your own values
client_id = "12345"
client_secret = "aabbccc"
tenant_id = "12345aabbcc"
resource = "https://vestas.sharepoint.com/"
#get the access token
auth_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/token" data = {
"client_id": client_id, "client_secret": client_secret, "resource": resource, "grant_type": "client_credentials",
}
response = requests.post(auth_url, data=data)
if response.status_code == 200:
auth_response = response.json()
access_token = auth_response["access_token"]
print("Access token obtained:", access_token)
else:
print("Failed to obtain access token, status code:", response.status_code)
#Replace the placeholder with the actual SharePoint API endpoint api_endpoint = "https://vestas.sharepoint.com/sites/DEP-TurbineEngineering-VAME/Shared Documents/General/Files for BI"
headers = { "Authorization": "Bearer " + access_token, "Accept": "application/json;odata=verbose" }
response = requests.get(api_endpoint, headers=headers)
#Check if the request was successful
if response.status_code == 200:
data = response.json()
print("Authorization successful")
#Do something with the data obtained from the API
else:
print("Authorization failed, status code:", response.status_code)