Skip to content
Snippets Groups Projects
Commit c2626e39 authored by Hague Matthew UXAC009's avatar Hague Matthew UXAC009
Browse files

refactor(ms): streamline cookie handling

parent 11d7c34c
Branches
No related tags found
No related merge requests found
# Getting MS Forms Responses from Python # Getting MS Forms Responses from Python
To save you having to keep going to MS Forms to download the spreadsheet if you have a form you need to check often. To save you having to keep going to MS Forms to download the spreadsheet if you have a form you need to check often. You might have to point your browser at MS Forms first though, just to get the cookies.
## 1. Get Your Form ID ## 1. Get Your Form ID
...@@ -17,35 +17,14 @@ Let's call this `FORM_ID` from now on ...@@ -17,35 +17,14 @@ Let's call this `FORM_ID` from now on
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. 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`…). 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`…), but you don't need to worry about that.
To get the cookies you can use the code below. To get the cookies you can use the code below.
import browser_cookie3 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() 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): You can check the cookie jar contains the cookies above if you want, and raise an error if it does not.
raise Exception(
f"Missing forms cookies, please make sure you are logged in."
)
## 3. Getting the Responses ## 3. Getting the Responses
...@@ -55,13 +34,13 @@ We get the responses from a URL that includes the `FORM_ID`. ...@@ -55,13 +34,13 @@ We get the responses from a URL that includes the `FORM_ID`.
"https://forms.office.com/formapi/DownloadExcelFile.ashx?formid=" \ "https://forms.office.com/formapi/DownloadExcelFile.ashx?formid=" \
+ FORM_ID + 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 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. If the response is not successful, it's probably because you're not logged into MS Forms.
import requests import requests
resp = requests.get(RESPONSES_URL, cookies=cookies) resp = requests.get(RESPONSES_URL, cookies=cookie_jar)
if resp.status_code != 200: if resp.status_code != 200:
raise Exception("Getting responses failed!") raise Exception("Getting responses failed, try logging in to MS Forms again")
## 4. Using the Responses ## 4. Using the Responses
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment