burnash / gspread

Google Sheets Python API
https://docs.gspread.org
MIT License
7.11k stars 948 forks source link

Error: 401 Unauthorized #247

Closed crluna closed 9 years ago

crluna commented 9 years ago

I am trying to just open a spreadsheet and read from it. Currently it seems that I can login and retrieve the spreadsheet successfully. However as soon as I try to read a sheet from it I get a 401 error. Here is the traceback:

HTTPError                                 Traceback (most recent call last)
<ipython-input-6-67a87af1a5a3> in <module>()
     13 # sheet_name = 'ACS221_General_Writing_Marksheet'
     14 sheet_name = 'ddf'
---> 15 wks = gs.open(sheet_name).sheet1
     16 #cell_list = wks.range('A1:E20')

C:\Python27\lib\site-packages\gspread\models.pyc in sheet1(self)
    159     def sheet1(self):
    160         """Shortcut property for getting the first worksheet."""
--> 161         return self.get_worksheet(0)
    162 
    163     @property

C:\Python27\lib\site-packages\gspread\models.pyc in get_worksheet(self, index)
    150         """
    151         if not self._sheet_list:
--> 152             self._fetch_sheets()
    153         try:
    154             return self._sheet_list[index]

C:\Python27\lib\site-packages\gspread\models.pyc in _fetch_sheets(self)
     66 
     67     def _fetch_sheets(self):
---> 68         feed = self.client.get_worksheets_feed(self)
     69         for elem in feed.findall(_ns('entry')):
     70             self._sheet_list.append(Worksheet(self, elem))

C:\Python27\lib\site-packages\gspread\client.pyc in get_worksheets_feed(self, spreadsheet, visibility, projection)
    235                             visibility=visibility, projection=projection)
    236 
--> 237         r = self.session.get(url)
    238         return ElementTree.fromstring(r.read())
    239 

C:\Python27\lib\site-packages\gspread\httpsession.pyc in get(self, url, **kwargs)
     77 
     78     def get(self, url, **kwargs):
---> 79         return self.request('GET', url, **kwargs)
     80 
     81     def delete(self, url, **kwargs):

C:\Python27\lib\site-packages\gspread\httpsession.pyc in request(self, method, url, data, headers)
     73 
     74         if response.status > 399:
---> 75             raise HTTPError("%s: %s" % (response.status, response.read()))
     76         return response
     77 

HTTPError: 401: <HTML>

``<HEAD>``
``<TITLE>Unauthorized</TITLE>``
``</HEAD>``
``<BODY BGCOLOR="#FFFFFF" TEXT="#000000">``
``<H1>Unauthorized</H1>``
``<H2>Error 401</H2>``
``</BODY>``
``</HTML>``

Any ideas how I can go about this?

Thanks.

burnash commented 9 years ago

Sorry for the late reply. Is the problem still persists?

If so, can you post your gspread's version and the method of authorization you're using?

duncstod commented 9 years ago

I get the same...thanks

ghost commented 9 years ago

I'm seeing this too. Here's my code snippet if it helps.

    json_key = json.load(open(config.get('FORM_RESPONSES', 'oauthJSON')))
    scope = ['http://spreadsheets.google.com/feeds']

    credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)

    gc = gspread.authorize(credentials)

    spreadsheetName = config.get('FORM_RESPONSES', 'spreadsheetName')

    wks = gc.open(spreadsheetName)
    worksheet = wks.get_worksheet(0)

    cell_list = worksheet.range('A1:A10')
    print(cell_list)
ghost commented 9 years ago

Figured out what the problem was. The scope url needs to have a secure http connection. So simple, yet so infuriating.