I attempted to follow the setup instructions and ran into an odd error. The instructions I was following are here: https://googlecloudplatform.github.io/gcloud-python/stable/gcloud-auth.html
Here is what I attempted:
virtualenv ~/venvsource ~/venv/bin/activatepip install gcloudgcloud auth loginpython>>> from gcloud import pubsub
>>> pubsub.Client()
Here's the error I received when I ran pubsub.Client()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../venv/local/lib/python2.7/site-packages/gcloud/client.py", line 184, in __init__
_ClientProjectMixin.__init__(self, project=project)
File ".../venv/local/lib/python2.7/site-packages/gcloud/client.py", line 143, in __init__
raise EnvironmentError('Project was not passed and could not be '
EnvironmentError: Project was not passed and could not be determined from the environment.
I do appear to have valid credentials. gcloud.credentials.get_credentials() happily provides me with a perfectly good GoogleCredentials object which works fine with httplib2 if I invoke it directly.
Thanks for the report! In addition to credentials, we need to know what project to use: a given set of credentials can have access to multiple projects. On GCE / GAE, we can determine the project from the host setup. On your own machine, you need to set an environment variable. E.g.:
$ export GCLOUD_PROJECT=flaky-piecrust-227
Or you can pass the project to the Client constructor:
>>> from gcloud import pubsub
>>> client = pubsub.Client('flaky-piecrust-227')
I think the "usage doc" sections should all link to a shared page explaining this requirement (similar to the Authentication page). @dhermes, @jgeewax WDYT?
Yea, we should probably clarify that... gcloud auth login does handle the auth, but you then need to do $ gcloud config set project <your project ID>, right?
So I ran into this when I started and I was wondering why the project ID isn't picked up from the credentials file?
{
"type": "service_account",
"project_id": "<PROJECT_ID>", <----
"private_key_id": "",
"private_key": "",
"client_email": "",
"client_id": "",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": ""
}
We recommend that you use separate credentials for different projects, so I would think it would be ok to pull it from there with the option to override it later if need be.
you may want to use two separate accounts to simultaneously access data from different projects
https://googlecloudplatform.github.io/gcloud-python/stable/gcloud-auth.html#explicit-credentials
Good call @daspecster, it's never occurred to us.
@dhermes, Is this something that should happen when oauth2client pulls in the other properties from the JSON?
Not really. oauth2client is only concerned with auth.
Most helpful comment
Thanks for the report! In addition to credentials, we need to know what project to use: a given set of credentials can have access to multiple projects. On GCE / GAE, we can determine the project from the host setup. On your own machine, you need to set an environment variable. E.g.:
Or you can pass the project to the
Clientconstructor:I think the "usage doc" sections should all link to a shared page explaining this requirement (similar to the Authentication page). @dhermes, @jgeewax WDYT?