Forums

"Access Denied" with Google Sheets Api v4

Hi, could you please help me to solve my problem?

When I try to access Google Spreadsheet as in this tutorial https://developers.google.com/sheets/api/quickstart/python I get "access denied" error referring to the fact that I am conneting a site out of the whitelist. However, is it really so? As I can see, the code satisfies the whitelist.

Can you give some more information for which line of code is getting an access denied error, and what the exact error message is?

Thank you for answering!! Well, now it is little better. Unexpectedly "access denied" dissapear. But it looks like a lottery: sometimes everything is ok, sometimes I get "Network is unreachable". Here is my code

def main():



    credentials = get_credentials()

    http = credentials.authorize(httplib2.Http())

    discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?' 'version=v4')
    service = discovery.build('sheets', 'v4', http=http,discoveryServiceUrl=discoveryUrl)

    spreadsheetId = 'Spreadsheet ID'
    rangeName = 'A24:E'
    result = service.spreadsheets().values().get(
        spreadsheetId=spreadsheetId, range=rangeName).execute()
    values = result.get('values', [])

if not values:
    print('No data found.')
else:

    for row in values:

        print('%s, %s' % (row[0], row[1]))

And this is the error:

Traceback (most recent call last)

  File homeInnoLinksBotmysiteflask_app.py, line 95, in module

    main()

  File homeInnoLinksBotmysiteflask_app.py, line 77, in main

    service = discovery.build('sheets', 'v4', http=http,discoveryServiceUrl=discoveryUrl)

  File homeInnoLinksBot.locallibpython3.5site-packagesoauth2client_helpers.py, line 133, in positional_wrapper

    return wrapped(args, kwargs)

  File homeInnoLinksBot.locallibpython3.5site-packagesgoogleapiclientdiscovery.py, line 228, in build

    requested_url, discovery_http, cache_discovery, cache)

  File homeInnoLinksBot.locallibpython3.5site-packagesgoogleapiclientdiscovery.py, line 275, in
 _retrieve_discovery_doc

    resp, content = http.request(actual_url)

  File homeInnoLinksBot.locallibpython3.5site-packagesoauth2clienttransport.py, line 175, in new_request

    redirections, connection_type)

  File homeInnoLinksBot.locallibpython3.5site-packagesoauth2clienttransport.py, line 282, in request

    connection_type=connection_type)

  File homeInnoLinksBot.locallibpython3.5site-packageshttplib2__init__.py, line 1322, in request

    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)

  File homeInnoLinksBot.locallibpython3.5site-packageshttplib2__init__.py, line 1072, in _request

    (response, content) = self._conn_request(conn, request_uri, method, body, headers)

  File homeInnoLinksBot.locallibpython3.5site-packageshttplib2__init__.py, line 995, in _conn_request

    conn.connect()

  File usrlibpython3.5httpclient.py, line 1252, in connect

    super().connect()

  File usrlibpython3.5httpclient.py, line 849, in connect

    (self.host,self.port), self.timeout, self.source_address)

  File usrlibpython3.5socket.py, line 711, in create_connection

    raise err

  File usrlibpython3.5socket.py, line 702, in create_connection

    sock.connect(sa)

OSError [Errno 101] Network is unreachable

[edited by admin: formatting]

Looking at this it seems like it might be a ipv4 vs ipv6 thing. Can you specify ipv4?

Really seems pretty similar. Could you please tell me how to specify it on pythonanywhere?

Another thing that we have been seeing for free users is that our proxy server was having some trouble (so some requests were timing out)- can you see if this is still a problem for you?

Otherwise you may need to figure out how to specify the IPv4 vs IPv6 configuration by reading the discovery.build documentation

w

Unfotunately, my problem wasn`t connected with proxy server trouble. I read discovery.build documentation, but I did not find there anything corresponding, except the fact, that by default discovery HTTP requests is be made through http = httplib2.Http(). This fact lead me to the post https://www.pythonanywhere.com/forums/topic/3485/ where support team advice to use request or urllib libraries. Unforunately, I can not understand how to change it in my particular case.

here's a quick thing to test- if you catch that error, and do a requests.get (or post or whatever it is) to the url, and see if that also times out? (then you can see if there is a difference between httplib vs requests)

The only case discovery uses httplib2 is that by default http = httplib2.Http(). I dont know the alternative in requests library, because dont understand what url they exactly use.

I`ve noticed that the error log has changed: the error came not while discovery service is building, but when when tries to perform actions with the spreadsheet:

