Forum Discussion
API to access MS forms
Hello community,
I'm currently working on a project where I need to access various URLs related to Microsoft Forms from my Python backend. However, I'm facing a 403 error when attempting to do so. Here's a breakdown of what I'm trying to achieve and the steps I've taken so far:
URLs to access:
Retrieve all Forms for a Microsoft 365 Group:
https://forms.office.com/formapi/api/{tenantId}/groups/{groupId}/forms
Obtain details for a group form:
https://forms.office.com/formapi/api/{tenantId}/groups/{groupId}/forms('{formId}')
Fetch questions from a group form:
https://forms.office.com/formapi/api/{tenantId}/groups/{groupId}/forms('{formId}')/questions
Retrieve responses to a group form:
https://forms.office.com/formapi/api/{tenantId}/groups/{groupId}/forms('{formId}')/responses
Objective: Access URLs for Microsoft Forms data from the backend.
Approach:
1. First, I log in to the MS Forms website.
2. Then, I append the necessary parameters (tenantId and groupId) to the URLs I want to access, and I get browser responses in JSON format.
3. Now using backend requests, I attempt to access these URLs by passing the required authentication token.
Issue: Despite successfully obtaining the authentication token using the provided code snippet, when I tried to access the Microsoft Forms URLs with the generated token, I received a 403 error.
Here's the code snippet I'm using to obtain the token:
=====================================================
token_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://forms.office.com/.default"
}
response = requests.post(token_url, data=data)
response.raise_for_status() # Raise an exception for non-200 status codes
=====================================================
And here's the code snippet I'm using to access the Microsoft Forms URLs with the obtained token:
=====================================================
headers = {
'Authorization': f'Bearer {response.json()["access_token"]}',
'Content-Type': 'application/json',
}
base_url = "https://forms.office.com/formapi/api/{tenantId}/groups/{groupId}/forms"
list_response = requests.get(url=base_url, headers=headers)
=====================================================
I'm uncertain why I'm receiving a 403 error despite having a valid token. I followed the instructions mentioned in this post's replies but was unsuccessful.
Any insights or assistance on resolving this issue would be highly appreciated.
Thank you in advance!