It would be beneficial to be able to create a client instance using a service account file whose JSON contents have already been loaded in to memory. Currently from_service_account_json only supports loading a service account file from disk. This makes it difficult to create a client in scenarios where you are loading service account JSON contents from some external service and don't necessarily want to store the contents in a file where your application is running.
As a workaround, you can use a tempfile:
import tempfile
from google.cloud.<apilib> import Client
with tempfile.NamedTemporaryFile() as temp:
temp.write(json_service_account_bytes)
temp.flush()
client = Client.from_service_account_json(temp.name)
The file will be closed / deleted when the with block exits.
Yeah, a tempfile can be used (that's actually what I'm currently doing) but avoiding storing the key on disk is better. You should be able to pass the JSON string directly to the client constructor.
@tab1293 @tseaver you could use a code similar to:
credentials = google.oauth2.service_account.Credentials.from_service_account_info(keyfile_dict, scopes=scopes)
client = VideoIntelligenceServiceClient(credentials)
Real use case: https://github.com/apache/airflow/blob/master/airflow/contrib/hooks/gcp_api_base_hook.py
+1 to https://github.com/googleapis/google-cloud-python/issues/7291#issuecomment-471982254
I never really liked those factory functions. It's better to explicitly create a credentials object with the google-auth library.
Rather than adding a from_service_account_json classmethod factory to each of the 95 or so clients which currently have from_service_account_file factories, let's instead just document how to construct credentials from JSON.
are you kidding me that this is not supported?:D or is this just not updated as of yet?
@busunkim96 Is it done?
The PR was merged on December 14, and the details look up to date now with a working example. Please reopen if the update is insufficient. Thank you.
Most helpful comment
@tab1293 @tseaver you could use a code similar to:
Real use case: https://github.com/apache/airflow/blob/master/airflow/contrib/hooks/gcp_api_base_hook.py