Traceback (most recent call last):
    2017-07-11 08:15:58,543:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
    2017-07-11 08:15:58,543:     response = self.full_dispatch_request()
    2017-07-11 08:15:58,544:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    2017-07-11 08:15:58,544:     rv = self.handle_user_exception(e)
    2017-07-11 08:15:58,544:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
    2017-07-11 08:15:58,544:     reraise(exc_type, exc_value, tb)
    2017-07-11 08:15:58,544:   File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
    2017-07-11 08:15:58,544:     raise value
    2017-07-11 08:15:58,545:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    2017-07-11 08:15:58,545:     rv = self.dispatch_request()
    2017-07-11 08:15:58,545:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
    2017-07-11 08:15:58,545:     return self.view_functions[rule.endpoint](**req.view_args)
    2017-07-11 08:15:58,545:   File "/home/InnoLinksBot/mysite/flask_app.py", line 22, in update_data
    2017-07-11 08:15:58,545:     fill_database(app.config['DATABASE'])
    2017-07-11 08:15:58,545:   File "/home/InnoLinksBot/mysite/parser.py", line 81, in fill_database
    2017-07-11 08:15:58,546:     result = service.spreadsheets().values().get(spreadsheetId=spreadsheetId, range=rangeName).execute()
    2017-07-11 08:15:58,546:   File "/home/InnoLinksBot/.local/lib/python3.6/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    2017-07-11 08:15:58,546:     return wrapped(*args, **kwargs)
    2017-07-11 08:15:58,546:   File "/usr/local/lib/python3.6/dist-packages/googleapiclient/http.py", line 835, in execute
    2017-07-11 08:15:58,546:     method=str(self.method), body=self.body, headers=self.headers)
    2017-07-11 08:15:58,546:   File "/usr/local/lib/python3.6/dist-packages/googleapiclient/http.py", line 162, in _retry_request
    2017-07-11 08:15:58,546:     resp, content = http.request(uri, method, *args, **kwargs)
    2017-07-11 08:15:58,546:   File "/home/InnoLinksBot/.local/lib/python3.6/site-packages/oauth2client/transport.py", line 186, in new_request
    2017-07-11 08:15:58,546:     credentials._refresh(orig_request_method)
    2017-07-11 08:15:58,547:   File "/home/InnoLinksBot/.local/lib/python3.6/site-packages/oauth2client/client.py", line 761, in _refresh
    2017-07-11 08:15:58,547:     self._do_refresh_request(http)
    2017-07-11 08:15:58,547:   File "/home/InnoLinksBot/.local/lib/python3.6/site-packages/oauth2client/client.py", line 780, in _do_refresh_request
    2017-07-11 08:15:58,547:     body=body, headers=headers)
    2017-07-11 08:15:58,547:   File "/home/InnoLinksBot/.local/lib/python3.6/site-packages/oauth2client/transport.py", line 282, in request
    2017-07-11 08:15:58,547:     connection_type=connection_type)
    2017-07-11 08:15:58,547:   File "/usr/local/lib/python3.6/dist-packages/httplib2/__init__.py", line 1322, in request
    2017-07-11 08:15:58,547:     (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
    2017-07-11 08:15:58,548:   File "/usr/local/lib/python3.6/dist-packages/httplib2/__init__.py", line 1072, in _request
    2017-07-11 08:15:58,548:     (response, content) = self._conn_request(conn, request_uri, method, body, headers)
    2017-07-11 08:15:58,548:   File "/usr/local/lib/python3.6/dist-packages/httplib2/__init__.py", line 995, in _conn_request
    2017-07-11 08:15:58,548:     conn.connect()
    2017-07-11 08:15:58,548:   File "/usr/lib/python3.6/http/client.py", line 1392, in connect
    2017-07-11 08:15:58,548:     super().connect()
    2017-07-11 08:15:58,548:   File "/usr/lib/python3.6/http/client.py", line 936, in connect
    2017-07-11 08:15:58,548:     (self.host,self.port), self.timeout, self.source_address)
    2017-07-11 08:15:58,549:   File "/usr/lib/python3.6/socket.py", line 722, in create_connection
    2017-07-11 08:15:58,549:     raise err
    2017-07-11 08:15:58,549:   File "/usr/lib/python3.6/socket.py", line 713, in create_connection
    2017-07-11 08:15:58,549:     sock.connect(sa)
    2017-07-11 08:15:58,549: OSError: [Errno 101] Network is unreachable

Also when I am building discovery service with developerKey instead of authentication the probability of default decreases a bit. Is it ok?!

service = discovery.build('sheets', 'v4',discoveryServiceUrl=discoveryUrl, developerKey = 'key')

A "Network is unreachable" error suggests that the library you're using is not using the proxy settings. You may want to see if there's a newer version of the library that uses requests or respects the proxy settings some other way. If not, perhaps you could raise it as an issue with the library.

As for whether using the developer key is ok, I would suggest checking the documentation to see what developer keys are supposed to be used for.