如何将GCP服务帐户JSON字符串转换为DataForm凭据字符串?

发布于 2025-02-06 18:21:39 字数 2223 浏览 2 评论 0原文

我有一个GCP服务帐户JSON密钥,需要将其转换为数据形式凭据模板,如下所述。基本上,新线必须转换为\ n,并进行更多转换。会有一种简单的方法吗?

{
  "projectId": "prj-dfad-4curtool-d-c818",
  "credentials": "{
    "type": "service_account",
    "project_id": "prj-xxxx-xxx-x-xxx",
    "private_key_id": "35cf...27b",
    "private_key": "-----BEGIN PRIVATE KEY-----\nMIIE...ZNwub\n-----END PRIVATE KEY-----\n",
    "client_email": "[email protected]",
    "client_id": "102742287670708666429",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/[email protected]"
  }",
  "location": "US"
}

这样的数据形式凭据

{
  "projectId": "prj-dfad-4curtool-d-c818",
  "credentials": "{\r\n  \"type\": \"service_account\",\r\n  \"project_id\": \"prj-xxxx-xxx-x-xxx\",\r\n  \"private_key_id\": \"35cf...27b\",\r\n  \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIE...ZNwub\\n-----END PRIVATE KEY-----\\n\",\r\n  \"client_email\": \"[email protected]\",\r\n  \"client_id\": \"102742287670708666429\",\r\n  \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\r\n  \"token_uri\": \"https://oauth2.googleapis.com/token\",\r\n  \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\r\n  \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/[email protected]\"\r\n}\r\n\r\n",
  "location": "US"
}

I have a GCP Service Account JSON key, that needs to be converted to a Dataform Credentials Template, as mentioned below. Basically, newlines have to be converted to \n , and some more transformations. Would there be an easy way to do this?

{
  "projectId": "prj-dfad-4curtool-d-c818",
  "credentials": "{
    "type": "service_account",
    "project_id": "prj-xxxx-xxx-x-xxx",
    "private_key_id": "35cf...27b",
    "private_key": "-----BEGIN PRIVATE KEY-----\nMIIE...ZNwub\n-----END PRIVATE KEY-----\n",
    "client_email": "[email protected]",
    "client_id": "102742287670708666429",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/[email protected]"
  }",
  "location": "US"
}

to the Dataform Credentials like this

{
  "projectId": "prj-dfad-4curtool-d-c818",
  "credentials": "{\r\n  \"type\": \"service_account\",\r\n  \"project_id\": \"prj-xxxx-xxx-x-xxx\",\r\n  \"private_key_id\": \"35cf...27b\",\r\n  \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIE...ZNwub\\n-----END PRIVATE KEY-----\\n\",\r\n  \"client_email\": \"[email protected]\",\r\n  \"client_id\": \"102742287670708666429\",\r\n  \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\r\n  \"token_uri\": \"https://oauth2.googleapis.com/token\",\r\n  \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\r\n  \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/[email protected]\"\r\n}\r\n\r\n",
  "location": "US"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间☆小暴躁 2025-02-13 18:21:39

一种选择是使用 sed 替换控制chars(EG NEWLINE)具有逃脱的等效物(例如\ n)。

Note 因为您\ r(返回马车),我假设您使用的是Mac或Windows机器。因此,命令可能略有不同。我已经将电托返回(\ r)和newline(\ n)分开,以便在不使用电托回收的Linux上使用。<<<<<<<<< /p>

在Linux上:

CREDENTIALS=$(more /path/to/key.json | sed \
--null-data \
--expression='s|\r|\\r|g' \
--expression='s|\n|\\n|g' \
--expression='s|"|\\"|g')

echo "{\"credentials\":\"${CREDENTIALS}\"}"

One option is to use sed to replace control chars (e.g. newline) with escaped equivalents (e.g. \n).

NOTE Because you've \r (carriage returns), I assume you're using a Mac or Windows machine. So, the command may be slightly different. I've kept the carriage-return (\r) and newline (\n) separate so that this will work on Linux where carriage-returns aren't used.

On Linux:

CREDENTIALS=$(more /path/to/key.json | sed \
--null-data \
--expression='s|\r|\\r|g' \
--expression='s|\n|\\n|g' \
--expression='s|"|\\"|g')

echo "{\"credentials\":\"${CREDENTIALS}\"}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文