Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using JSON string as PubSub credentials in Python #225

Closed
reselbob opened this issue Nov 29, 2017 · 4 comments
Closed

Using JSON string as PubSub credentials in Python #225

reselbob opened this issue Nov 29, 2017 · 4 comments
Assignees
Labels
🚨 This issue needs some love. triage me I really want to be triaged.

Comments

@reselbob
Copy link

reselbob commented Nov 29, 2017

Using Python, Is it possible to use just the JSON authentication credentials instead of a declaring the path to the file the contains the JSON, like so:

import os
from google.cloud import pubsub
json_string = os.environ['PUBSUB_JSON']
creds = json.loads(json_string)
client = pubsub.PublisherClient(credentials=creds)

I can do this technique in Node, but I am having trouble figuring out how to do the JSON only initialization in Python. When I use the service account technique like so:

import json
from google.oauth2 import service_account

json_string = os.environ['PUBSUB_JSON']
info = json.loads(json_string)
creds = service_account.Credentials.from_service_account_info(info)
client = pubsub.PublisherClient(credentials=creds)
topic_path = client.topic_path(os.getenv('GOOGLE_CLOUD_PROJECT'), 'cron_job_topic')
data = data.encode('utf-8')
future = client.publish(topic_path, data=data)

gives me an error:

google.auth.exceptions.RefreshError: ('invalid_scope: Empty or missing scope not allowed.', '{\n "error" : "invalid_scope",\n "error_description" : "Empty or missing scope not allowed."\n}')

The JSON in question works fine when declared using a file path.

Thanks in advance for any help!

@dhermes
Copy link
Contributor

dhermes commented Nov 29, 2017

The issue is that you haven't declared the scopes needed. For Pub / Sub you'll want to also provide:

scope = 'https://www.googleapis.com/auth/pubsub'
creds = service_account.Credentials.from_service_account_info(
    info, scopes=(scope,))

It's worth noting that if you set the GOOGLE_APPLICATION_CREDENTIALS environment variable to a file containing your JSON key, then you will automatically get scoped credentials via:

client = pubsub.PublisherClient()

(i.e. no arguments).

@dhermes
Copy link
Contributor

dhermes commented Nov 29, 2017

I am going to pre-emptively close this since it seems resolved.

@dhermes dhermes closed this as completed Nov 29, 2017
@reselbob
Copy link
Author

Yes, the scope declaration you provides is the fix I needed.

Please accept my most sincere thanks for sticking with me on this one.

One last thing, have you a URL that describes what scope is about?

@yoshi-automation yoshi-automation added 🚨 This issue needs some love. triage me I really want to be triaged. labels Apr 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚨 This issue needs some love. triage me I really want to be triaged.
Projects
None yet
Development

No branches or pull requests

3 participants