Datastore / Pubsub emulators break when using default credentials
See original GitHub issueFor clarity / brevity (edit by @dhermes). When using just the default credentials, the oauth2client refresh tries to generate an access token and append one to a datastore / pubsub request. However, this is not needed and can cause breakage.
There is a workaround in both datastore and pubsub. We should determine if this should be “fixed” with docs or with custom credential usage in the Connection constructors.
Original report by @txomon : #1837 2nd issue
I think it is about mistakenly patching httplib for local testing.
Test script
from gcloud import datastore
client = datastore.Client()
entity = datastore.Entity(client.key('Test1'))
client.put(entity)
Setting up environment
This one needs:
- Application default credentials set up
Showcase
I think this manifests with a Connection refused error:
(ve) javier@theoden:~/projects/spinoffs/gae$ python test-script.py
Traceback (most recent call last):
File "test-script.py", line 5, in <module>
client.put(entity)
File "/home/javier/projects/spinoffs/gae/ve/lib/python3.4/site-packages/gcloud/datastore/client.py", line 319, in put
self.put_multi(entities=[entity])
File "/home/javier/projects/spinoffs/gae/ve/lib/python3.4/site-packages/gcloud/datastore/client.py", line 345, in put_multi
current.commit()
File "/home/javier/projects/spinoffs/gae/ve/lib/python3.4/site-packages/gcloud/datastore/batch.py", line 260, in commit
self._commit()
File "/home/javier/projects/spinoffs/gae/ve/lib/python3.4/site-packages/gcloud/datastore/batch.py", line 243, in _commit
self.project, self._commit_request, self._id)
File "/home/javier/projects/spinoffs/gae/ve/lib/python3.4/site-packages/gcloud/datastore/connection.py", line 331, in commit
_datastore_pb2.CommitResponse)
File "/home/javier/projects/spinoffs/gae/ve/lib/python3.4/site-packages/gcloud/datastore/connection.py", line 121, in _rpc
data=request_pb.SerializeToString())
File "/home/javier/projects/spinoffs/gae/ve/lib/python3.4/site-packages/gcloud/datastore/connection.py", line 93, in _request
method='POST', headers=headers, body=data)
File "/home/javier/projects/spinoffs/gae/ve/lib/python3.4/site-packages/oauth2client/client.py", line 631, in new_request
redirections, connection_type)
File "/home/javier/projects/spinoffs/gae/ve/lib/python3.4/site-packages/httplib2/__init__.py", line 1314, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/home/javier/projects/spinoffs/gae/ve/lib/python3.4/site-packages/httplib2/__init__.py", line 1064, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/home/javier/projects/spinoffs/gae/ve/lib/python3.4/site-packages/httplib2/__init__.py", line 987, in _conn_request
conn.connect()
File "/usr/lib/python3.4/http/client.py", line 871, in connect
self.timeout, self.source_address)
File "/usr/lib/python3.4/socket.py", line 516, in create_connection
raise err
File "/usr/lib/python3.4/socket.py", line 507, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
This is caused because it tries to make a auth token refresh against the emulator. There are no relevant logs for the emulator.
Workaround
I thought I had found a workaround but I cannot reproduce the workaround, therefore not a workaround.
The problem is the default credential, so it can be fixed by (ordered by personal preference):
Create a configuration with no default credentials, for the same project, or;Remove the default credentials~gcloud beta auth application-default revoke
Hopefully you know how to patch the library not to collide!
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (3 by maintainers)

Top Related StackOverflow Question
Yes. I was assuming @txomon wanted that release for a specific release series vs. newer one and that’s the highest for 0.12.x. 🙂
Gotcha. Thanks.