Forum Discussion
ravipokala
Jun 15, 2020Copper Contributor
API to access MS forms
Looking for an API to access MS forms and pull the survey results on a weekly basis.
- jmistry307Copper Contributor
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! - MockmyThe1stCopper ContributorAnyone Found if there is a way to programmatically create a form?
Found already how to get the responses, but no way how to create the Forms- NunoN370Copper Contributor
MockmyThe1stthere is a video that shows that it is possible to create a form from Power Apps here.
Although many details are missing, so I haven't figured it out yet.
Have you managed to find a solution?
- devglCopper ContributorHi MockmyThe1st, may I know do you get the form responses? I'm following the steps from others' comments to setup the API permissions, but I'm getting 403.
- MockmyThe1stCopper Contributor
devgl: Haven't read the steps above for permissions.
I have set it up in a Power automate flow directly.Just used the forms connector from there and my MS account.
- RobElliottSilver Contributor
MockmyThe1st there is no way to create forms programmatically. There are various requests on the official Microsoft feedback portal relating to the api which you can vote for, for example here.
Rob
Los Gallardos
Intranet, SharePoint and Power Platform Manager (and classic 1967 Morris Traveller driver)
- Leandro_AbadeCopper Contributor
ravipokala I'm using VBA + SeleniumBasic-2.0.9.0
You must first open the URL “https://forms.office.com/Pages/DesignPage.aspx” to obtain a token
Then you can call "https://forms.office.com/formapi/DownloadExcelFile.ashx?formid=" + put your form ID Here + "&timezoneOffset=180&minResponseId=1&maxResponseId=1000"
This is working for me- Christian ZenzanoBrass Contributorwhen using https://forms.office.com/formapi/DownloadExcelFile.ashx?formid= + formID i get the following error from PowerBI
we were unable to connect because this credential type isn't supported for this resource. please choose another credential type.
what is interesting is that coping the URL to edge it downloads the form.- AndreaMXTCopper Contributor
I am trying to bump this thread. What is the easiest way for getting the excel file from a specific Microsoft Forms in an automated way?
Are the Org_Settings.Forms the only permissions that are required for reading the responses to a specific form?
- ssahalCopper ContributorLeandro_Abade could you please highlight on how did you obtain the token? Did you have to register in app in Azure portal or by some other way. I am implementing something similar and stuck with the first pre-requisite of obtaining token.
- Leandro_AbadeCopper Contributor
Hi ssahal ! You just need access the URL “https://forms.office.com/Pages/DesignPage.aspx”, then in same section you calls "https://forms.office.com/formapi/DownloadExcelFile.ashx?formid=" + [put your form ID Here] + "&timezoneOffset=180&minResponseId=1&maxResponseId=1000"
The token is automatically generated in the site section.
So, unfortunately, you need to use a browser to use the API. I tried everything to get files using code and nothing worked.
If you find out how to generate the token by code, please share it with us! Tks
- RobElliottSilver Contributor
ravipokala an API has been promised for Forms for a long time but has never arrived. The solution is therefore to use a flow in Power Automate to save each form response to a list in SharePoint. You can either see the last week's responses there or you could have another flow with a recurrence schedule trigger set to 1 week. That would get the items created in the list in the last week and do whatever you want to do with it.
Rob
Los Gallardos
Microsoft Power Automate Community Super User- MartinDreyerCopper Contributor
RobElliott , a fairly good workaround. Unfortunately, Flows are prone to failing now and then and if you miss a response I guess there's no way to re-fetch it in retrospect?
- RobElliottSilver Contributor
MartinDreyer the response will still be in the spreadsheet behind the form, so you've still got the response even if the flow fails for some reason. Over the last 3 years I've found Power Automate to be very reliable and a flows will very rarely fail once you have properly tested it. Power Automate is far more reliable than Forms which, as this form shows, is far more prone to errors and things not working the way they should.
Rob
Los Gallardos
Microsoft Power Automate Community Super User