S
S
Soul12021-05-25 17:11:58
Python
Soul1, 2021-05-25 17:11:58

How to get authorization access for your desktop app in Google Sheets?

There is a desktop application, it accesses a Google spreadsheet via an access link. The link is restricted to view only by authorized users. It is assumed that the user of the application has already been added to the allowed users by his Google mail. But the problem is that when the application accesses Google, authorization is required.
I read that in general you need to register your application in Google so that they approve and issue access keys (or an access token). Is it so? Or is there an easier way to login?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
UberPool, 2021-05-26
@UberPool

You need to get an authorization token, Google usually gives an authorization file with the necessary data.
See more here

S
Soul1, 2021-05-26
@Soul1

I found this code, apparently not the first freshness. Made changes in the code:
1) Replaced urllib2 with urllib.request
2) Replaced
req = urllib2.Request(url, urllib.urlencode(params))
with
req = urllib.request.Request(url, email, password, service ) in the _get_auth_token function , source)
3) In the download function, replaced
req = urllib2.Request(url_format % (spreadsheet.key, format, gid), headers=headers)
with
req = urllib.request.Request(url_format % (spreadsheet.key, format, gid ), headers=headers)
Code

spoiler
#!/usr/bin/python

import openpyxl
import xlrd
import re, urllib, urllib.request


class Spreadsheet(object):
    def __init__(self, key):
        super(Spreadsheet, self).__init__()
        self.key = key

class Client(object):
    def __init__(self, email, password):
        super(Client, self).__init__()
        self.email = email
        self.password = password

    def _get_auth_token(self, email, password, source, service):
        url = "https://www.google.com/accounts/ClientLogin"
        params = {"Email": email,
                  "Passwd": password,
                  "service": service,
                  "accountType": "HOSTED_OR_GOOGLE",
                  "source": source}
        req = urllib.request.Request(url, email, password, service, source)
        return re.findall(r"Auth=(.*)", urllib.request.urlopen(req).read())[0]

    def get_auth_token(self):
        source = type(self).__name__
        return self._get_auth_token(self.email, self.password, source, service="wise")

    def download(self, spreadsheet, gid=0, format="xls"):
        url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=%s&gid=%i"
        headers = { "Authorization": "GoogleLogin auth=" + self.get_auth_token(), "GData-Version": "3.0" }
        req = urllib.request.Request(url_format % (spreadsheet.key, format, gid), headers=headers)
        return urllib.request.urlopen(req)

if __name__ == "__main__":
    email = "[email protected]" # (your email here)
    password = '.....'
    spreadsheet_id = "...." # (spreadsheet id here)
    # Create client and spreadsheet objects
    gs = Client(email, password)
    ss = Spreadsheet(spreadsheet_id) # Request a file-like object containing the spreadsheet's contents
    downloaded_spreadsheet = gs.download(ss)

    book = xlrd.open_workbook(file_contents=downloaded_spreadsheet.read(), formatting_info=True)
    file = open('path/to/file.xlsx', 'rb')
    wb = openpyxl.load_workbook(filename = file)

But at startup it gives the following error:
for key, value in headers.items():
AttributeError: 'str' object has no attribute 'items'
Tell me, maybe I wrote the replacement incorrectly or do I need to change / add something else?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question