If you have logged into MS Forms recently, you'll have some cookies in your browser you can use to get in. You can use these cookies in a Python script using the `browser_cookie3` library. I'll use `browser_cookie3.firefox()` to get FireFox cookies. You might want `browser_cookie3.chrome()` or any of the other ones it supports.
The important cookies are `AADAuth.forms`, `OIDCAuth.forms`, and `FormsWebSessionId` in the domain `forms.office.com` (except `AADAuth.forms` which has domain `.forms.office.com`…).
To get the cookies you can use the code below.
import browser_cookie3
FORMS_DOMAINS = set([
"forms.office.com",
".forms.office.com", # oh microsoft
])
FORMS_COOKIES = set([
"AADAuth.forms",
"FormsWebSessionId",
"OIDCAuth.forms"
])
cookie_jar = browser_cookie3.firefox()
cookies = {
c.name : c.value
for c in cookie_jar
if c.name in FORMS_COOKIES and c.domain in FORMS_DOMAINS
}
And check if something went wrong:
if len(cookies) < len(FORMS_COOKIES):
raise Exception(
f"Missing forms cookies, please make sure you are logged in."
)
## 3. Getting the Responses
We get the responses from a URL that includes the `FORM_ID`.
To get the responses we'll use a GET with the requests library. The response (if successful -- status code 200) will contain an Excel spreadsheet in its content.
To use the responses directly, you might read them into a Pandas dataframe. I used `na_filter=False` and `dtype=str` to avoid Pandas trying to turn things into numbers. You may prefer other options.