DEV Community

Discussion on: How I Automated The Google Form Filling For My College Attendance Using Python

Collapse
 
jsimonrichard profile image
Simon Richard • Edited

Great post, James! I needed to do a similar task with google forms, and this was very helpful. However, it didn't work right off the bat for me. After using requests.post(url, data=d), I got the status code 401 (which stands for an Unauthorized Error).

Side Note: no error is actually thrown. To view the status code, print the object or use r.status_code.

However, I was able to find a solution, and for the sake of the other readers I wanted to share it here. Here's what I did:

First, I filled out the form using chrome. After submitting the form, I used the developer tools in Chrome (on the landing page of the form) to look at the headers. That can be found on the Network tab after selecting the formResponse entry. For anyone following along, there should be a list of several header categories including General, Response Headers, Request Headers, and Form Data.

Another Side Note: all of the field names with the data you entered in the Google Form can be found under Form Data; I found that using this was a lot easier than hunting through html tag attributes.

Anyways, Request Headers is the one you want. Now, I'm not sure if you need every single field under the Request Headers, but I decided to use all of them. To do this, I just copied the plain text under Request Headers and assigned it to a variable like this:

header_text = '''
field_one: value
field_two: value
etc.
'''
Enter fullscreen mode Exit fullscreen mode

Then, I parsed the string using this function:

def parse_headers(text):
    # Init dictionary
    headers = {}

    # Loop through lines
    for line in text.split("\n"):
        # Split field name and value up so they can be assigned
        header = line.split(": ")

        if len(header) < 2:
            continue # no ": ", so it's probably just an empty line
        elif len(header) > 2:
            print("Help!") # This shouldn't happen
        else:
            headers[header[0]] = header[1]

    return headers
Enter fullscreen mode Exit fullscreen mode

This returns a dictionary with all of the fields and values under Request Headers. Now, let's parse the header and make the request (to submit the Google Form).

headers = parse_headers(header_text)
r = requests.post(url, data=d, headers=headers)
Enter fullscreen mode Exit fullscreen mode

I hope this helps!

Collapse
 
jamesshah profile image
James Shah • Edited

Hey Simon, Thank you for your feedback. I'm glad, it helped you. About Error 401, I didn't ran into this, and I think it's because of the url. Have you changed your google form url as mentioned in the post? You've to change the /viewform to /formResponse at the end of the url. I'm not sure if this will prevent the 401 error but I guess, this should work.

And about Form Data, thank you so much about that. Finding attributes from the developer tools is the most tiresome work in this script.

Again, thanks for your comment! And it feels good when someone uses your script and tells you how can you improve it!😄

Collapse
 
jsimonrichard profile image
Simon Richard

I did use /formResponse. However, I didn't make the form, so there might be some setting differences... I'm not sure. Thanks again, though.

Collapse
 
rizdaagisa profile image
rizdaagisa

how i can get the header text please?

Collapse
 
jsimonrichard profile image
Simon Richard

Here's a screenshot:
screenshot
It's under Network, click the fromResponse request, then make sure you're on the header tab and look for "Response Headers."

Thread Thread
 
rizdaagisa profile image
rizdaagisa

Thanks a lot

Collapse
 
skelliam profile image
William Skellenger • Edited

I found this helpful, Simon. I did some experiments to figure out what parts of the headers were needed and I was able to get it down to only one: The cookie: line.

My concern is that this cookie will eventually expire, so hardcoding it may not be a long term solution.