Title: Create client from service account stored in string · Issue #8 · googleapis/python-cloud-core · GitHub
Open Graph Title: Create client from service account stored in string · Issue #8 · googleapis/python-cloud-core
X Title: Create client from service account stored in string · Issue #8 · googleapis/python-cloud-core
Description: Hi, This was raised in python-storage: googleapis/python-storage#93 python-cloud-core library has support for from_service_account_json, which accepts a file path to a service account. Feature request is to add support to accept a json s...
Open Graph Description: Hi, This was raised in python-storage: googleapis/python-storage#93 python-cloud-core library has support for from_service_account_json, which accepts a file path to a service account. Feature requ...
X Description: Hi, This was raised in python-storage: googleapis/python-storage#93 python-cloud-core library has support for from_service_account_json, which accepts a file path to a service account. Feature requ...
Opengraph URL: https://github.com/googleapis/python-cloud-core/issues/8
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Create client from service account stored in string","articleBody":"Hi,\r\n\r\nThis was raised in python-storage: https://github.com/googleapis/python-storage/issues/93\r\n\r\npython-cloud-core library has support for [from_service_account_json](https://github.com/googleapis/python-cloud-core/blob/master/google/cloud/client.py#L50-L84), which accepts a file path to a service account. \r\n\r\nFeature request is to add support to accept a json string of the service account. User stores the service account file contents in an environment variable instead of in a file.\r\n\r\n**Open question**: I'm not sure if this is a path that should be supported so I'm leaving that as an open question in this issue. If this feature is not supported then the workaround is to construct a credentials object and explicitly set a project id:\r\n\r\n```python\r\n# Storage example but is a general issue AFAIU\r\n\r\nimport os\r\nimport json\r\n\r\nfrom google.cloud import storage\r\nfrom google.oauth2 import service_account\r\n\r\ncredentials_json_string = \"{...service account....}\"\r\n\r\ncredentials_json = json.loads(credentials_json_string)\r\ncredentials = service_account.Credentials.from_service_account_info(credentials_json)\r\n\r\n# Credentials has project_id already so project should be optional but it's not.\r\nclient = storage.Client(project=credentials['project_id'], credentials=credentials)\r\n\r\nprint(list(client.list_buckets()))\r\n```\r\n\r\n## Proposal:\r\n\r\n```python\r\n @classmethod\r\n def from_service_account_json_string(cls, json_credentials, *args, **kwargs):\r\n \"\"\"Factory to create credentials with provided json_credentials.\r\n :type json_credentials: str\r\n :param json_credentials: The contents of a private key file. This string must contain\r\n a JSON object with a private key and\r\n other credentials information (downloaded\r\n from the Google APIs console).\r\n :type args: tuple\r\n :param args: Remaining positional arguments to pass to constructor.\r\n :param kwargs: Remaining keyword arguments to pass to constructor.\r\n :rtype: :class:`_ClientFactoryMixin`\r\n :returns: The client created with the provided JSON credentials.\r\n :raises TypeError: if there is a conflict with the kwargs\r\n and the credentials created by the factory.\r\n \"\"\"\r\n if \"credentials\" in kwargs:\r\n raise TypeError(\"credentials must not be in keyword arguments\")\r\n credentials_info = json.load(json_credentials)\r\n credentials = service_account.Credentials.from_service_account_info(\r\n credentials_info\r\n )\r\n if cls._SET_PROJECT:\r\n if \"project\" not in kwargs:\r\n kwargs[\"project\"] = credentials_info.get(\"project_id\")\r\n\r\n kwargs[\"credentials\"] = credentials\r\n return cls(*args, **kwargs)\r\n```","author":{"url":"https://github.com/frankyn","@type":"Person","name":"frankyn"},"datePublished":"2020-04-17T23:39:35.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/8/python-cloud-core/issues/8"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:cc89a309-6d4e-13bf-b7ad-6ff345a8ca38 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B2EC:1960:158FD3B:1F0BAC8:6A4F772F |
| html-safe-nonce | 5a8cf6e6ed55dc19b0ffa10a94cea78205bceb37844afb22cba18c0fe9c87af5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMkVDOjE5NjA6MTU4RkQzQjoxRjBCQUM4OjZBNEY3NzJGIiwidmlzaXRvcl9pZCI6IjQ1MzgxNjQwMjI3MTM3NDMxNTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d04558a1e02f12ffc54d6c0c61a1f8a845305556213db7b764975a5bdd434ce5 |
| hovercard-subject-tag | issue:602292681 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/python-cloud-core/8/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b420e08874128f553db801006b1df94d54901008361e394f365aa173e7fbe37/googleapis/python-cloud-core/issues/8 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b420e08874128f553db801006b1df94d54901008361e394f365aa173e7fbe37/googleapis/python-cloud-core/issues/8 |
| og:image:alt | Hi, This was raised in python-storage: googleapis/python-storage#93 python-cloud-core library has support for from_service_account_json, which accepts a file path to a service account. Feature requ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | frankyn |
| hostname | github.com |
| expected-hostname | github.com |
| None | b92d11c0aa4a77d54ef4af1078b6a15fb5a70a215b30c4ecf28889d5a8e656d9 |
| turbo-cache-control | no-preview |
| go-import | github.com/googleapis/python-cloud-core git https://github.com/googleapis/python-cloud-core.git |
| octolytics-dimension-user_id | 16785467 |
| octolytics-dimension-user_login | googleapis |
| octolytics-dimension-repository_id | 226992462 |
| octolytics-dimension-repository_nwo | googleapis/python-cloud-core |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 226992462 |
| octolytics-dimension-repository_network_root_nwo | googleapis/python-cloud-core |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 4b249b445842943ed31549e027f57a8ade9881ed |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width