Forum Discussion

ravipokala's avatar
ravipokala
Copper Contributor
Jun 15, 2020

API to access MS forms

Looking for an API to access MS forms and pull the survey results on a weekly basis.

  • jmistry307's avatar
    jmistry307
    Copper Contributor

    BinaryBotany mpheasant 

    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!

  • MockmyThe1st's avatar
    MockmyThe1st
    Copper Contributor
    Anyone 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
    • NunoN370's avatar
      NunoN370
      Copper 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?

    • devgl's avatar
      devgl
      Copper Contributor
      Hi 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.
      • MockmyThe1st's avatar
        MockmyThe1st
        Copper 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.

    • RobElliott's avatar
      RobElliott
      Silver 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)

      • AndreaMXT's avatar
        AndreaMXT
        Copper 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?


    • ssahal's avatar
      ssahal
      Copper Contributor
      Leandro_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.
  • RobElliott's avatar
    RobElliott
    Silver 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

    • MartinDreyer's avatar
      MartinDreyer
      Copper 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?

      • RobElliott's avatar
        RobElliott
        Silver 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

Resources