From 9a636102f532860d713ee2761cbe3cb17f614fe3 Mon Sep 17 00:00:00 2001 From: Miguel Lozano Date: Thu, 5 Nov 2026 01:05:17 -0500 Subject: [PATCH 1/6] feat(deps): add google-auth for GA4 service account JWT authentication --- pyproject.toml | 1 + uv.lock | 193 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 5c92cb4..1093ef4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ dependencies = [ "pyyaml>=6.0", "requests>=2.31", "python-dotenv>=1.0", + "google-auth>=2.0", ] [project.urls] diff --git a/uv.lock b/uv.lock index 5a1f4aa..8a0f01e 100644 --- a/uv.lock +++ b/uv.lock @@ -105,6 +105,91 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/32/d1/a3652fe2c19134d0d15922177821d863cbd7e842d53a21bbe7e8e3a1b5ac/certifi-2026.10.23-py3-none-any.whl", hash = "sha256:e9b54de76e530bb6cf0ccd036787103acb58fd42ba930ea8bf948ddbaa2f61d1", size = 135082, upload-time = "2026-10-23T11:36:14.763Z" }, ] +[[package]] +name = "cffi" +version = "2.1.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "pycparser", marker = "implementation_name != 'PyPy'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/9e/ef/008a1939e372c06329a3fce4279c02f328488f3526744906eeec3da7ad5f/cffi-2.1.1.tar.gz", hash = "sha256:dd31f52ea1086513bb9df30f8fcee9b8918323ae067a3d5b78bc826a000712be", size = 530807, upload-time = "2026-08-03T21:21:18.939Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/10/69/43965eccfdead3b9220015fd1320e117be8c6ed01a62ffab76eeb752f5d5/cffi-2.1.1-cp312-cp312-macosx_10_15_x86_64.whl", hash = "sha256:c8c69575568085ba0b1b10c0249d779a214aea6f6522e949a0fc9fb0fcb449d0", size = 184821, upload-time = "2026-08-03T21:19:44.887Z" }, + { url = "https://files.pythonhosted.org/packages/54/7d/16e5a096677b5e313ca80cd5e5170efa3ea44624a82bb111925522da64b1/cffi-2.1.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:f81b3b8f3d4e343550fa4baa0e479bba9f2d29ce9c2e9b51d1ce1718d7442fcf", size = 184719, upload-time = "2026-08-03T21:19:46.129Z" }, + { url = "https://files.pythonhosted.org/packages/56/e6/8941622732edec876dd17d0453dce07317ae96db34f2ec1436c9d3785986/cffi-2.1.1-cp312-cp312-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:811bd1e21d32de12efca32393a0ab3f5133b54fce9bd44b8bd77ab07da14bf6a", size = 214799, upload-time = "2026-08-03T21:19:47.218Z" }, + { url = "https://files.pythonhosted.org/packages/44/de/f98430906df1545ffde0d543dd124a7a439bc2cd32b36b9c53f805df7333/cffi-2.1.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:68e62fe11f30d5ca8289242866f0a5291402d8529ca2178ab8afc5c9694ae890", size = 222389, upload-time = "2026-08-03T21:19:48.331Z" }, + { url = "https://files.pythonhosted.org/packages/6a/5b/717f1526b9957b34456313c31645c5b82b8fb5c3fe9e4752999be7128bfc/cffi-2.1.1-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:4a7c934f7360e8cd64fe9efadcbd10c7c6364f531e432b9a4bf5ccbc9e0e8b50", size = 210249, upload-time = "2026-08-03T21:19:49.543Z" }, + { url = "https://files.pythonhosted.org/packages/64/b3/f8aa4f3e34986c7e4ec45072d1b1b9dd295b6b18007b45518d79726dd725/cffi-2.1.1-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:3143d81e29e1e20a9ce10901ec369012947876596f75a222235965f2b7ae832e", size = 208775, upload-time = "2026-08-03T21:19:50.918Z" }, + { url = "https://files.pythonhosted.org/packages/b1/db/dceb9dd5b231e1da801793f8acc9f3c52a7e1afe40bb1aae37e02b0faad5/cffi-2.1.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:c1453022f490d2459a11819d83ad1d586e9ff65a12ac3e705ffebd46d3685dcf", size = 221822, upload-time = "2026-08-03T21:19:52.054Z" }, + { url = "https://files.pythonhosted.org/packages/a0/d2/6cd24ae3be000a634109c247d1475d62e5616d0dc78c82770942ec384248/cffi-2.1.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:208f941bb9d18e768138677f0a6d2ce01f590df56043dda1df1535ac57c88517", size = 225232, upload-time = "2026-08-03T21:19:53.109Z" }, + { url = "https://files.pythonhosted.org/packages/cb/52/3fa190537004dd7f0ab860a6dc7c0175b8667f68d1e618a46f5498d30250/cffi-2.1.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:210019b6c7cf07f081b4c54635c8cf744377001350e29cc0f81c4377b4797735", size = 223597, upload-time = "2026-08-03T21:19:54.515Z" }, + { url = "https://files.pythonhosted.org/packages/80/fb/0bb75b7039588c074b37ae99f40d9bfddf990ecb2fbc346ebccd2e56b9be/cffi-2.1.1-cp312-cp312-win32.whl", hash = "sha256:046bfc24911b37851ee1b51aab8bffe713d89c68c6a057b09484ce9fd5f69b4e", size = 175292, upload-time = "2026-08-03T21:19:55.566Z" }, + { url = "https://files.pythonhosted.org/packages/d9/79/615cc094e2fb508cade7de88d3b4f6c4ec2bab695c97bce9153dc65aadf5/cffi-2.1.1-cp312-cp312-win_amd64.whl", hash = "sha256:f53e442b08449d42821fa4a4fba000095af9f62742a500f978a9f557ec44339a", size = 185919, upload-time = "2026-08-03T21:19:56.89Z" }, + { url = "https://files.pythonhosted.org/packages/70/c6/d0ea84713fe46b243a436a18fcd47d639732747e21635c8a27191b06dc30/cffi-2.1.1-cp312-cp312-win_arm64.whl", hash = "sha256:7bde5e4cc5c10140859842b9d383af292b22639a4dffb725314baf45968cef80", size = 180093, upload-time = "2026-08-03T21:19:58.155Z" }, + { url = "https://files.pythonhosted.org/packages/9d/f4/035513d4117049066b4779dc3b7c0c0fdad175fa13731c9f4003f1cd1478/cffi-2.1.1-cp313-cp313-ios_13_0_arm64_iphoneos.whl", hash = "sha256:b5bdfd1c873d4e093aabc0ca84c4ca6dbc4f752afb5c86f146d9742580c9da2e", size = 194248, upload-time = "2026-08-03T21:19:59.399Z" }, + { url = "https://files.pythonhosted.org/packages/76/af/2aeb4dbb5fc41a04161ae9ff1518de7cec08e164f44a8ce6a4cf7fd2cd1d/cffi-2.1.1-cp313-cp313-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:31348097ff5bbe827ccc41795d4dd099d9f0625e7def00ee653c137a490c2a6c", size = 196908, upload-time = "2026-08-03T21:20:00.746Z" }, + { url = "https://files.pythonhosted.org/packages/a7/46/2e5fdde8555706dd98139a910ca11be02809f3f605ce956f655d0214e100/cffi-2.1.1-cp313-cp313-macosx_10_15_x86_64.whl", hash = "sha256:9d2055050ea716bd38b7f7f1579c275386646b4894c155a3e2f3cd62ed41b7c6", size = 184805, upload-time = "2026-08-03T21:20:02.02Z" }, + { url = "https://files.pythonhosted.org/packages/55/41/4c7042f317b9217502988f0873af87e16ad606dc20f84e546e3e6ce9764c/cffi-2.1.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:19ee6127ee34de7d83ce3d371ebc5ed91addbdcc39f9ab15ce4eb35a4e534971", size = 184764, upload-time = "2026-08-03T21:20:03.141Z" }, + { url = "https://files.pythonhosted.org/packages/43/1f/1c3d90d91811c8f86ced9ed637956c54bfe5b79ca98fe976d7f8c8979f6b/cffi-2.1.1-cp313-cp313-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl", hash = "sha256:6a8dddef476fab96d066d578fc88526767b836ab5ab21754e1d5bf3879c31c7c", size = 214722, upload-time = "2026-08-03T21:20:04.377Z" }, + { url = "https://files.pythonhosted.org/packages/37/6f/3b5ce4c3b2192d250f04908f2bfd91ef34552ec8f7716a5d4abdb8d67bb2/cffi-2.1.1-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:f16c709686a78c727bbbf059f92b0bf41c6fc60deec706d2dc19f529175a6125", size = 222369, upload-time = "2026-08-03T21:20:05.544Z" }, + { url = "https://files.pythonhosted.org/packages/02/10/4b3c75dde3d9663c9e02ba05c2668b954f671d4bbe346413ca8c696b295a/cffi-2.1.1-cp313-cp313-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:fcd22650c908d7b7da162bbfaab594a1227a15d1643a98c68b122ac642fa2264", size = 210175, upload-time = "2026-08-03T21:20:06.75Z" }, + { url = "https://files.pythonhosted.org/packages/df/62/14f74b9543e605d17701dc797b815958b8bb70b7624ce1b832ddad48ed6c/cffi-2.1.1-cp313-cp313-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:aa9511c62d14da7aacc9b4bf51f3f697a621e83b2d6919008243c3aad168eea3", size = 208670, upload-time = "2026-08-03T21:20:08.04Z" }, + { url = "https://files.pythonhosted.org/packages/95/95/86342356ff5953b3fb06f7ef7c5bee212d45e770abc7218d451b9148313c/cffi-2.1.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:a931079504ecc49efed7744c476a5c343a92fabf66dec2db95edb1b2fdc770e2", size = 221824, upload-time = "2026-08-03T21:20:09.274Z" }, + { url = "https://files.pythonhosted.org/packages/eb/ff/7b3429ff53aafe931ed8a5fc69f481bbef7ba6de87ddcbb63d08f483f613/cffi-2.1.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:a2d7755bef5a12ed488f4ef1f1b69ee9191d7396083b755a5d2295f6edb4768b", size = 225148, upload-time = "2026-08-03T21:20:10.7Z" }, + { url = "https://files.pythonhosted.org/packages/34/34/a95870b9221e09cf4f2ce3178b1a210abdfe63a1bd357da940418d7b8d15/cffi-2.1.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:e0bcb7e0f677f543555d2adff3bf19c05f66cdb4796e5ff602442ab2fe3c4ef7", size = 223564, upload-time = "2026-08-03T21:20:12.165Z" }, + { url = "https://files.pythonhosted.org/packages/70/ea/839b50531021a647fb5e929f72cf97bc1ff702b5472166164b5b6e76b851/cffi-2.1.1-cp313-cp313-win32.whl", hash = "sha256:334644fbac4eff73d985a17a91226df55d0f394160c4cfb880e084c8f7161cac", size = 175263, upload-time = "2026-08-03T21:20:13.559Z" }, + { url = "https://files.pythonhosted.org/packages/60/a6/8b149b2c3f2e11aaa1618ef64500b45f50f22c57a977a4dff1aff1f91042/cffi-2.1.1-cp313-cp313-win_amd64.whl", hash = "sha256:1aa5645c30469b09530c4ebca77ebf8f17618293c58f8549cb1a543a50236e7d", size = 185688, upload-time = "2026-08-03T21:20:14.69Z" }, + { url = "https://files.pythonhosted.org/packages/01/9a/11f687cb39d6a3504060d5242f04f48c735afb4d3d533958a20594890cb2/cffi-2.1.1-cp313-cp313-win_arm64.whl", hash = "sha256:63bbfd5ded17c4840ac07cd8f1c21ba9d9708141f840b324f422f41b207e3973", size = 180078, upload-time = "2026-08-03T21:20:15.917Z" }, + { url = "https://files.pythonhosted.org/packages/d3/7b/d6bbf82b8b96e7391438898c42f5bd96dd02030fd5b64937d248220003e2/cffi-2.1.1-cp314-cp314-ios_13_0_arm64_iphoneos.whl", hash = "sha256:7dbb61fe3a7699468030f71bbe5f8a0e326a151daa91beb11a6fc1f980c55e1c", size = 194064, upload-time = "2026-08-03T21:20:17.148Z" }, + { url = "https://files.pythonhosted.org/packages/94/e6/bcc91b283be94735e268487a054004f0aa19947b6348fa367db53230abc8/cffi-2.1.1-cp314-cp314-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:f24fb43132a4c6b4cb4eb029492919b2db645be6808d738f244fd146c03c32cb", size = 196720, upload-time = "2026-08-03T21:20:18.268Z" }, + { url = "https://files.pythonhosted.org/packages/d9/99/c4b0c17cacdc9c3b8f280026286a9826d6a208c0f047591a3c3ce99b91fd/cffi-2.1.1-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:d28630f5854ab07ab1fd4aba756de52326c82e6be15d414b12793f1975048b54", size = 184964, upload-time = "2026-08-03T21:20:19.708Z" }, + { url = "https://files.pythonhosted.org/packages/b3/a9/9db617d05d7367c1ad0ab00b3aa6e6f9281edd689b4ee9ea0e5a84e89c97/cffi-2.1.1-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:661c298b4821edebead0c91edd2b00374d67ad7c5a1f7a91d4442633b79d6a72", size = 184962, upload-time = "2026-08-03T21:20:20.833Z" }, + { url = "https://files.pythonhosted.org/packages/67/b8/b42132ca113dc567d37684437b46ca1dafc885902b02a110a02d5b511857/cffi-2.1.1-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:58acb8ab8e295e6c5ea12f888cbb13cf21511ef2a3303a23f4325c29d17fe5c1", size = 222328, upload-time = "2026-08-03T21:20:22.118Z" }, + { url = "https://files.pythonhosted.org/packages/80/10/c5c0cbf0a657aecf59ef511409734230bf556f05a0d6c9eed7aa5c0a0166/cffi-2.1.1-cp314-cp314-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:456a61fa52d579ebf9df2e9552ead5129855dbaff6c1e5a9b1bc408809bdc062", size = 209985, upload-time = "2026-08-03T21:20:23.401Z" }, + { url = "https://files.pythonhosted.org/packages/d5/6c/bfa0b87b03b9238148beca990292843c9396ba069b54496596594173de7b/cffi-2.1.1-cp314-cp314-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a4f00aa42f75d6e4595e8866e748cc1705adc0cddfeb2ca86d0d03993d63ba03", size = 208530, upload-time = "2026-08-03T21:20:24.628Z" }, + { url = "https://files.pythonhosted.org/packages/e9/02/4e7d553a7ac4b4238b38b3c1b80d486e9d4436f8d2acbf87a0997fe3f402/cffi-2.1.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:b0431303acaea1089ad4b3e9ce4e6518193def1118d4073ca848635ee4ea2e96", size = 221525, upload-time = "2026-08-03T21:20:25.758Z" }, + { url = "https://files.pythonhosted.org/packages/82/1d/a4aaf9babd75acb4d5f223bff71533bee748dd770a382619a798960ee9ba/cffi-2.1.1-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:64faea20f4e2613363a1a9b9c7dd73058f3ecd00133a511e72ad7c511658f527", size = 225053, upload-time = "2026-08-03T21:20:26.985Z" }, + { url = "https://files.pythonhosted.org/packages/81/10/5dc0e7bdd18e22107054288283380fc97a06ae3f1656a106908d666a3c88/cffi-2.1.1-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:5c58fe613dc5e5336357eff555824a314d8e43282600435c8d1cb6a7a2fedd13", size = 223213, upload-time = "2026-08-03T21:20:28.277Z" }, + { url = "https://files.pythonhosted.org/packages/0b/e9/d0061c364cde06ee43168a0d076ac1da512cbc380d44767b844ba34fe2b6/cffi-2.1.1-cp314-cp314-win32.whl", hash = "sha256:1a18a57b58cfb21fc28d72e876acf10eaed67a1ed96226f92af4df681d571c4c", size = 177682, upload-time = "2026-08-03T21:20:44.288Z" }, + { url = "https://files.pythonhosted.org/packages/a7/06/1c3e01e3ba14c39f6d10bfbac52753b7e22259e38088e5cfe1d704918690/cffi-2.1.1-cp314-cp314-win_amd64.whl", hash = "sha256:3222ba5d678f80a030e6afbcc33dc1ae5cb45facabb61cee2c7016b8432fde48", size = 187949, upload-time = "2026-08-03T21:20:45.623Z" }, + { url = "https://files.pythonhosted.org/packages/87/5b/da4e39efe18eeb89cf580ea9cfc66b6a7c3eadb808fc0cc1d3a295cb5a5d/cffi-2.1.1-cp314-cp314-win_arm64.whl", hash = "sha256:ab36d55f9ed2d067327667c2fea18dda018eb628dd6347aa01dda6cf1f5d3836", size = 182947, upload-time = "2026-08-03T21:20:46.955Z" }, + { url = "https://files.pythonhosted.org/packages/23/59/40338bf421c5accea1d45158170c87006ef1cd371b05c077e76476949728/cffi-2.1.1-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:7750c6449dff7864bb9bb27ddfb0267756189201a3afc911d82b3caacd70dfc3", size = 188504, upload-time = "2026-08-03T21:20:29.495Z" }, + { url = "https://files.pythonhosted.org/packages/7d/47/5ecf1023850036e674c77ec4de86182d309ae344e39e7cba984b7df5d647/cffi-2.1.1-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:0beceaabe56af686895136a2de78db54ecd8e4046b236b8fd6d6cb61389e9bf2", size = 188259, upload-time = "2026-08-03T21:20:31.291Z" }, + { url = "https://files.pythonhosted.org/packages/2a/9c/92934c3bea9f785b23eba304538c0b4d37a2a96d2431eb3a1bc87a11aa19/cffi-2.1.1-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:49cbc70e6542d4ccccb936558d1064a8012541e78f821f955cff24e357776c94", size = 223864, upload-time = "2026-08-03T21:20:32.571Z" }, + { url = "https://files.pythonhosted.org/packages/4d/45/ba4c93527bc38616a8bd36488acb69a2212d60486794f0c1f318949bbb76/cffi-2.1.1-cp314-cp314t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:e2d65b31f36619cda3999b78b2aa9632e76b78448e7a56fc4240824200e7c4fc", size = 211538, upload-time = "2026-08-03T21:20:33.808Z" }, + { url = "https://files.pythonhosted.org/packages/80/e9/b6ef565e452acb932fb0cb5443f44a78efbd1233e566f02b5a83855e9115/cffi-2.1.1-cp314-cp314t-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:28907ab9bfb6aa13184cfc17c6b8e1023c5ab6fd7076d8c20a35e59fe04f8f29", size = 210688, upload-time = "2026-08-03T21:20:34.974Z" }, + { url = "https://files.pythonhosted.org/packages/9a/95/eff5f0cee78d2eabc7eebffec40d3fc1876b5f3c95582e018bb4b99601f2/cffi-2.1.1-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:51b31d1c98274844cfd7838ce00bfc27c7423a4dc00fc0772fc3331c2cc90676", size = 223803, upload-time = "2026-08-03T21:20:36.564Z" }, + { url = "https://files.pythonhosted.org/packages/fa/01/579d39fb8bef00a335a23d83757b44feb24cd6345a2c451b64cb67b9c362/cffi-2.1.1-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:5e7cecbaadb83884793e05828cee59b210b24583b9c7425d0ba6a754fe22eb4e", size = 226763, upload-time = "2026-08-03T21:20:37.816Z" }, + { url = "https://files.pythonhosted.org/packages/8d/b0/0b44f47c60b01b57b6e2bbd92343f13a85a1d93bc46ccf6e47e244acd99c/cffi-2.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:25792eac27877609e7bb06d42ff88278a6624fff2ba9bbb523c09616b117e80f", size = 225688, upload-time = "2026-08-03T21:20:38.959Z" }, + { url = "https://files.pythonhosted.org/packages/eb/d2/3b7176cb570a1d3e27faf67b72f591af508036e0d8b2be2ef9af9e8c84bb/cffi-2.1.1-cp314-cp314t-win32.whl", hash = "sha256:8ef53b2de9bcb9197d31854256575d59dbac0cba72ac627bb291ef5eceb74be4", size = 182868, upload-time = "2026-08-03T21:20:40.388Z" }, + { url = "https://files.pythonhosted.org/packages/56/78/31f00c1bcd97c9bbf55f1bfdf5bc809a5de8887473e90bb9960dca825e80/cffi-2.1.1-cp314-cp314t-win_amd64.whl", hash = "sha256:616f097f2fe415bc92a247f02e11f634e1f9e9a83d327e3c915c15089c87869e", size = 194104, upload-time = "2026-08-03T21:20:41.725Z" }, + { url = "https://files.pythonhosted.org/packages/7b/1b/58496f2ed0a35de575250c02a43ab3cc2c04d494a88fed31c1cabc0fd176/cffi-2.1.1-cp314-cp314t-win_arm64.whl", hash = "sha256:ad2c86c495b899d862ea0f4b42891b8713a3bd45dd4105c7fd51c2a72f39f3a5", size = 186402, upload-time = "2026-08-03T21:20:43.042Z" }, + { url = "https://files.pythonhosted.org/packages/c1/8f/9ebe220eab48a093d1a5a5e339ab0dc7316eef3bb04d63c42f0251b61f50/cffi-2.1.1-cp315-cp315-ios_13_0_arm64_iphoneos.whl", hash = "sha256:dddad92b554513a31f272570678ba307fb9f618f05e3d4a5eacafff9eae03e1d", size = 194043, upload-time = "2026-08-03T21:20:48.179Z" }, + { url = "https://files.pythonhosted.org/packages/ff/69/844bad3ece306c4782c2ecb93597035b6690d48704b803914c199da1e8b3/cffi-2.1.1-cp315-cp315-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:da0e573f9f97159390c89d9f1a9e41908b66d408cc5b58d08cf3847d844c531b", size = 196737, upload-time = "2026-08-03T21:20:49.457Z" }, + { url = "https://files.pythonhosted.org/packages/1b/8a/af668013284634733f02d683458a0728739c7d6ddb5e14cb0c20832266fe/cffi-2.1.1-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:fb92203a88b3d3053034db775110081c49d28be6551923805e039924093761e4", size = 184933, upload-time = "2026-08-03T21:20:50.639Z" }, + { url = "https://files.pythonhosted.org/packages/0c/75/2f5207ff6d1a613133b23a5203cc0c2a628313b5eb3974d7956ae3c57950/cffi-2.1.1-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:2ae64be792b8966f2c69538199728b290e34726562896df1e5dc8ffd8d8188e8", size = 185002, upload-time = "2026-08-03T21:20:52.173Z" }, + { url = "https://files.pythonhosted.org/packages/e2/31/9e1313b0a6e30e91b3b3d3fff51ae99c857c07738e3afcce1f7334e1b7ab/cffi-2.1.1-cp315-cp315-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:507a24c282e0f42f8ed737cf048572cbf580468da5555764a8331735e9c736b6", size = 222271, upload-time = "2026-08-03T21:20:53.462Z" }, + { url = "https://files.pythonhosted.org/packages/50/e3/f6234a833e6e08c7007003074723c406559eecf9b48dfc97471e5a8eb7a0/cffi-2.1.1-cp315-cp315-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:246fa40ce8645a614ff682e0b70f37134e460eaf93a775e0cbe3cca585a67a80", size = 209919, upload-time = "2026-08-03T21:20:54.783Z" }, + { url = "https://files.pythonhosted.org/packages/0d/fc/5f74e293fced6edb51af3a46c4ccf6c23c9943774ecb375ddbd522c76add/cffi-2.1.1-cp315-cp315-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:471cee653ae88de62096552e6d24ccb4a5adb8c8c9f10b5054d0122c15bf2779", size = 208529, upload-time = "2026-08-03T21:20:56.066Z" }, + { url = "https://files.pythonhosted.org/packages/44/16/29e6d01b388bef055ecd6ca8244b3f4d336bd09e92d5d892187b9601084e/cffi-2.1.1-cp315-cp315-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:aeae0e330c9f6acd681f647d46cefd30c29f93e3392882e792e82080c9691399", size = 221630, upload-time = "2026-08-03T21:20:57.336Z" }, + { url = "https://files.pythonhosted.org/packages/a4/18/fa7f1f6857d5eb88a4ca99ffcbfb7c387a287ccc154c64a73e86314745d7/cffi-2.1.1-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:42a494cee34437f05546455144f2b5d9ac09b1face62bcfce597d2e521066688", size = 225134, upload-time = "2026-08-03T21:20:58.675Z" }, + { url = "https://files.pythonhosted.org/packages/e0/9f/e8e3dfa04a1b4c241f8c91faacad872b4d4efd051d49764ad4e2fd4b9fea/cffi-2.1.1-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:cc572dace3f60ef98d7b12ff411d20f5362feb31a0439eab0085bbfd349982d7", size = 223197, upload-time = "2026-08-03T21:20:59.968Z" }, + { url = "https://files.pythonhosted.org/packages/f8/7e/8debeb04f1ab9fe2a6963964cd6f1aaf7192627b83926586a6a4e089c9fa/cffi-2.1.1-cp315-cp315-win32.whl", hash = "sha256:4f42141fc14250de6dde5ee7ea4432be017252d91f19c5ad043c084cea629cac", size = 177683, upload-time = "2026-08-03T21:21:14.901Z" }, + { url = "https://files.pythonhosted.org/packages/e0/31/5158704cc474ab65c1647932e88be78dc0873f47130e253be38bcaf13d01/cffi-2.1.1-cp315-cp315-win_amd64.whl", hash = "sha256:e6e8cff14d6fb0be70a09c0bdc58096f501952d04624ebf867e0e56da2df8960", size = 187897, upload-time = "2026-08-03T21:21:16.108Z" }, + { url = "https://files.pythonhosted.org/packages/cc/4b/b3a2da8570c704ffc0f9762cdc3ec0f02c8573798e0b5cf7f11c82bbb70f/cffi-2.1.1-cp315-cp315-win_arm64.whl", hash = "sha256:27350daa11d4f10c540e6e89dada4c54feb7256ad03e9a4dc075ebad7ba360d1", size = 182935, upload-time = "2026-08-03T21:21:17.271Z" }, + { url = "https://files.pythonhosted.org/packages/d0/ef/5443574510a1207e6f6bc38ba6e1f1de36cb48fef07b2728bb896a21f430/cffi-2.1.1-cp315-cp315t-macosx_10_15_x86_64.whl", hash = "sha256:c26608d2222fb1e94487e4a387d85f13eb55d5ed725cb25a0c589ac4ee60e7bc", size = 188464, upload-time = "2026-08-03T21:21:01.163Z" }, + { url = "https://files.pythonhosted.org/packages/7e/ae/a56fa8c4686ad50e148fcbc8d3ae0d03915ff5c30d795058988c24118cef/cffi-2.1.1-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:4be96343e422f2dfcd12ab5c9f5aebe03f82f737c6bffeca6830b3875cb44aab", size = 188262, upload-time = "2026-08-03T21:21:02.382Z" }, + { url = "https://files.pythonhosted.org/packages/53/b2/6187f46f2912276a3ae284076109cc5c8680482f11f766ccf26db4a86427/cffi-2.1.1-cp315-cp315t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:937c0052c05a31ca1daf18de3158eed4dbfcb9cc107adbea227728d647be701e", size = 223779, upload-time = "2026-08-03T21:21:03.553Z" }, + { url = "https://files.pythonhosted.org/packages/8a/f6/c3ad28bd19f77047a03084424fbd4cbe997303267c14423737324be0385d/cffi-2.1.1-cp315-cp315t-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl", hash = "sha256:df423d40ee8654634421812bc3b196da3f9bd7d32929da813f8394c4348a5358", size = 211520, upload-time = "2026-08-03T21:21:04.863Z" }, + { url = "https://files.pythonhosted.org/packages/a0/cd/ccac9013a5bd9fd764de118674ab9c805b5ca10c19270d90ee273f8b2240/cffi-2.1.1-cp315-cp315t-manylinux2014_s390x.manylinux_2_17_s390x.whl", hash = "sha256:a730a083190634c65cca36ba5f489531576ebd79bcd5c8e172130f6453127231", size = 210673, upload-time = "2026-08-03T21:21:06.223Z" }, + { url = "https://files.pythonhosted.org/packages/52/86/2976131c639aead931c5bee5aba67e4b09fbeb8018b6f282f70803f923a7/cffi-2.1.1-cp315-cp315t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:363e05fa78e15116c3c32c210ee36884fd6b9afa6d440e47112c3bd511d64cb6", size = 223835, upload-time = "2026-08-03T21:21:07.539Z" }, + { url = "https://files.pythonhosted.org/packages/ac/0c/33a7aeab2f9c76918c52e084beb39c570db3588133412929e8ec06fab90b/cffi-2.1.1-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:770de9db11e84213beec501cfcaa013b019820ca881e03344dea5844f7876d94", size = 226705, upload-time = "2026-08-03T21:21:08.774Z" }, + { url = "https://files.pythonhosted.org/packages/e3/26/2cde30fdde421130bfc18f70395731a6e6b2053c6a1978a5258ff04e72fa/cffi-2.1.1-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:7da0c5eff80f0197f3b3d1232ec5a682a9325f4ae9016a78f5f5ca35f9ced1f5", size = 225539, upload-time = "2026-08-03T21:21:09.911Z" }, + { url = "https://files.pythonhosted.org/packages/6d/cd/a361394c94b2129d604bb846f624a8e88255a3ee33129c434a00d715e64f/cffi-2.1.1-cp315-cp315t-win32.whl", hash = "sha256:06c72bb76605a4b0cd0aad6930b69d4baf7dd5d806cfc409b824191099700e66", size = 182707, upload-time = "2026-08-03T21:21:11.226Z" }, + { url = "https://files.pythonhosted.org/packages/9b/b5/ba2b299993c26577d529b6ae29841f9e15b9fcf004d65f423f4fcf94ade9/cffi-2.1.1-cp315-cp315t-win_amd64.whl", hash = "sha256:d9c275eaacd24aa73f94ffd6de08fc3f932424d8b6c376f4bed7cde376fe7bc3", size = 193772, upload-time = "2026-08-03T21:21:12.39Z" }, + { url = "https://files.pythonhosted.org/packages/aa/29/35e016098c814cd93de9cd320c66b5bfba14dc6ecedd3cb518fa7c408c69/cffi-2.1.1-cp315-cp315t-win_arm64.whl", hash = "sha256:d18e5ac0f2f03f4f518d3e23db0f0cad7faa1da8620e9c09461d443bbf6e6692", size = 186360, upload-time = "2026-08-03T21:21:13.636Z" }, +] + [[package]] name = "charset-normalizer" version = "3.4.9" @@ -187,6 +272,69 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" }, ] +[[package]] +name = "cryptography" +version = "50.0.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "cffi", marker = "platform_python_implementation != 'PyPy'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/9d/af/182eb91b0df3fe75c4d9f26fe70684569566745f6ba7e5c9c73a862c5252/cryptography-50.0.2.tar.gz", hash = "sha256:7b46165bb56eb4704e2eaaf86f3c940d19154535d9b0ca7d6d590b04060e00d5", size = 880623, upload-time = "2026-09-30T15:30:04.884Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e5/56/d194340cc4a57535e82e1bee9e89667ac4b7c13b5d3f59686deae3094dd5/cryptography-50.0.2-cp311-abi3-macosx_11_0_arm64.whl", hash = "sha256:fa8f5efb344d6908a1ce62f4a24e2e5780f825d6f53f5f50ec5ffacac72936cb", size = 3914904, upload-time = "2026-09-30T14:43:44.339Z" }, + { url = "https://files.pythonhosted.org/packages/d9/69/c9bd862c3bf43d6399c433caf002df16e2dffd4be49bdf515cda38038711/cryptography-50.0.2-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:79def8d059362e7831389ed3be0ecdf58a89386e1271e35dd9f5af84e81bffd0", size = 4731146, upload-time = "2026-09-30T14:43:47.113Z" }, + { url = "https://files.pythonhosted.org/packages/21/69/64cef1f702bf6657e0cc186ed1a2891d50d29fb41586b254e1c07adea261/cryptography-50.0.2-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:630ebfea3bf689d075f82316324ff7433dc447fe6bc1bfc76524b74b4a9567d2", size = 4719841, upload-time = "2026-09-30T14:43:49.01Z" }, + { url = "https://files.pythonhosted.org/packages/38/6b/61a3f8d8c5e1e49a6cddccafc4015cc1c0021360ab0acb4080e7a423644a/cryptography-50.0.2-cp311-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:f9f6143a8c75945eb960d9eb98905a441394abfa24afaae239d514ffb2586480", size = 4738340, upload-time = "2026-09-30T14:43:50.932Z" }, + { url = "https://files.pythonhosted.org/packages/7b/2e/7212ca32fd43dc91f2f41db20160b268098874b4c9a0e7be94d6835f5b2e/cryptography-50.0.2-cp311-abi3-manylinux_2_28_ppc64le.whl", hash = "sha256:a582ab2ae1d34f67112cadc86702774c9ea4374df6bca6afe672817203c99134", size = 5367029, upload-time = "2026-09-30T14:43:52.911Z" }, + { url = "https://files.pythonhosted.org/packages/1a/f1/b474e930c4d910328780e3940da76f5aa5cbc48ce1fc14e44d239d9ea9db/cryptography-50.0.2-cp311-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:4061c0079120205fb760c58acab6443e217307dcf05e3702cf970e0689972856", size = 4753050, upload-time = "2026-09-30T14:43:55.272Z" }, + { url = "https://files.pythonhosted.org/packages/7c/52/9af10e80ac16b0fcc2123f9cbd5e7afbd0fd5075bb7a607c592258a39cda/cryptography-50.0.2-cp311-abi3-manylinux_2_31_armv7l.whl", hash = "sha256:ac9ed99d81760c62fe89d5f0815cdfa1ba9a35141cf30f1c2d044f04b4803d2e", size = 4376724, upload-time = "2026-09-30T14:43:57.24Z" }, + { url = "https://files.pythonhosted.org/packages/71/37/6202e488cc1eb625ea110c292c6bda92823176e023f427d8d5660ce8d632/cryptography-50.0.2-cp311-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:87e9ce85beb6b328ba370cc6e6aea483c92617b4c95b1d33a49297eb662bfb04", size = 4737859, upload-time = "2026-09-30T14:43:59.541Z" }, + { url = "https://files.pythonhosted.org/packages/8f/30/e86d7d518489b0ae2497091a35287abcb1a2ce4037837a34afbe9b1d6964/cryptography-50.0.2-cp311-abi3-manylinux_2_34_ppc64le.whl", hash = "sha256:f265528741e048bce55c3463ed721fb0aa45a5888d8add8cfeccb3035451bbdc", size = 5324103, upload-time = "2026-09-30T14:44:01.901Z" }, + { url = "https://files.pythonhosted.org/packages/d3/69/2c833a049475e0a3444e94c7d0aca0aa51d166374a449b09e92ac98138de/cryptography-50.0.2-cp311-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:9dab55f57c74c3cad24c323bacbbd04be4705ba6eb0d92e920b1fc4837ed5079", size = 4752576, upload-time = "2026-09-30T14:44:04.545Z" }, + { url = "https://files.pythonhosted.org/packages/6c/5d/906970b83bbfc1f5bbfb677a143c181f2801f23b6a7204a3b47c42c97e65/cryptography-50.0.2-cp311-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:25784ce8b9621c90c643efb9e1e2162ab3b0224cae446ad5e70e7fcb1ce18b51", size = 4870819, upload-time = "2026-09-30T14:44:06.884Z" }, + { url = "https://files.pythonhosted.org/packages/68/e3/f2298d3bb55e0c4a91841ec4d01b3f020ba8c5fbf15ccdcc6dcf03f97025/cryptography-50.0.2-cp311-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:85d0d9a31b9098e98534226d5686b47264b95e62ce459dc2e62fdfc809f9fe93", size = 5030152, upload-time = "2026-09-30T14:44:09.443Z" }, + { url = "https://files.pythonhosted.org/packages/9a/4f/adfc442765721292fff86d314ce385d3249d22db42295c0dd057727b60f3/cryptography-50.0.2-cp311-abi3-win_amd64.whl", hash = "sha256:7afa5a6602a9f29af1f3a2965f831bae7c9d5d597b7cbb716d41ab3b7d89879c", size = 3824692, upload-time = "2026-09-30T14:44:11.671Z" }, + { url = "https://files.pythonhosted.org/packages/ce/cb/52eb3770c0d0be2702a98c6e96065ddc0a2877cf0845aa9c23397c142cd4/cryptography-50.0.2-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:f785f6161f202ab04d8ca194158968798e480ca058943907972da5f12e2881e8", size = 3892731, upload-time = "2026-09-30T14:44:13.485Z" }, + { url = "https://files.pythonhosted.org/packages/19/8e/aa1fc533d4546b127b45de8aa024eb5933d23eff9debfe25931e56861095/cryptography-50.0.2-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:0ecbc5652bdb6fc9eaf89a7d196e20941adfe812f43bc4ca05d9150496821047", size = 4710431, upload-time = "2026-09-30T14:44:15.427Z" }, + { url = "https://files.pythonhosted.org/packages/6a/64/72bc3f75176e7e406b748a3e3830432b8c51297b38368713df04dc04898a/cryptography-50.0.2-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:ab50ee449bf968271e820086f10a33d101dd060370abc10bcd22279be2656539", size = 4694824, upload-time = "2026-09-30T14:44:17.69Z" }, + { url = "https://files.pythonhosted.org/packages/4e/c6/62c77550edfa5ca3f14bf44a1e6739b9fa09d6e998a11d97ed8213bccc98/cryptography-50.0.2-cp314-cp314t-manylinux_2_28_aarch64.whl", hash = "sha256:a9f7355e6fab51f6c369b86fb7571cffa05edee2c2121e0380a37fb9ac1cd5c1", size = 4716967, upload-time = "2026-09-30T14:44:19.661Z" }, + { url = "https://files.pythonhosted.org/packages/f4/37/cce70f150c432914460157a6ecc161752e053aa5ec0ef3b3f7dc6e31039a/cryptography-50.0.2-cp314-cp314t-manylinux_2_28_ppc64le.whl", hash = "sha256:94e5e9f108ee10471288214d3d233fbfbb492840a8457eb85178d643ddeb32c7", size = 5328676, upload-time = "2026-09-30T14:44:21.744Z" }, + { url = "https://files.pythonhosted.org/packages/aa/9a/6f2f0304d634ceafdeaf23e84537336664ac419b5d07611675c2ad3f6b7a/cryptography-50.0.2-cp314-cp314t-manylinux_2_28_x86_64.whl", hash = "sha256:241449bf940a5d27309bd317e6f9a2af6932113818bb2b8f5c59ddc7ef16da18", size = 4727698, upload-time = "2026-09-30T14:44:24.178Z" }, + { url = "https://files.pythonhosted.org/packages/1d/de/66bcf9244d118663b2e1aaded8990f4640e3d7b7411870a5765f252074d2/cryptography-50.0.2-cp314-cp314t-manylinux_2_31_armv7l.whl", hash = "sha256:d8947001be83df1394050758ce0e745dd74fb134eef0a4b5124208dfc3a68c37", size = 4354821, upload-time = "2026-09-30T14:44:26.263Z" }, + { url = "https://files.pythonhosted.org/packages/bd/e6/db28a28c7b6c676addce89136de3d8db49ea825a8c863472e36e42ead4ad/cryptography-50.0.2-cp314-cp314t-manylinux_2_34_aarch64.whl", hash = "sha256:4a20ce1e5cb4284a86692fdcba7cb8754185c6b2e5c56fcef3751cf451d3cdc2", size = 4716748, upload-time = "2026-09-30T14:44:28.447Z" }, + { url = "https://files.pythonhosted.org/packages/30/96/01546c7f69ea0e2ab790a2e4f0934a4052fb9b388147fbf83c2fd72f1e57/cryptography-50.0.2-cp314-cp314t-manylinux_2_34_ppc64le.whl", hash = "sha256:84f964e537f916e2cc85199e5a88742e964939b575ac8598b3f9d6cc416cdaf1", size = 5285085, upload-time = "2026-09-30T14:44:30.704Z" }, + { url = "https://files.pythonhosted.org/packages/6c/01/03263395f74d50b071e9e66daace3f8bef80493e5d410726f2ba8554736b/cryptography-50.0.2-cp314-cp314t-manylinux_2_34_x86_64.whl", hash = "sha256:828d49b0ff5a0e3975865571c5d91dbbdd0d38d8289b249a163e9425413a5e05", size = 4727268, upload-time = "2026-09-30T14:44:32.92Z" }, + { url = "https://files.pythonhosted.org/packages/eb/94/2bfe8f29ec0cc9c0d99359c4161adf32858e4934b72c6d100d2ac0bbe962/cryptography-50.0.2-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:deb9fde5c60e437ee4821bc9bc39ff31b42135c27e1dc61ef0a629389c1de62e", size = 4849503, upload-time = "2026-09-30T14:44:34.969Z" }, + { url = "https://files.pythonhosted.org/packages/54/44/e80651ecbf0e42b62e2bb5f5768916e07eea72e1297338956a61df361f88/cryptography-50.0.2-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:8c71ba2cd31fc93748c38e1b613200ff1c2665cbfd5341fe3a61cfde35a1430e", size = 5004057, upload-time = "2026-09-30T14:44:37.064Z" }, + { url = "https://files.pythonhosted.org/packages/f8/cc/1d33befb3cd7ea7e77d2d73f43f2066471da1b21f24a6156efcaabf6d2e8/cryptography-50.0.2-cp314-cp314t-win_amd64.whl", hash = "sha256:78198641e5be9521beea5aa782bb551a58068d10e6eb04c9c680c1b69f2e7d45", size = 3795868, upload-time = "2026-09-30T14:44:39.71Z" }, + { url = "https://files.pythonhosted.org/packages/2d/49/93f6a6e7a87c9aa68d44d3e1cdb5fe8f60c90d5d2f46acae9a56892816b8/cryptography-50.0.2-cp315-abi3.abi3t-macosx_11_0_arm64.whl", hash = "sha256:edc3342adf8f697fc5f59c887a304356f147b397809440ed64e2fa6af2f50f37", size = 4133708, upload-time = "2026-09-30T14:44:41.807Z" }, + { url = "https://files.pythonhosted.org/packages/8c/75/32ac2a56243d778805c16ca6a32b8f74fb757df7e28d7ecb560afafb59cf/cryptography-50.0.2-cp315-abi3.abi3t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:d370b8d1dfcdf7130178137f6fbee6140774a1acc6cacefc4b42643ec11d0a3a", size = 4956267, upload-time = "2026-09-30T14:44:43.693Z" }, + { url = "https://files.pythonhosted.org/packages/aa/a4/2c8d734e43d97f0842ee9f1b7b4bfb3d0cf5e19edebf43c2afe6675c2320/cryptography-50.0.2-cp315-abi3.abi3t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:f2f9bd7f90c64fe89253f0a2c05e3c4856072660429ce8831b4235bf29403a67", size = 4966465, upload-time = "2026-09-30T14:44:45.769Z" }, + { url = "https://files.pythonhosted.org/packages/c2/58/ee288c829a6f41f6235ae9dd33d82fd19b45442b65b4c8a3da36963d9f7a/cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_28_aarch64.whl", hash = "sha256:e275096ea1e60cc595cda2836fd4a6c725d1125108b868be17f53684d164e2cc", size = 4959356, upload-time = "2026-09-30T14:44:48.211Z" }, + { url = "https://files.pythonhosted.org/packages/92/20/9ded6d51ddd9897f6b6e81fb9ebea7951d7cc5d6c890b0ed8abf77a51a80/cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_28_ppc64le.whl", hash = "sha256:b13478603dcd0a2479ff8e87e2c19a7d525734686fe3c49542472293a204212d", size = 5548822, upload-time = "2026-09-30T14:44:50.86Z" }, + { url = "https://files.pythonhosted.org/packages/02/a8/8df951850d6b31d2a00218f19e2b3f999523437ed7a819df7fa427942fca/cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_28_x86_64.whl", hash = "sha256:58a0c478eeca76fe5e07993c5a0703def34a6dc6a0cda4f5564639b33112ffe7", size = 5001199, upload-time = "2026-09-30T14:44:53.379Z" }, + { url = "https://files.pythonhosted.org/packages/8b/f9/36b3022218ce75b7cdf068fb95f809f9bd0d820e4955ef43b90c255cc7ac/cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_31_armv7l.whl", hash = "sha256:d38cdff612d06fa6a32840d5e1b1f7a27cee4a349aa9085d94a67789d6bfd408", size = 4629333, upload-time = "2026-09-30T14:44:55.635Z" }, + { url = "https://files.pythonhosted.org/packages/8c/72/20f99a219f6af47cdd1cbd978c243b92d71496e168a746138af44ded4f29/cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_34_aarch64.whl", hash = "sha256:fdd28f912fccfec1846a94e2e1e8f9b0012f557f0c46fe4f3eb0d7a87afcf90b", size = 4958822, upload-time = "2026-09-30T14:44:59.639Z" }, + { url = "https://files.pythonhosted.org/packages/f2/20/196f112617fb08eb4d608a2a6c422373d46f9cc2857f38fc0667033c0899/cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_34_ppc64le.whl", hash = "sha256:cbc8738fd8526d80f35cb3a40d41f41a2e7030bb3b18b09a6778ef63d291c2fd", size = 5506351, upload-time = "2026-09-30T14:45:02.267Z" }, + { url = "https://files.pythonhosted.org/packages/24/95/83378121ef3eaaaf71d4b781577ff794acb39b9e1b87a3f156898c8497ed/cryptography-50.0.2-cp315-abi3.abi3t-manylinux_2_34_x86_64.whl", hash = "sha256:e105ab60406787da31fccc883fc0f733af1efd78f0136a4599692c4083a73d0c", size = 5000859, upload-time = "2026-09-30T14:45:05.009Z" }, + { url = "https://files.pythonhosted.org/packages/22/f7/70fd7ae4d1dbfa7ba29b02e1b9068771519a86027756510b700ce81086a8/cryptography-50.0.2-cp315-abi3.abi3t-musllinux_1_2_aarch64.whl", hash = "sha256:6f8700550aa1474a91e5dc07049c46f98b423b5b1ddd0483e0b51362eeeaf5be", size = 5092151, upload-time = "2026-09-30T15:29:15.932Z" }, + { url = "https://files.pythonhosted.org/packages/d4/be/688367b74de86984bd58d8efacfc7c9e68b89a6a22ced0fb4f38db50254a/cryptography-50.0.2-cp315-abi3.abi3t-musllinux_1_2_x86_64.whl", hash = "sha256:c71be1cbfa5cd9a41ee452acf1eccd82b2c05950358b106ec8ceb83411d1a020", size = 5286120, upload-time = "2026-09-30T15:29:18.309Z" }, + { url = "https://files.pythonhosted.org/packages/39/d1/55f8a3f2ef5d1529e16835ef10cf0fe3d559ce237b46dddc440c0bba3649/cryptography-50.0.2-cp315-abi3.abi3t-win_amd64.whl", hash = "sha256:c423ab384a46c4dff7217b2ea5ba2e11cffdeab6441acd04cf65a369caf0366c", size = 4111557, upload-time = "2026-09-30T15:29:20.155Z" }, + { url = "https://files.pythonhosted.org/packages/23/ad/ac987755d00e1e64273760228d2635ae38dae2be83e3c6e0d3289d91dec3/cryptography-50.0.2-cp39-abi3-macosx_11_0_arm64.whl", hash = "sha256:0ec5f09541743261e66e291b4a0cbf0fb2997aeaab6d9e9c740b9dba1b58d1c2", size = 3943588, upload-time = "2026-09-30T15:29:22.265Z" }, + { url = "https://files.pythonhosted.org/packages/d5/8d/6d585339bedf85d45044c85d8412dac53f2bb6f918e8b7777efba1787844/cryptography-50.0.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:c5e67125c7dca78d199ec4e116aa93dbb83494808ecbb8211a2cb09b1bf41dbd", size = 4756166, upload-time = "2026-09-30T15:29:24.58Z" }, + { url = "https://files.pythonhosted.org/packages/bf/f1/1c1f6874e8550cfddd4b688ceb38cefb6ed15ceed224d56f133f3d88c214/cryptography-50.0.2-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:ee247f5c245c9a2fe7c8e2214e295918838e44e00a45a6718451e4004219e767", size = 4749145, upload-time = "2026-09-30T15:29:26.807Z" }, + { url = "https://files.pythonhosted.org/packages/c1/63/61b15dc1a8de03fe0adbe3fd7608b3ad5c73bf50993bbcb1faaa930afe33/cryptography-50.0.2-cp39-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:dfe9763530994147d9af1def057a5b9658b00e8f8fe8743d144d1e0911c2e454", size = 4763638, upload-time = "2026-09-30T15:29:28.588Z" }, + { url = "https://files.pythonhosted.org/packages/fc/35/b345bdfa40c9126df1a9d33236aa98418367931b8725f84fc3ae2b98dc59/cryptography-50.0.2-cp39-abi3-manylinux_2_28_ppc64le.whl", hash = "sha256:58ddb5a8e3179d12f19e4ea34d2d32e9d63a4baa142c875c1eb59f41b7243acd", size = 5382217, upload-time = "2026-09-30T15:29:30.589Z" }, + { url = "https://files.pythonhosted.org/packages/4f/87/ef344a9e616871f2519c22d6afcda79ddd5d35e9592d95eb6e677608d055/cryptography-50.0.2-cp39-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:f21e8a22c8605750c7af886bab299a363721264061b4ac0a30efb73cfd58efc5", size = 4781387, upload-time = "2026-09-30T15:29:32.605Z" }, + { url = "https://files.pythonhosted.org/packages/90/5b/f2fdb13cd0b96f6f932c8627bb292a45f11c64d21620a8e120aee9a3b848/cryptography-50.0.2-cp39-abi3-manylinux_2_31_armv7l.whl", hash = "sha256:9c8402a82ea0dc4ceeab793db05f0fafa8ca139ca34fcde5df0f596103c74107", size = 4403790, upload-time = "2026-09-30T15:29:34.374Z" }, + { url = "https://files.pythonhosted.org/packages/bc/ce/7e4f662b1e3c393513569e402cfc85ac7da0bd3d5435e122a3140219eb2d/cryptography-50.0.2-cp39-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:0ddc924c04591c2811ca024d62ecad4f7f6f08af8939c211438f48a16bd23602", size = 4764319, upload-time = "2026-09-30T15:29:36.149Z" }, + { url = "https://files.pythonhosted.org/packages/3c/3f/86ff33ce34cc0de6847fb96e035a1a760d81652e38643f617c02ad32ef7a/cryptography-50.0.2-cp39-abi3-manylinux_2_34_ppc64le.whl", hash = "sha256:a6557e5f38e065ca9fbdaf7cfc7435ecb1d113aa81a022d1b51921ee7432e227", size = 5338560, upload-time = "2026-09-30T15:29:39.053Z" }, + { url = "https://files.pythonhosted.org/packages/40/cf/6b5c8e2fd9202d98988ab7cb5cc5c991704c4ad55f492ff408e4969f83f1/cryptography-50.0.2-cp39-abi3-manylinux_2_34_x86_64.whl", hash = "sha256:1981f1db4630889b9ef7803fadef12b056f428cb6b85c27ba57b774793b6093c", size = 4780973, upload-time = "2026-09-30T15:29:41.251Z" }, + { url = "https://files.pythonhosted.org/packages/10/bf/8d6ebc7dded797bd0f0160d52188021211f011a2b164ef0ae1dac4587465/cryptography-50.0.2-cp39-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:7a8701d6b584d76e909e3d305b7d126b41439876a5aaf76cddc67fc230eafa2e", size = 4897738, upload-time = "2026-09-30T15:29:43.106Z" }, + { url = "https://files.pythonhosted.org/packages/d4/aa/f3f6e0de7e6253b8baa8b2d8fb9d50924fa75cee3d4624bd4bc1208ee923/cryptography-50.0.2-cp39-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:ce47f66801c20ec6c6632453bb5960fe38939e9306970b48b3a5a26de7745d94", size = 5058280, upload-time = "2026-09-30T15:29:44.827Z" }, + { url = "https://files.pythonhosted.org/packages/f6/b6/a1faf3a27ae9405fb34b1713cc73b2d8a26b04d5c561578fa2e6ef3e5bb9/cryptography-50.0.2-cp39-abi3-win_amd64.whl", hash = "sha256:4e81d95e5bafc2d6e34e4bed780e53e4d5b9a2f928573428aa4d35fbec1eb0de", size = 3854095, upload-time = "2026-09-30T15:29:46.782Z" }, +] + [[package]] name = "daff" version = "1.4.2" @@ -427,6 +575,19 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/c7/96/147a2771ab655b9353781fb2f95c94eaf1d8576dccc991c1a61d0d355067/giturlparse-0.15.0-py2.py3-none-any.whl", hash = "sha256:76d2e6983b037356ab99b30683e533ac3db96409b68e2163a20fc3aff6446f10", size = 16683, upload-time = "2026-06-16T07:28:55.184Z" }, ] +[[package]] +name = "google-auth" +version = "2.62.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "cryptography" }, + { name = "pyasn1-modules" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/c4/38/d79ab09dcfc1e1638d887c8f65657619d3ebc454165c558ef79355971ce3/google_auth-2.62.1.tar.gz", hash = "sha256:3c8e1ce2553784ae17fd220e6f88df3281d86ab98019278f436d06dffab17a9e", size = 398519, upload-time = "2026-11-03T03:06:03.008Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/14/dd/2e11f0976ecb3ce48ef3f324837460e9ea50071fa4eb01f1e49df87d3cee/google_auth-2.62.1-py3-none-any.whl", hash = "sha256:015399a5abde39033c69404cc1c079e6d4c2de2993e164c9f4eac6e5376ad72b", size = 267975, upload-time = "2026-11-03T03:06:01.422Z" }, +] + [[package]] name = "humanize" version = "4.16.0" @@ -811,6 +972,7 @@ source = { editable = "." } dependencies = [ { name = "dbt-postgres" }, { name = "dlt", extra = ["postgres"] }, + { name = "google-auth" }, { name = "python-dotenv" }, { name = "pyyaml" }, { name = "requests" }, @@ -829,6 +991,7 @@ dev = [ requires-dist = [ { name = "dbt-postgres", specifier = ">=1.8" }, { name = "dlt", extras = ["postgres"], specifier = ">=1.0" }, + { name = "google-auth", specifier = ">=2.0" }, { name = "python-dotenv", specifier = ">=1.0" }, { name = "pyyaml", specifier = ">=6.0" }, { name = "requests", specifier = ">=2.31" }, @@ -1124,6 +1287,36 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/20/be/b732c8418ffa5bcfda002890f5dc4c869fc17db66ff11f53b17cfe44afc0/psycopg2_binary-2.9.12-cp314-cp314-win_amd64.whl", hash = "sha256:f12ae41fcafadb39b2785e64a40f9db05d6de2ac114077457e0e7c597f3af980", size = 2848762, upload-time = "2026-04-20T23:35:46.421Z" }, ] +[[package]] +name = "pyasn1" +version = "0.6.5" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/88/7e/3c0da41061a2021cac7cd524fd57092adbe5b515e8bf9005702e366802e2/pyasn1-0.6.5.tar.gz", hash = "sha256:6b11616a6bcf3a40e635f082bb232fa57b5e0dd7f517bd7ddd69ec3794508a47", size = 155152, upload-time = "2026-10-27T06:32:49.125Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/19/1f/3eefd5c84c35cae44549ce1c7f7e58f7945b34f5a6bce07a135438522073/pyasn1-0.6.5-py3-none-any.whl", hash = "sha256:4017b0fce125096b8573b39c989fdb1a4b19cf761e683398184550fea0e66587", size = 84807, upload-time = "2026-10-27T06:32:48.045Z" }, +] + +[[package]] +name = "pyasn1-modules" +version = "0.4.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "pyasn1" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/e9/e6/78ebbb10a8c8e4b61a59249394a4a594c1a7af95593dc933a349c8d00964/pyasn1_modules-0.4.2.tar.gz", hash = "sha256:677091de870a80aae844b1ca6134f54652fa2c8c5a52aa396440ac3106e941e6", size = 307892, upload-time = "2025-03-28T02:41:22.17Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/47/8d/d529b5d697919ba8c11ad626e835d4039be708a35b0d22de83a269a6682c/pyasn1_modules-0.4.2-py3-none-any.whl", hash = "sha256:29253a9207ce32b64c3ac6600edc75368f98473906e8fd1043bd6b5b1de2c14a", size = 181259, upload-time = "2025-03-28T02:41:19.028Z" }, +] + +[[package]] +name = "pycparser" +version = "3.12" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/cb/27/eeb9f307bf07060f3d9f9aa0fb8c15b01bdfded0a941e4b2719c7f94cacd/pycparser-3.12.tar.gz", hash = "sha256:f03387877eab82ce01bbdfd0ba7252ee55a90b1c708d5129e2ce915475cf1db6", size = 158785, upload-time = "2026-10-22T13:40:03.56Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/b6/ae/315bbeee126a769cbde43cf4908acb4b40408d8bf8bbf334dd7368d8dadf/pycparser-3.12-py3-none-any.whl", hash = "sha256:a076f3f489b39b43ed13563a1dafe42447445bec2e294154ec5c57fcdda4c85e", size = 62600, upload-time = "2026-10-22T13:40:02.021Z" }, +] + [[package]] name = "pydantic" version = "2.13.4" From 7deacac1b1745c6c1f61fa8deeb4f6642701e42a Mon Sep 17 00:00:00 2001 From: Miguel Lozano Date: Thu, 5 Nov 2026 01:05:27 -0500 Subject: [PATCH 2/6] feat(auth): implement unified GoogleTokenManager and OAuth renewal wizard --- src/agency_analytics/google_auth.py | 368 ++++++++++++++++++++++++++++ tests/test_google_auth.py | 305 +++++++++++++++++++++++ 2 files changed, 673 insertions(+) create mode 100644 src/agency_analytics/google_auth.py create mode 100644 tests/test_google_auth.py diff --git a/src/agency_analytics/google_auth.py b/src/agency_analytics/google_auth.py new file mode 100644 index 0000000..76c8c4b --- /dev/null +++ b/src/agency_analytics/google_auth.py @@ -0,0 +1,368 @@ +"""Unified Google OAuth 2.0 authentication and token management engine. + +Provides memory-cached token auto-refresh for Google services (YouTube, GTM, Google Ads), +hierarchical multi-tenant credential resolution, and an interactive OAuth consent wizard. +""" + +from __future__ import annotations + +import getpass +import os +import time +import urllib.parse +import webbrowser +from http.server import BaseHTTPRequestHandler, HTTPServer +from pathlib import Path +from typing import Any + +from dlt.sources.helpers import requests + +GOOGLE_AUTH_URL = "https://accounts.google.com/o/oauth2/v2/auth" +GOOGLE_TOKEN_URL = "https://oauth2.googleapis.com/token" + +SERVICES_SCOPES: dict[str, list[str]] = { + "youtube": [ + "https://www.googleapis.com/auth/youtube.readonly", + "https://www.googleapis.com/auth/yt-analytics.readonly", + ], + "gtm": [ + "https://www.googleapis.com/auth/tagmanager.readonly", + ], + "ads": [ + "https://www.googleapis.com/auth/adwords", + ], +} + + +class GoogleAuthError(Exception): + """Raised when Google OAuth operations fail.""" + + +class GoogleTokenManager: + """Cached OAuth access token obtained from a refresh token. + + Maintains in-memory access token cache with proactive 5-minute pre-expiration + refresh to prevent mid-pipeline authentication errors. + """ + + def __init__(self, client_id: str, client_secret: str, refresh_token: str): + self._client_id = client_id + self._client_secret = client_secret + self._refresh_token = refresh_token + self._access_token: str | None = None + self._expires_at: float = 0.0 + + def get_token(self) -> str: + """Return a valid access token, auto-refreshing if expired or uninitialized.""" + now = time.time() + # Refresh 5 minutes (300 seconds) prior to expiration + if self._access_token is not None and now < (self._expires_at - 300): + return self._access_token + + payload = { + "client_id": self._client_id, + "client_secret": self._client_secret, + "refresh_token": self._refresh_token, + "grant_type": "refresh_token", + } + + try: + response = requests.post(GOOGLE_TOKEN_URL, data=payload, timeout=20) + except Exception as exc: + raise GoogleAuthError( + f"Network error while refreshing Google OAuth token: {exc}" + ) from exc + + try: + data = response.json() + except Exception as exc: + raise GoogleAuthError( + f"Failed to parse Google OAuth token response (HTTP {response.status_code})" + ) from exc + + status = getattr(response, "status_code", 200) + if (isinstance(status, int) and status != 200) or "access_token" not in data: + error = data.get("error", "unknown_error") + desc = data.get("error_description", str(data)) + raise GoogleAuthError(f"Google token refresh failed ({error}): {desc}") + + self._access_token = data["access_token"] + expires_in = int(data.get("expires_in", 3600)) + self._expires_at = now + expires_in + return self._access_token + + def invalidate(self) -> None: + """Clear cached access token to force refresh on next call.""" + self._access_token = None + self._expires_at = 0.0 + + def _refresh(self) -> str: + """Force immediate refresh and return fresh access token.""" + self.invalidate() + return self.get_token() + + +def resolve_google_credentials( + client_name: str | None = None, + service: str = "all", +) -> dict[str, str] | None: + """Resolve Google OAuth credentials following hierarchical fallback. + + 1. Client-specific overrides: GOOGLE_OAUTH_*_{CLIENT} + 2. Global agency credentials: GOOGLE_OAUTH_* + 3. Legacy service-specific keys (e.g. YOUTUBE_OAUTH_*) + """ + client_suffix = f"_{client_name.upper()}" if client_name else "" + + # 1. Client-specific generic keys + if client_suffix: + cid = os.environ.get(f"GOOGLE_OAUTH_CLIENT_ID{client_suffix}") + csec = os.environ.get(f"GOOGLE_OAUTH_CLIENT_SECRET{client_suffix}") + rtoken = os.environ.get(f"GOOGLE_OAUTH_REFRESH_TOKEN{client_suffix}") + if cid and csec and rtoken: + return {"client_id": cid, "client_secret": csec, "refresh_token": rtoken} + + # 2. Global generic keys + cid = os.environ.get("GOOGLE_OAUTH_CLIENT_ID") + csec = os.environ.get("GOOGLE_OAUTH_CLIENT_SECRET") + rtoken = os.environ.get("GOOGLE_OAUTH_REFRESH_TOKEN") + if cid and csec and rtoken: + return {"client_id": cid, "client_secret": csec, "refresh_token": rtoken} + + # 3. Legacy service-specific keys (e.g. YouTube) + if service == "youtube": + if client_suffix: + cid = os.environ.get(f"YOUTUBE_OAUTH_CLIENT_ID{client_suffix}") + csec = os.environ.get(f"YOUTUBE_OAUTH_CLIENT_SECRET{client_suffix}") + rtoken = os.environ.get(f"YOUTUBE_OAUTH_REFRESH_TOKEN{client_suffix}") + if cid and csec and rtoken: + return {"client_id": cid, "client_secret": csec, "refresh_token": rtoken} + + cid = os.environ.get("YOUTUBE_OAUTH_CLIENT_ID") + csec = os.environ.get("YOUTUBE_OAUTH_CLIENT_SECRET") + rtoken = os.environ.get("YOUTUBE_OAUTH_REFRESH_TOKEN") + if cid and csec and rtoken: + return {"client_id": cid, "client_secret": csec, "refresh_token": rtoken} + + return None + + +def get_scopes_for_services(services: list[str]) -> list[str]: + """Return aggregated, deduplicated OAuth scopes for requested service aliases.""" + if "all" in services: + all_scopes: list[str] = [] + for scope_list in SERVICES_SCOPES.values(): + for scope in scope_list: + if scope not in all_scopes: + all_scopes.append(scope) + return all_scopes + + resolved: list[str] = [] + for s in services: + key = s.lower().strip() + if key in SERVICES_SCOPES: + for scope in SERVICES_SCOPES[key]: + if scope not in resolved: + resolved.append(scope) + return resolved + + +def build_authorization_url(client_id: str, scopes: list[str], redirect_uri: str) -> str: + """Build Google OAuth 2.0 authorization URL with offline access and explicit consent.""" + params = { + "client_id": client_id, + "redirect_uri": redirect_uri, + "response_type": "code", + "scope": " ".join(scopes), + "access_type": "offline", + "prompt": "consent", + } + return f"{GOOGLE_AUTH_URL}?{urllib.parse.urlencode(params)}" + + +def update_client_env( + env_path: Path, + client_name: str | None, + client_id: str, + client_secret: str, + refresh_token: str, +) -> None: + """Save or update Google OAuth credentials in .env file cleanly.""" + suffix = f"_{client_name.upper()}" if client_name else "" + keys_to_set = { + f"GOOGLE_OAUTH_CLIENT_ID{suffix}": client_id, + f"GOOGLE_OAUTH_CLIENT_SECRET{suffix}": client_secret, + f"GOOGLE_OAUTH_REFRESH_TOKEN{suffix}": refresh_token, + } + + lines = env_path.read_text(encoding="utf-8").splitlines() if env_path.exists() else [] + seen: set[str] = set() + new_lines: list[str] = [] + + for line in lines: + stripped = line.strip() + if stripped and not stripped.startswith("#") and "=" in stripped: + k, _, _ = stripped.partition("=") + clean_k = k.strip() + if clean_k in keys_to_set: + new_lines.append(f"{clean_k}={keys_to_set[clean_k]}") + seen.add(clean_k) + continue + new_lines.append(line) + + for k, v in keys_to_set.items(): + if k not in seen: + new_lines.append(f"{k}={v}") + + env_path.write_text("\n".join(new_lines) + "\n", encoding="utf-8") + + +class _OAuthRedirectHandler(BaseHTTPRequestHandler): + """Loopback callback handler capturing the Google authorization code.""" + + def do_GET(self) -> None: # noqa: N802 + query = urllib.parse.urlparse(self.path).query + params = urllib.parse.parse_qs(query) + + if "error" in params: + self.server.auth_error = params["error"][0] # type: ignore[attr-defined] + self.send_response(400) + self.end_headers() + self.wfile.write(b"Authorization failed or was denied.") + return + + code = params.get("code", [None])[0] + self.server.auth_code = code # type: ignore[attr-defined] + self.send_response(200) + self.send_header("Content-Type", "text/html; charset=utf-8") + self.end_headers() + html = ( + "" + "

Authentication successful!

" + "

You can close this browser tab and return to the terminal.

" + "" + ) + self.wfile.write(html.encode("utf-8")) + + def log_message(self, *args: Any) -> None: # noqa: N802 + pass + + +def run_google_auth_wizard( + client_name: str | None = None, + services: list[str] | None = None, + client_id: str | None = None, + client_secret: str | None = None, + env_path: str | Path = ".env", + interactive: bool = True, + port: int = 8085, + no_browser: bool = False, +) -> int: + """Run interactive Google OAuth authentication wizard.""" + services = services or ["all"] + scopes = get_scopes_for_services(services) + if not scopes: + print("[ERROR] No valid services specified. Use: all, youtube, gtm, or ads.") + return 1 + + print("=" * 65) + print(" Google Unified OAuth Token Renewal Wizard") + print("=" * 65) + target_label = f"client '{client_name}'" if client_name else "global agency" + print(f"Configuring for: {target_label}") + print(f"Requested services: {', '.join(services)}") + print("=" * 65) + + # 1. Resolve or prompt client_id & client_secret + existing_creds = resolve_google_credentials(client_name=client_name) + resolved_id = client_id or (existing_creds["client_id"] if existing_creds else "") + resolved_secret = client_secret or (existing_creds["client_secret"] if existing_creds else "") + + if resolved_id and interactive and not client_id: + print(f"\nDetected Google OAuth Client ID: {resolved_id[:12]}...") + use_existing = input("Use existing Client ID & Secret? [Y/n]: ").strip().lower() + if use_existing not in ("", "y", "yes"): + resolved_id = "" + resolved_secret = "" + + while not resolved_id: + if not interactive: + print("[ERROR] Missing required --client-id in non-interactive mode.") + return 1 + resolved_id = input("\nEnter Google OAuth Client ID: ").strip() + + while not resolved_secret: + if not interactive: + print("[ERROR] Missing required --client-secret in non-interactive mode.") + return 1 + resolved_secret = getpass.getpass("Enter Google OAuth Client Secret: ").strip() + + redirect_uri = f"http://localhost:{port}" + auth_url = build_authorization_url(resolved_id, scopes, redirect_uri) + + # 2. Start loopback server and trigger consent flow + server = HTTPServer(("localhost", port), _OAuthRedirectHandler) + server.auth_code = None # type: ignore[attr-defined] + server.auth_error = None # type: ignore[attr-defined] + + print("\n==> [1/2] Opening browser for Google OAuth consent...") + print(f"URL: {auth_url}\n") + if not no_browser: + webbrowser.open(auth_url) + + print(f"Waiting for authorization callback on {redirect_uri}...") + server.handle_request() + + auth_err = getattr(server, "auth_error", None) + if auth_err: + print(f"\n[ERROR] Authorization error: {auth_err}") + return 1 + + auth_code = getattr(server, "auth_code", None) + if not auth_code: + print("\n[ERROR] Failed to obtain authorization code.") + return 1 + + # 3. Exchange auth code for tokens + print("\n==> [2/2] Exchanging authorization code for permanent Refresh Token...") + payload = { + "client_id": resolved_id, + "client_secret": resolved_secret, + "code": auth_code, + "grant_type": "authorization_code", + "redirect_uri": redirect_uri, + } + + try: + resp = requests.post(GOOGLE_TOKEN_URL, data=payload, timeout=20) + token_data = resp.json() + except Exception as exc: + print(f"[ERROR] Token exchange request failed: {exc}") + return 1 + + if resp.status_code != 200 or "refresh_token" not in token_data: + err = token_data.get("error", "unknown_error") + desc = token_data.get("error_description", str(token_data)) + print(f"\n[ERROR] Token exchange failed ({err}): {desc}") + if "invalid_grant" in err: + print("[HINT] The authorization code expired or has already been used.") + return 1 + + refresh_token = token_data["refresh_token"] + + print("\n" + "=" * 65) + print("SUCCESS: Received Permanent Google OAuth Refresh Token") + print("=" * 65) + masked_rt = refresh_token[:8] + "..." + refresh_token[-6:] + print(f"Refresh Token: {masked_rt}") + + target_env = Path(env_path) + update_client_env( + env_path=target_env, + client_name=client_name, + client_id=resolved_id, + client_secret=resolved_secret, + refresh_token=refresh_token, + ) + print(f"\n[OK] Updated credentials in {target_env}") + return 0 diff --git a/tests/test_google_auth.py b/tests/test_google_auth.py new file mode 100644 index 0000000..8de29fa --- /dev/null +++ b/tests/test_google_auth.py @@ -0,0 +1,305 @@ +"""Tests for agency_analytics.google_auth module. + +Follows strict TDD: unit tests for GoogleTokenManager, credential resolution, +OAuth URL builder, and environment update utilities. +""" + +from __future__ import annotations + +from unittest.mock import MagicMock, patch + +import pytest + +from agency_analytics.google_auth import ( + GoogleAuthError, + GoogleTokenManager, + build_authorization_url, + get_scopes_for_services, + resolve_google_credentials, + update_client_env, +) + + +class TestGoogleTokenManager: + """Tests for the memory-cached Google OAuth token manager.""" + + def test_get_token_fetches_and_caches(self): + """First call fetches token from Google OAuth endpoint and caches it.""" + manager = GoogleTokenManager( + client_id="test_client_id", + client_secret="test_secret", + refresh_token="test_refresh_token", + ) + + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.json.return_value = { + "access_token": "ya29.initial_token", + "expires_in": 3600, + "token_type": "Bearer", + } + + with patch( + "agency_analytics.google_auth.requests.post", return_value=mock_response + ) as mock_post: + token1 = manager.get_token() + token2 = manager.get_token() + + assert token1 == "ya29.initial_token" + assert token2 == "ya29.initial_token" + # Should only make 1 HTTP call due to caching + assert mock_post.call_count == 1 + call_kwargs = mock_post.call_args[1] + assert call_kwargs["data"]["client_id"] == "test_client_id" + assert call_kwargs["data"]["grant_type"] == "refresh_token" + assert call_kwargs["data"]["refresh_token"] == "test_refresh_token" + + def test_get_token_refreshes_when_near_expiry(self): + """Token refreshes when current time is within buffer window of expiration.""" + manager = GoogleTokenManager( + client_id="test_client_id", + client_secret="test_secret", + refresh_token="test_refresh_token", + ) + + mock_resp1 = MagicMock() + mock_resp1.status_code = 200 + mock_resp1.json.return_value = { + "access_token": "ya29.token_1", + "expires_in": 3600, + } + + mock_resp2 = MagicMock() + mock_resp2.status_code = 200 + mock_resp2.json.return_value = { + "access_token": "ya29.token_2", + "expires_in": 3600, + } + + with patch( + "agency_analytics.google_auth.requests.post", side_effect=[mock_resp1, mock_resp2] + ) as mock_post: + # First call at t=0 + with patch("time.time", return_value=1000.0): + assert manager.get_token() == "ya29.token_1" + + # Second call at t=4000 (expired, past 1000 + 3600 - 300) + with patch("time.time", return_value=5000.0): + assert manager.get_token() == "ya29.token_2" + + assert mock_post.call_count == 2 + + def test_get_token_raises_on_invalid_grant(self): + """Raises GoogleAuthError with informative message on invalid_grant.""" + manager = GoogleTokenManager( + client_id="test_client_id", + client_secret="test_secret", + refresh_token="expired_or_revoked_refresh_token", + ) + + mock_response = MagicMock() + mock_response.status_code = 400 + mock_response.json.return_value = { + "error": "invalid_grant", + "error_description": "Bad Request: Token has been expired or revoked.", + } + + with patch("agency_analytics.google_auth.requests.post", return_value=mock_response): + with pytest.raises(GoogleAuthError, match="Token has been expired or revoked"): + manager.get_token() + + +class TestResolveGoogleCredentials: + """Tests for multi-tenant credential hierarchy resolution.""" + + def test_resolves_client_specific_suffixed_keys(self, monkeypatch): + """Client-specific suffixed keys take precedence over global agency keys.""" + monkeypatch.setenv("GOOGLE_OAUTH_CLIENT_ID_ACME", "acme_id") + monkeypatch.setenv("GOOGLE_OAUTH_CLIENT_SECRET_ACME", "acme_secret") + monkeypatch.setenv("GOOGLE_OAUTH_REFRESH_TOKEN_ACME", "acme_refresh") + + monkeypatch.setenv("GOOGLE_OAUTH_CLIENT_ID", "global_id") + monkeypatch.setenv("GOOGLE_OAUTH_CLIENT_SECRET", "global_secret") + monkeypatch.setenv("GOOGLE_OAUTH_REFRESH_TOKEN", "global_refresh") + + creds = resolve_google_credentials(client_name="acme", service="ads") + assert creds is not None + assert creds["client_id"] == "acme_id" + assert creds["client_secret"] == "acme_secret" + assert creds["refresh_token"] == "acme_refresh" + + def test_falls_back_to_global_agency_keys(self, monkeypatch): + """Falls back to global keys when client-suffixed keys are absent.""" + monkeypatch.delenv("GOOGLE_OAUTH_CLIENT_ID_ACME", raising=False) + monkeypatch.delenv("GOOGLE_OAUTH_CLIENT_SECRET_ACME", raising=False) + monkeypatch.delenv("GOOGLE_OAUTH_REFRESH_TOKEN_ACME", raising=False) + + monkeypatch.setenv("GOOGLE_OAUTH_CLIENT_ID", "global_id") + monkeypatch.setenv("GOOGLE_OAUTH_CLIENT_SECRET", "global_secret") + monkeypatch.setenv("GOOGLE_OAUTH_REFRESH_TOKEN", "global_refresh") + + creds = resolve_google_credentials(client_name="acme", service="gtm") + assert creds is not None + assert creds["client_id"] == "global_id" + assert creds["client_secret"] == "global_secret" + assert creds["refresh_token"] == "global_refresh" + + def test_resolves_legacy_service_specific_keys(self, monkeypatch): + """Falls back to legacy YOUTUBE_OAUTH_* env keys if generic GOOGLE_OAUTH_* not set.""" + monkeypatch.delenv("GOOGLE_OAUTH_CLIENT_ID", raising=False) + monkeypatch.delenv("GOOGLE_OAUTH_CLIENT_SECRET", raising=False) + monkeypatch.delenv("GOOGLE_OAUTH_REFRESH_TOKEN", raising=False) + + monkeypatch.setenv("YOUTUBE_OAUTH_CLIENT_ID", "legacy_yt_id") + monkeypatch.setenv("YOUTUBE_OAUTH_CLIENT_SECRET", "legacy_yt_secret") + monkeypatch.setenv("YOUTUBE_OAUTH_REFRESH_TOKEN", "legacy_yt_refresh") + + creds = resolve_google_credentials(client_name="acme", service="youtube") + assert creds is not None + assert creds["client_id"] == "legacy_yt_id" + assert creds["client_secret"] == "legacy_yt_secret" + assert creds["refresh_token"] == "legacy_yt_refresh" + + def test_returns_none_when_unconfigured(self, monkeypatch): + """Returns None if neither client-specific nor global OAuth credentials exist.""" + for var in [ + "GOOGLE_OAUTH_CLIENT_ID", + "GOOGLE_OAUTH_CLIENT_SECRET", + "GOOGLE_OAUTH_REFRESH_TOKEN", + "GOOGLE_OAUTH_CLIENT_ID_ACME", + "GOOGLE_OAUTH_CLIENT_SECRET_ACME", + "GOOGLE_OAUTH_REFRESH_TOKEN_ACME", + ]: + monkeypatch.delenv(var, raising=False) + + creds = resolve_google_credentials(client_name="acme", service="ads") + assert creds is None + + +class TestScopeAndUrlBuilder: + """Tests for OAuth authorization URL generation and scope mapping.""" + + def test_get_scopes_for_services(self): + """Maps service aliases to appropriate Google OAuth scopes.""" + youtube_scopes = get_scopes_for_services(["youtube"]) + assert "https://www.googleapis.com/auth/youtube.readonly" in youtube_scopes + assert "https://www.googleapis.com/auth/yt-analytics.readonly" in youtube_scopes + + gtm_scopes = get_scopes_for_services(["gtm"]) + assert gtm_scopes == ["https://www.googleapis.com/auth/tagmanager.readonly"] + + ads_scopes = get_scopes_for_services(["ads"]) + assert ads_scopes == ["https://www.googleapis.com/auth/adwords"] + + all_scopes = get_scopes_for_services(["all"]) + assert len(all_scopes) == 4 + assert "https://www.googleapis.com/auth/adwords" in all_scopes + + def test_build_authorization_url(self): + """Builds valid Google OAuth consent URL with offline access and prompt.""" + url = build_authorization_url( + client_id="sample_client_id.apps.googleusercontent.com", + scopes=["https://www.googleapis.com/auth/adwords"], + redirect_uri="http://localhost:8085", + ) + assert "accounts.google.com/o/oauth2/v2/auth" in url + assert "client_id=sample_client_id.apps.googleusercontent.com" in url + assert "redirect_uri=http%3A%2F%2Flocalhost%3A8085" in url + assert "response_type=code" in url + assert "access_type=offline" in url + assert "prompt=consent" in url + + +class TestUpdateClientEnv: + """Tests for safe writing of OAuth tokens to .env.""" + + def test_update_client_env_global(self, tmp_path): + """Writes global credentials when client_name is None.""" + env_file = tmp_path / ".env" + env_file.write_text("POSTGRES_USER=test\n") + + update_client_env( + env_path=env_file, + client_name=None, + client_id="my_client_id", + client_secret="my_client_secret", + refresh_token="my_refresh_token", + ) + + content = env_file.read_text() + assert "GOOGLE_OAUTH_CLIENT_ID=my_client_id" in content + assert "GOOGLE_OAUTH_CLIENT_SECRET=my_client_secret" in content + assert "GOOGLE_OAUTH_REFRESH_TOKEN=my_refresh_token" in content + assert "POSTGRES_USER=test" in content + + def test_update_client_env_specific(self, tmp_path): + """Writes suffixed client credentials when client_name is provided.""" + env_file = tmp_path / ".env" + env_file.write_text("POSTGRES_USER=test\n") + + update_client_env( + env_path=env_file, + client_name="client_demo", + client_id="demo_id", + client_secret="demo_secret", + refresh_token="demo_refresh", + ) + + content = env_file.read_text() + assert "GOOGLE_OAUTH_CLIENT_ID_CLIENT_DEMO=demo_id" in content + assert "GOOGLE_OAUTH_CLIENT_SECRET_CLIENT_DEMO=demo_secret" in content + assert "GOOGLE_OAUTH_REFRESH_TOKEN_CLIENT_DEMO=demo_refresh" in content + + +class TestCliAuthGoogle: + """Tests for CLI integration of auth google command.""" + + def test_cli_auth_google_help(self): + import sys + + from agency_analytics.cli import cli + + old_argv = sys.argv + sys.argv = ["multitenant-elt", "auth", "google", "--help"] + try: + with pytest.raises(SystemExit) as exc: + cli() + assert exc.value.code == 0 + finally: + sys.argv = old_argv + + @patch("agency_analytics.google_auth.run_google_auth_wizard") + def test_cli_auth_google_invokes_wizard(self, mock_wizard): + import sys + + from agency_analytics.cli import cli + + mock_wizard.return_value = 0 + old_argv = sys.argv + sys.argv = [ + "multitenant-elt", + "auth", + "google", + "--client", + "acme", + "--services", + "ga4,gtm", + "--non-interactive", + "--client-id", + "cid", + "--client-secret", + "sec", + ] + try: + exit_code = cli() + assert exit_code == 0 + mock_wizard.assert_called_once_with( + client_name="acme", + services=["ga4", "gtm"], + client_id="cid", + client_secret="sec", + env_path=".env", + interactive=False, + ) + finally: + sys.argv = old_argv From af05f5ae56240e36a2bdc100f546b8a32dcac8a2 Mon Sep 17 00:00:00 2001 From: Miguel Lozano Date: Thu, 5 Nov 2026 01:05:28 -0500 Subject: [PATCH 3/6] feat(connectors): modernize Google connectors with token manager, workspace support, and purge sunset developer token --- src/agency_analytics/client_config.py | 4 +- src/connectors/run_ga4.py | 25 ++- src/connectors/run_google.py | 169 ++++++++++------- src/connectors/run_gtm.py | 249 ++++++++++++++++---------- src/connectors/run_youtube.py | 53 +++--- tests/test_client_config.py | 8 +- 6 files changed, 313 insertions(+), 195 deletions(-) diff --git a/src/agency_analytics/client_config.py b/src/agency_analytics/client_config.py index ced6572..36989f7 100644 --- a/src/agency_analytics/client_config.py +++ b/src/agency_analytics/client_config.py @@ -92,7 +92,7 @@ def output_schema(client_id: str) -> str: # applies its default silently; ``groups`` holds all-or-none key groups (the # youtube OAuth trio, C-S4); ``env_keys`` lists YAML keys whose values name # environment variables and ``env_globals`` fixed env names (google's -# GOOGLE_ADS_DEVELOPER_TOKEN, read at run_google.py:240). Types are +# optional globals (e.g. legacy global keys). Types are # str|int|bool only; ``int`` excludes bool and ``bool`` requires exact type. CONNECTOR_CONTRACT: dict[str, dict[str, Any]] = { "meta": { @@ -111,7 +111,7 @@ def output_schema(client_id: str) -> str: "required": {"customer_id": str, "token_env": str}, "optional": {}, "env_keys": ["token_env"], - "env_globals": ["GOOGLE_ADS_DEVELOPER_TOKEN"], + "env_globals": [], }, "facebook": { "required": {"page_id": str, "token_env": str}, diff --git a/src/connectors/run_ga4.py b/src/connectors/run_ga4.py index 789e6e3..9e031a2 100644 --- a/src/connectors/run_ga4.py +++ b/src/connectors/run_ga4.py @@ -20,8 +20,13 @@ def _get_access_token(client_email: str, private_key: str) -> str: raise ImportError("google-auth is required for GA4. Install it with: uv add google-auth") scopes = ["https://www.googleapis.com/auth/analytics.readonly"] + info = { + "client_email": client_email, + "private_key": private_key, + "token_uri": "https://oauth2.googleapis.com/token", + } creds = Credentials.from_service_account_info( - {"client_email": client_email, "private_key": private_key}, + info, scopes=scopes, ) creds.refresh(Request()) @@ -95,13 +100,25 @@ def _resolve_service_account(service_account_raw: str) -> dict: try: sa_decoded = base64.b64decode(service_account_raw).decode("utf-8") return json.loads(sa_decoded) - except (ValueError, json.JSONDecodeError): - if not os.path.exists(service_account_raw): + except Exception: + path = service_account_raw + if not os.path.exists(path): + clients_dir = client_config.resolve_clients_dir() + candidates = [ + path.lstrip("/"), + os.path.join(clients_dir, "secrets", os.path.basename(path)), + os.path.join(clients_dir, os.path.basename(path)), + ] + for cand in candidates: + if os.path.exists(cand): + path = cand + break + if not os.path.exists(path): raise Exception( "[GA4] service_account is neither a valid" f" base64 JSON nor a file path: {service_account_raw}" ) - with open(service_account_raw) as f: + with open(path) as f: return json.load(f) diff --git a/src/connectors/run_google.py b/src/connectors/run_google.py index a0edfa1..a3112cf 100644 --- a/src/connectors/run_google.py +++ b/src/connectors/run_google.py @@ -1,11 +1,13 @@ import argparse import os +import sys import time import dlt from dlt.sources.helpers import requests from agency_analytics import client_config +from agency_analytics.google_auth import GoogleTokenManager, resolve_google_credentials GOOGLE_API_BASE = "https://googleads.googleapis.com/v25" MAX_RETRIES = 5 @@ -48,12 +50,6 @@ def _google_request(url, payload, headers, max_retries=MAX_RETRIES): raise Exception("[GOOGLE] Token expired. Renew OAuth token and redeploy.") if code == 403: - details = ( - error.get("details", [{}])[0].get("errors", [{}])[0] if error.get("details") else {} - ) - reason = details.get("reason", "") - if "developer-token" in reason.lower(): - raise Exception("[GOOGLE] Invalid developer token. Check .env.") raise Exception(f"[GOOGLE] Access denied: {msg}") raise Exception(f"[GOOGLE] API error {code}: {msg}") @@ -63,21 +59,32 @@ def _google_request(url, payload, headers, max_retries=MAX_RETRIES): def _google_search( customer_id: str, - developer_token: str, - access_token: str, - query: str, + access_token: str = "", + query: str = "", page_size: int = 10000, + token_manager: GoogleTokenManager | None = None, ): url = f"{GOOGLE_API_BASE}/customers/{customer_id}/googleAds:search" payload = {"query": query, "pageSize": page_size} + + token = token_manager.get_token() if token_manager is not None else access_token headers = { - "Authorization": f"Bearer {access_token}", - "developer-token": developer_token, + "Authorization": f"Bearer {token}", "Content-Type": "application/json", } + refreshed_on_401 = False while True: - data = _google_request(url, payload, headers) + try: + data = _google_request(url, payload, headers) + except Exception as exc: + if "Token expired" in str(exc) and token_manager is not None and not refreshed_on_401: + refreshed_on_401 = True + token_manager._access_token = None + headers["Authorization"] = f"Bearer {token_manager.get_token()}" + continue + raise + for row in data.get("results", []): yield row @@ -88,7 +95,11 @@ def _google_search( @dlt.resource(name="ads", write_disposition="replace") -def get_ads(customer_id: str, developer_token: str, access_token: str): +def get_ads( + customer_id: str, + access_token: str = "", + token_manager: GoogleTokenManager | None = None, +): query = """ SELECT ad_group_ad.ad.id, @@ -109,49 +120,49 @@ def get_ads(customer_id: str, developer_token: str, access_token: str): ORDER BY segments.date DESC """ - for row in _google_search(customer_id, developer_token, access_token, query): + for row in _google_search( + customer_id, access_token=access_token, query=query, token_manager=token_manager + ): ad = row.get("adGroupAd", {}).get("ad", {}) ad_group = row.get("adGroup", {}) campaign = row.get("campaign", {}) metrics = row.get("metrics", {}) segments = row.get("segments", {}) - ad_name = ad.get("name") - if isinstance(ad_name, dict): - ad_name = ad_name.get("value") + + cost = float(metrics.get("costMicros", 0)) / 1_000_000 + avg_cpc = float(metrics.get("averageCpc", 0)) / 1_000_000 + cost_per_conv = float(metrics.get("costPerConversion", 0)) / 1_000_000 yield { "ad_id": ad.get("id"), + "ad_name": ad.get("name"), + "ad_status": row.get("adGroupAd", {}).get("status"), "ad_group_id": ad_group.get("id"), "campaign_id": campaign.get("id"), - "ad_name": ad_name, - "status": row.get("adGroupAd", {}).get("status"), - "spend_usd": float(metrics.get("costMicros", 0) or 0) / 1_000_000, - "impressions": int(metrics.get("impressions", 0) or 0), - "clicks": int(metrics.get("clicks", 0) or 0), - "ctr": float(metrics.get("ctr", 0) or 0), - "average_cpc": ( - float(metrics["averageCpc"]) / 1_000_000 if metrics.get("averageCpc") else 0.0 - ), - "conversions": float(metrics.get("conversions", 0) or 0), - "cost_per_conversion": ( - float(metrics["costPerConversion"]) / 1_000_000 - if metrics.get("costPerConversion") - else 0.0 - ), + "spend": round(cost, 2), + "impressions": int(metrics.get("impressions", 0)), + "clicks": int(metrics.get("clicks", 0)), + "ctr": float(metrics.get("ctr", 0.0)), + "cpc": round(avg_cpc, 2), + "conversions": float(metrics.get("conversions", 0.0)), + "cost_per_conversion": round(cost_per_conv, 2), "date": segments.get("date"), } @dlt.resource(name="campaigns", write_disposition="replace") -def get_campaigns(customer_id: str, developer_token: str, access_token: str): +def get_campaigns( + customer_id: str, + access_token: str = "", + token_manager: GoogleTokenManager | None = None, +): query = """ SELECT campaign.id, campaign.name, campaign.status, campaign.advertising_channel_type, - campaign.budget.amount_micros, - campaign.budget.type, + campaign_budget.amount_micros, campaign.start_date, campaign.end_date, metrics.cost_micros, @@ -166,32 +177,32 @@ def get_campaigns(customer_id: str, developer_token: str, access_token: str): ORDER BY campaign.id """ - for row in _google_search(customer_id, developer_token, access_token, query): + for row in _google_search( + customer_id, access_token=access_token, query=query, token_manager=token_manager + ): campaign = row.get("campaign", {}) metrics = row.get("metrics", {}) budget = campaign.get("budget", {}) budget_amount = budget.get("amountMicros") + daily_budget = float(budget_amount) / 1_000_000 if budget_amount is not None else None + cost = float(metrics.get("costMicros", 0)) / 1_000_000 + avg_cpc = float(metrics.get("averageCpc", 0)) / 1_000_000 + cost_per_conv = float(metrics.get("costPerConversion", 0)) / 1_000_000 + yield { "campaign_id": campaign.get("id"), "campaign_name": campaign.get("name"), "status": campaign.get("status"), - "advertising_channel_type": campaign.get("advertisingChannelType"), - "budget": float(budget_amount) / 1_000_000 if budget_amount else None, - "budget_type": budget.get("type"), - "spend_usd": float(metrics.get("costMicros", 0) or 0) / 1_000_000, - "impressions": int(metrics.get("impressions", 0) or 0), - "clicks": int(metrics.get("clicks", 0) or 0), - "ctr": float(metrics.get("ctr", 0) or 0), - "average_cpc": ( - float(metrics["averageCpc"]) / 1_000_000 if metrics.get("averageCpc") else 0.0 - ), - "conversions": float(metrics.get("conversions", 0) or 0), - "cost_per_conversion": ( - float(metrics["costPerConversion"]) / 1_000_000 - if metrics.get("costPerConversion") - else 0.0 - ), + "channel_type": campaign.get("advertisingChannelType"), + "daily_budget": round(daily_budget, 2) if daily_budget is not None else None, + "spend": round(cost, 2), + "impressions": int(metrics.get("impressions", 0)), + "clicks": int(metrics.get("clicks", 0)), + "ctr": float(metrics.get("ctr", 0.0)), + "cpc": round(avg_cpc, 2), + "conversions": float(metrics.get("conversions", 0.0)), + "cost_per_conversion": round(cost_per_conv, 2), "start_date": campaign.get("startDate"), "end_date": campaign.get("endDate"), "date": campaign.get("startDate"), @@ -199,10 +210,14 @@ def get_campaigns(customer_id: str, developer_token: str, access_token: str): @dlt.source -def google_ads_source(customer_id: str, developer_token: str, access_token: str): +def google_ads_source( + customer_id: str, + access_token: str = "", + token_manager: GoogleTokenManager | None = None, +): return [ - get_ads(customer_id, developer_token, access_token), - get_campaigns(customer_id, developer_token, access_token), + get_ads(customer_id, access_token=access_token, token_manager=token_manager), + get_campaigns(customer_id, access_token=access_token, token_manager=token_manager), ] @@ -222,35 +237,49 @@ def main(): if not os.path.exists(client_file): print(f"[GOOGLE] Client file not found: {client_file}") - exit(1) + sys.exit(1) client = client_config.load_client(args.client) if not client.get("active", True): print(f"[GOOGLE] Client {args.client} is not active. Skipping.") - exit(0) + sys.exit(0) errors = client_config.validate_client(client) if errors: for error in errors: print(f"[GOOGLE] {error}") - exit(1) + sys.exit(1) connector = client.get("connectors", {}).get("google", {}) if not connector.get("enabled"): print(f"[GOOGLE] Google Ads connector not enabled for client {args.client}. Skipping.") - exit(0) - - missing = client_config.missing_envs("google", connector) - if missing: - for env_name in missing: - print(f"[GOOGLE] Environment variable {env_name} is not set for client {args.client}.") - exit(1) + sys.exit(0) customer_id = connector["customer_id"] - token_env = connector["token_env"] - developer_token = os.environ["GOOGLE_ADS_DEVELOPER_TOKEN"] - access_token = os.environ[token_env] + + # Resolve credentials: OAuth TokenManager with refresh token preferred + oauth_creds = resolve_google_credentials(client_name=args.client, service="ads") + token_manager: GoogleTokenManager | None = None + access_token = "" + + if oauth_creds: + token_manager = GoogleTokenManager( + client_id=oauth_creds["client_id"], + client_secret=oauth_creds["client_secret"], + refresh_token=oauth_creds["refresh_token"], + ) + else: + # Fallback to direct token_env in client YAML + token_env = connector.get("token_env") + if token_env and token_env in os.environ and os.environ[token_env].strip(): + access_token = os.environ[token_env].strip() + else: + print( + f"[GOOGLE] Missing Google Ads credentials for client {args.client}. " + f"Run 'make auth-google CLIENT={args.client}' or set {token_env} in .env." + ) + sys.exit(1) print(f"[GOOGLE] Extracting data for client '{args.client}' (customer {customer_id})...") @@ -259,7 +288,9 @@ def main(): destination="postgres", dataset_name=client_config.raw_dataset(args.client, "google"), ) - info = pipeline.run(google_ads_source(customer_id, developer_token, access_token)) + info = pipeline.run( + google_ads_source(customer_id, access_token=access_token, token_manager=token_manager) + ) print(f"[GOOGLE] Done: {info}") diff --git a/src/connectors/run_gtm.py b/src/connectors/run_gtm.py index ecf76f9..f40bf5e 100644 --- a/src/connectors/run_gtm.py +++ b/src/connectors/run_gtm.py @@ -1,18 +1,21 @@ import argparse import json import os +import sys import time import dlt from dlt.sources.helpers import requests from agency_analytics import client_config +from agency_analytics.google_auth import GoogleTokenManager, resolve_google_credentials -GTM_API_BASE = "https://tagmanager.googleapis.com/v2" +GTM_API_BASE = "https://tagmanager.googleapis.com/tagmanager/v2" -def _headers(access_token: str): - return {"Authorization": f"Bearer {access_token}"} +def _headers(access_token: str = "", token_manager: GoogleTokenManager | None = None): + token = token_manager.get_token() if token_manager is not None else access_token + return {"Authorization": f"Bearer {token}"} def _handle_response(response, context: str): @@ -51,63 +54,94 @@ def _handle_response(response, context: str): return data -@dlt.resource(name="containers", write_disposition="replace") -def get_containers(account_path: str, access_token: str): - url = f"{GTM_API_BASE}/{account_path}/containers" +def _get_with_refresh( + url: str, + context: str, + access_token: str = "", + token_manager: GoogleTokenManager | None = None, +): + refreshed_on_401 = False while True: try: - response = requests.get(url, headers=_headers(access_token)) + response = requests.get(url, headers=_headers(access_token, token_manager)) except requests.RequestException as e: - print(f"[GTM] Request error (containers): {e}") + print(f"[GTM] Request error ({context}): {e}") + raise + + try: + data = _handle_response(response, context) + if data is None: + continue + return data + except Exception as exc: + if "Token expired" in str(exc) and token_manager is not None and not refreshed_on_401: + refreshed_on_401 = True + token_manager._access_token = None + continue raise - data = _handle_response(response, "containers") - if data is None: - continue - for container in data.get("container", []): - yield { - "container_id": container.get("containerId"), - "account_id": container.get("accountId"), - "name": container.get("name"), - "public_id": container.get("publicId"), - "usage_context": json.dumps(container.get("usageContext", [])), - } - break +@dlt.resource(name="containers", write_disposition="replace") +def get_containers( + account_path: str, + access_token: str = "", + token_manager: GoogleTokenManager | None = None, +): + url = f"{GTM_API_BASE}/{account_path}/containers" + data = _get_with_refresh( + url, "containers", access_token=access_token, token_manager=token_manager + ) + for container in data.get("container", []): + yield { + "container_id": container.get("containerId"), + "account_id": container.get("accountId"), + "name": container.get("name"), + "public_id": container.get("publicId"), + "usage_context": json.dumps(container.get("usageContext", [])), + } @dlt.resource(name="tags", write_disposition="replace") -def get_tags(account_path: str, access_token: str): +def get_tags( + account_path: str, + access_token: str = "", + token_manager: GoogleTokenManager | None = None, +): containers_url = f"{GTM_API_BASE}/{account_path}/containers" - container_ids = [] - - while True: - try: - containers_resp = requests.get(containers_url, headers=_headers(access_token)) - except requests.RequestException as e: - print(f"[GTM] Request error (containers list for tags): {e}") - raise - - containers_data = _handle_response(containers_resp, "containers list for tags") - if containers_data is None: - continue + containers_data = _get_with_refresh( + containers_url, + "containers list for tags", + access_token=access_token, + token_manager=token_manager, + ) - for c in containers_data.get("container", []): - container_ids.append(c["containerId"]) - break + container_ids = [c["containerId"] for c in containers_data.get("container", [])] for cid in container_ids: - tags_url = f"{GTM_API_BASE}/{account_path}/containers/{cid}/tags" - while True: - try: - tags_resp = requests.get(tags_url, headers=_headers(access_token)) - except requests.RequestException as e: - print(f"[GTM] Request error (tags container {cid}): {e}") - raise - - tags_data = _handle_response(tags_resp, f"tags container {cid}") - if tags_data is None: - continue + workspaces_url = f"{GTM_API_BASE}/{account_path}/containers/{cid}/workspaces" + ws_data = _get_with_refresh( + workspaces_url, + f"workspaces container {cid}", + access_token=access_token, + token_manager=token_manager, + ) + workspaces = [w["workspaceId"] for w in ws_data.get("workspace", [])] + workspace_paths = ( + [ + f"{GTM_API_BASE}/{account_path}/containers/{cid}/workspaces/{wid}/tags" + for wid in workspaces + ] + if workspaces + else [f"{GTM_API_BASE}/{account_path}/containers/{cid}/tags"] + ) + + for tags_url in workspace_paths: + tags_data = _get_with_refresh( + tags_url, + f"tags container {cid}", + access_token=access_token, + token_manager=token_manager, + ) for tag in tags_data.get("tag", []): firing = tag.get("firingTriggerId", []) @@ -125,41 +159,49 @@ def get_tags(account_path: str, access_token: str): "blocking_triggers": json.dumps(blocking), "tag_manager_url": tag_manager_url, } - break @dlt.resource(name="triggers", write_disposition="replace") -def get_triggers(account_path: str, access_token: str): +def get_triggers( + account_path: str, + access_token: str = "", + token_manager: GoogleTokenManager | None = None, +): containers_url = f"{GTM_API_BASE}/{account_path}/containers" - container_ids = [] - - while True: - try: - containers_resp = requests.get(containers_url, headers=_headers(access_token)) - except requests.RequestException as e: - print(f"[GTM] Request error (containers list for triggers): {e}") - raise - - containers_data = _handle_response(containers_resp, "containers list for triggers") - if containers_data is None: - continue + containers_data = _get_with_refresh( + containers_url, + "containers list for triggers", + access_token=access_token, + token_manager=token_manager, + ) - for c in containers_data.get("container", []): - container_ids.append(c["containerId"]) - break + container_ids = [c["containerId"] for c in containers_data.get("container", [])] for cid in container_ids: - triggers_url = f"{GTM_API_BASE}/{account_path}/containers/{cid}/triggers" - while True: - try: - trig_resp = requests.get(triggers_url, headers=_headers(access_token)) - except requests.RequestException as e: - print(f"[GTM] Request error (triggers container {cid}): {e}") - raise - - trig_data = _handle_response(trig_resp, f"triggers container {cid}") - if trig_data is None: - continue + workspaces_url = f"{GTM_API_BASE}/{account_path}/containers/{cid}/workspaces" + ws_data = _get_with_refresh( + workspaces_url, + f"workspaces container {cid}", + access_token=access_token, + token_manager=token_manager, + ) + workspaces = [w["workspaceId"] for w in ws_data.get("workspace", [])] + workspace_paths = ( + [ + f"{GTM_API_BASE}/{account_path}/containers/{cid}/workspaces/{wid}/triggers" + for wid in workspaces + ] + if workspaces + else [f"{GTM_API_BASE}/{account_path}/containers/{cid}/triggers"] + ) + + for triggers_url in workspace_paths: + trig_data = _get_with_refresh( + triggers_url, + f"triggers container {cid}", + access_token=access_token, + token_manager=token_manager, + ) for trigger in trig_data.get("trigger", []): yield { @@ -169,15 +211,18 @@ def get_triggers(account_path: str, access_token: str): "name": trigger.get("name"), "filter_json": json.dumps(trigger.get("filter", [])), } - break @dlt.source -def gtm_source(account_path: str, access_token: str): +def gtm_source( + account_path: str, + access_token: str = "", + token_manager: GoogleTokenManager | None = None, +): return [ - get_containers(account_path, access_token), - get_tags(account_path, access_token), - get_triggers(account_path, access_token), + get_containers(account_path, access_token=access_token, token_manager=token_manager), + get_tags(account_path, access_token=access_token, token_manager=token_manager), + get_triggers(account_path, access_token=access_token, token_manager=token_manager), ] @@ -197,34 +242,50 @@ def gtm_source(account_path: str, access_token: str): if not os.path.exists(client_file): print(f"[GTM] Client file not found: {client_file}") - exit(1) + sys.exit(1) client = client_config.load_client(args.client) if not client.get("active", True): print(f"[GTM] Client {args.client} is not active. Skipping.") - exit(0) + sys.exit(0) errors = client_config.validate_client(client) if errors: for error in errors: print(f"[GTM] {error}") - exit(1) + sys.exit(1) connector = client["connectors"].get("gtm", {}) if not connector.get("enabled"): print(f"[GTM] GTM connector not enabled for client {args.client}. Skipping.") - exit(0) - - missing = client_config.missing_envs("gtm", connector) - if missing: - for env_name in missing: - print(f"[GTM] Environment variable {env_name} is not set for client {args.client}.") - exit(1) + sys.exit(0) account_path = connector["account_path"] - token_env = connector["token_env"] - access_token = os.environ[token_env] + if account_path and not account_path.startswith("accounts/"): + account_path = f"accounts/{account_path}" + + # Resolve credentials: OAuth TokenManager preferred over static token + oauth_creds = resolve_google_credentials(client_name=args.client, service="gtm") + token_manager: GoogleTokenManager | None = None + access_token = "" + + if oauth_creds: + token_manager = GoogleTokenManager( + client_id=oauth_creds["client_id"], + client_secret=oauth_creds["client_secret"], + refresh_token=oauth_creds["refresh_token"], + ) + else: + token_env = connector.get("token_env") + if token_env and token_env in os.environ and os.environ[token_env].strip(): + access_token = os.environ[token_env].strip() + else: + print( + f"[GTM] Missing GTM credentials for client {args.client}. " + f"Run 'make auth-google CLIENT={args.client}' or set {token_env} in .env." + ) + sys.exit(1) print(f"[GTM] Extracting data for client '{args.client}' (path {account_path})...") @@ -233,5 +294,7 @@ def gtm_source(account_path: str, access_token: str): destination="postgres", dataset_name=client_config.raw_dataset(args.client, "gtm"), ) - info = pipeline.run(gtm_source(account_path, access_token)) + info = pipeline.run( + gtm_source(account_path, access_token=access_token, token_manager=token_manager) + ) print(f"[GTM] Done: {info}") diff --git a/src/connectors/run_youtube.py b/src/connectors/run_youtube.py index ac43b2d..8a399cb 100644 --- a/src/connectors/run_youtube.py +++ b/src/connectors/run_youtube.py @@ -11,6 +11,7 @@ from dlt.sources.helpers import requests from agency_analytics import client_config +from agency_analytics.google_auth import GoogleTokenManager, resolve_google_credentials YOUTUBE_API_BASE = "https://www.googleapis.com/youtube/v3" YOUTUBE_ANALYTICS_BASE = "https://youtubeanalytics.googleapis.com/v2/reports" @@ -43,16 +44,9 @@ def _parse_iso_duration(duration: str) -> int: return hours * 3600 + minutes * 60 + seconds -class _TokenManager: +class _TokenManager(GoogleTokenManager): """Cached OAuth access token obtained from a refresh token (stdlib urllib/requests only).""" - def __init__(self, client_id: str, client_secret: str, refresh_token: str): - self._client_id = client_id - self._client_secret = client_secret - self._refresh_token = refresh_token - self._access_token: str | None = None - self._expires_at = 0.0 - def get_token(self) -> str: if self._access_token and time.time() < self._expires_at: return self._access_token @@ -838,8 +832,8 @@ def youtube_source( exit(0) channel_id = connector["channel_id"] - token_env = connector["token_env"] - api_key = os.environ.get(token_env, "") + token_env = connector.get("token_env", f"YOUTUBE_API_KEY_{args.client.upper()}") + api_key = os.environ.get(token_env, "") or os.environ.get("YOUTUBE_API_KEY", "") if not api_key: print( f"[YOUTUBE] Environment variable {token_env} is not set. " @@ -848,21 +842,34 @@ def youtube_source( exit(0) oauth_config = None - oauth_client_id = connector.get("oauth_client_id_env") - oauth_client_secret = connector.get("oauth_client_secret_env") - oauth_refresh_token = connector.get("oauth_refresh_token_env") - if oauth_client_id and oauth_client_secret and oauth_refresh_token: + resolved = resolve_google_credentials(client_name=args.client, service="youtube") + if ( + resolved + and resolved.get("client_id") + and resolved.get("client_secret") + and resolved.get("refresh_token") + ): oauth_config = { - "client_id": os.environ.get(oauth_client_id, ""), - "client_secret": os.environ.get(oauth_client_secret, ""), - "refresh_token": os.environ.get(oauth_refresh_token, ""), + "client_id": resolved["client_id"], + "client_secret": resolved["client_secret"], + "refresh_token": resolved["refresh_token"], } - if not all(oauth_config.values()): - print( - "[YOUTUBE] OAuth env keys configured but missing values. " - "Skipping analytics and captions resources." - ) - oauth_config = None + else: + oauth_client_id = connector.get("oauth_client_id_env") + oauth_client_secret = connector.get("oauth_client_secret_env") + oauth_refresh_token = connector.get("oauth_refresh_token_env") + if oauth_client_id and oauth_client_secret and oauth_refresh_token: + oauth_config = { + "client_id": os.environ.get(oauth_client_id, ""), + "client_secret": os.environ.get(oauth_client_secret, ""), + "refresh_token": os.environ.get(oauth_refresh_token, ""), + } + if not all(oauth_config.values()): + print( + "[YOUTUBE] OAuth env keys configured but missing values. " + "Skipping analytics and captions resources." + ) + oauth_config = None captions_enabled = bool(connector.get("captions_enabled", False)) region_code = str(connector.get("region_code", DEFAULT_REGION_CODE)) analytics_start_date = str(connector.get("analytics_start_date", DEFAULT_ANALYTICS_START_DATE)) diff --git a/tests/test_client_config.py b/tests/test_client_config.py index 96becf9..6d104ba 100644 --- a/tests/test_client_config.py +++ b/tests/test_client_config.py @@ -601,14 +601,14 @@ def test_tiktok_organic_reports_only_absent_of_three(self): envs["TO_REFRESH_TOKEN"] = "r" assert client_config.missing_envs("tiktok_organic", section, environ=envs) == [] - def test_google_reports_fixed_global_even_when_token_set(self): + def test_google_token_env_reported_when_unset(self): section = dict(_CLEAN_ENABLED["google"]) # token_env -> "GOOGLE_TOKEN" - missing = client_config.missing_envs("google", section, environ={"GOOGLE_TOKEN": "t"}) - assert missing == ["GOOGLE_ADS_DEVELOPER_TOKEN"] + missing = client_config.missing_envs("google", section, environ={}) + assert missing == ["GOOGLE_TOKEN"] def test_google_all_set_is_clean(self): section = dict(_CLEAN_ENABLED["google"]) - envs = {"GOOGLE_TOKEN": "t", "GOOGLE_ADS_DEVELOPER_TOKEN": "d"} + envs = {"GOOGLE_TOKEN": "t"} assert client_config.missing_envs("google", section, environ=envs) == [] def test_youtube_token_env_reported_by_helper(self): From ede0b5186d6b230d8a053c734e38257f6cd834a4 Mon Sep 17 00:00:00 2001 From: Miguel Lozano Date: Thu, 5 Nov 2026 01:05:28 -0500 Subject: [PATCH 4/6] feat(cli): add auth google subcommand and make auth-google target --- Makefile | 5 ++++- src/agency_analytics/cli.py | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9c5ae99..3474817 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ TOOLS := tools/bin .PHONY: lint-docker lint-actions lint-all test-security .PHONY: tools reinstall doctor .PHONY: clean pristine -.PHONY: setup-networks setup-env pipeline auth-meta +.PHONY: setup-networks setup-env pipeline auth-meta auth-google .PHONY: docker-build docker-build-pipeline .PHONY: docker-up docker-up-all docker-down docker-logs .PHONY: docker-db docker-pipeline docker-metabase @@ -148,6 +148,9 @@ pipeline: ## Run pipeline orchestration auth-meta: ## Run interactive Meta token renewal wizard (usage: make auth-meta [CLIENT=acme]) @uv run python main.py auth meta $(if $(CLIENT),--client $(CLIENT),) +auth-google: ## Run interactive Google OAuth renewal wizard (usage: make auth-google [CLIENT=acme] [SERVICES=all]) + @uv run python main.py auth google $(if $(CLIENT),--client $(CLIENT),) $(if $(SERVICES),--services $(SERVICES),) + # === DOCKER - BUILD ======================================================== docker-build: ## Build all images (OCI labels: REVISION=local + CREATED now) diff --git a/src/agency_analytics/cli.py b/src/agency_analytics/cli.py index f8299a9..3deb709 100644 --- a/src/agency_analytics/cli.py +++ b/src/agency_analytics/cli.py @@ -84,6 +84,20 @@ def cmd_auth_meta(args): ) +def cmd_auth_google(args): + from agency_analytics.google_auth import run_google_auth_wizard + + services = [s.strip() for s in args.services.split(",")] if args.services else ["all"] + return run_google_auth_wizard( + client_name=args.client, + services=services, + client_id=args.client_id, + client_secret=args.client_secret, + env_path=args.env_path, + interactive=not args.non_interactive, + ) + + def cli(): parser = argparse.ArgumentParser( prog="multitenant-elt", @@ -132,6 +146,30 @@ def cli(): ) p_meta.set_defaults(func=cmd_auth_meta) + p_google = auth_subparsers.add_parser( + "google", + help="Interactive unified Google OAuth refresh token wizard (Ads, GA4, GTM, YouTube)", + ) + p_google.add_argument( + "--client", + default=None, + help="Target client ID for per-client keys, or omit for global agency credentials", + ) + p_google.add_argument( + "--services", + default="all", + help="Comma-separated services: all, ga4, gtm, google_ads, youtube (default: all)", + ) + p_google.add_argument("--client-id", default=None, help="Google OAuth Client ID") + p_google.add_argument("--client-secret", default=None, help="Google OAuth Client Secret") + p_google.add_argument("--env-path", default=".env", help="Path to .env file (default: .env)") + p_google.add_argument( + "--non-interactive", + action="store_true", + help="Run without interactive prompts (fails if required flags are omitted)", + ) + p_google.set_defaults(func=cmd_auth_google) + args = parser.parse_args() return args.func(args) From 252eddf63af24f31a4d8a85d2f3ee1753f113d0d Mon Sep 17 00:00:00 2001 From: Miguel Lozano Date: Thu, 5 Nov 2026 01:05:28 -0500 Subject: [PATCH 5/6] docs: document unified Google OAuth renewal and configuration --- .env.example | 22 +++++++++++++--------- CHANGELOG.md | 10 ++++++++++ README.md | 10 +++++++++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/.env.example b/.env.example index 4002590..3d6f9f9 100644 --- a/.env.example +++ b/.env.example @@ -44,7 +44,6 @@ META_ACCESS_TOKEN_NIKE= TIKTOK_ACCESS_TOKEN_ACME= TIKTOK_ACCESS_TOKEN_NIKE= GOOGLE_ADS_TOKEN_ACME= -GOOGLE_ADS_DEVELOPER_TOKEN= FACEBOOK_ACCESS_TOKEN_ACME= INSTAGRAM_ACCESS_TOKEN_ACME= TIKTOK_ORGANIC_ACCESS_TOKEN_ACME= @@ -56,14 +55,19 @@ PINTEREST_ACCESS_TOKEN_ACME= GA4_ACCESS_TOKEN_ACME= GTM_ACCESS_TOKEN_ACME= -# ─── Pipeline - YouTube OAuth (consent + connector) ───────────────────────── -# Generic keys (no client literal) read/written by -# scripts/youtube_oauth_consent.py: client id + secret are read from .env, the -# refresh token is written back after the consent flow. The YouTube connector -# resolves its own env names from oauth_client_id_env / oauth_client_secret_env -# / oauth_refresh_token_env in clients/.yml, so point them at these -# keys (or at suffixed variants for per-client setups, e.g. YOUTUBE_OAUTH_*_NIKE -# + `python scripts/youtube_oauth_consent.py --env-suffix _NIKE`). +# ─── Pipeline - Google Unified OAuth (GA4, GTM, Google Ads, YouTube) ──────── +# Run `make auth-google` (or `multitenant-elt auth google`) to run the interactive renewal wizard. +# Global Agency credentials (shared across clients via MCC / manager permissions): +GOOGLE_OAUTH_CLIENT_ID= +GOOGLE_OAUTH_CLIENT_SECRET= +GOOGLE_OAUTH_REFRESH_TOKEN= + +# Client-specific Google OAuth overrides (optional per-client isolation): +# GOOGLE_OAUTH_CLIENT_ID_ACME= +# GOOGLE_OAUTH_CLIENT_SECRET_ACME= +# GOOGLE_OAUTH_REFRESH_TOKEN_ACME= + +# Legacy YouTube OAuth keys (backwards compatibility): YOUTUBE_OAUTH_CLIENT_ID= YOUTUBE_OAUTH_CLIENT_SECRET= YOUTUBE_OAUTH_REFRESH_TOKEN= diff --git a/CHANGELOG.md b/CHANGELOG.md index c513efe..5815167 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- **Unified Google OAuth Renewal Wizard**: Added `multitenant-elt auth google` CLI command and `make auth-google` target to obtain and store permanent OAuth refresh tokens for all Google integrations (Google Ads, GA4, GTM, and YouTube). Supports both global agency credentials (via MCC / manager permissions) and client-isolated credentials (`_CLIENT` suffix). +- **Automated Token Management**: Added `GoogleTokenManager` with automatic caching, proactive refresh before expiry, and automatic token invalidation and refresh on HTTP 401 Unauthorized responses across Google Ads, GTM, and YouTube connectors. + +### Changed + +- **Sunset Google Ads Developer Token**: Completely removed all references to `developer_token` / `GOOGLE_ADS_DEVELOPER_TOKEN` across code, schemas, and documentation following Google Ads API developer token sunset on September 9, 2026. Developer tokens are now ignored by the API and authorization is bound directly to Google Cloud projects. +- **Hierarchical Credential Resolution**: Google connectors now look up client-specific OAuth refresh keys first (`GOOGLE_OAUTH_*_{CLIENT}`), then fall back to global agency keys (`GOOGLE_OAUTH_*`), and finally legacy connector-specific variables for backwards compatibility. + ## [1.3.0] - 2026-11-04 ### Added diff --git a/README.md b/README.md index 5173211..2f34c22 100644 --- a/README.md +++ b/README.md @@ -286,7 +286,7 @@ See [.env.example](.env.example) for the full list of environment variables. |---|---| | **Database** | `POSTGRES_PASSWORD`, `DESTINATION__POSTGRES__CREDENTIALS__*` | | **Meta Ads** | `META_ACCESS_TOKEN_{CLIENT}` | -| **Google Ads** | `GOOGLE_ADS_DEVELOPER_TOKEN`, `GOOGLE_ADS_ACCESS_TOKEN_{CLIENT}` | +| **Google Suite (Ads, GA4, GTM, YouTube)** | `GOOGLE_OAUTH_CLIENT_ID`, `GOOGLE_OAUTH_CLIENT_SECRET`, `GOOGLE_OAUTH_REFRESH_TOKEN` (or per-client `_CLIENT`) | | **Facebook** | `FACEBOOK_ACCESS_TOKEN_{CLIENT}` | | **Instagram** | `INSTAGRAM_ACCESS_TOKEN_{CLIENT}` | | **Pipeline** | `CLIENTS_DIR`, `TELEGRAM_BOT_TOKEN`, `TELEGRAM_CHAT_ID` | @@ -318,6 +318,14 @@ All tests are mock-based - no live API calls. Each connector has tests for succe make auth-meta # Or for a specific client: make auth-meta CLIENT=acme + +# Interactive Google OAuth Renewal Wizard (GA4, GTM, Google Ads, YouTube) +# Generates durable Refresh Tokens and configures global agency or per-client credentials +make auth-google +# Or for a specific client: +make auth-google CLIENT=acme +# Or restrict to specific services (default: all): +make auth-google SERVICES=ga4,gtm ``` --- From 56c251d33ef7db32be9709d63234dd29cd8e84fc Mon Sep 17 00:00:00 2001 From: Miguel Lozano Date: Thu, 5 Nov 2026 01:06:08 -0500 Subject: [PATCH 6/6] test: enhance Google Ads and GTM tests with token manager auto-refresh logic --- tests/test_run_google.py | 97 +++++++++++++++++++++++----------------- tests/test_run_gtm.py | 60 ++++++++++++++++++++++--- 2 files changed, 112 insertions(+), 45 deletions(-) diff --git a/tests/test_run_google.py b/tests/test_run_google.py index 13b8a7b..3856829 100644 --- a/tests/test_run_google.py +++ b/tests/test_run_google.py @@ -3,6 +3,8 @@ import pytest +from agency_analytics.google_auth import GoogleTokenManager + GOOGLE_AD_ROW = { "adGroupAd": { "ad": {"id": "4928103746", "name": "Google Ad Test"}, @@ -62,60 +64,68 @@ def _mock_error_response(code, message, **extra): return resp -def _mock_429_response(retry_delay="5s"): - resp = MagicMock() - resp.json.return_value = { - "error": { - "code": 429, - "message": "Rate limit exceeded", - "details": [{"retryDelay": retry_delay}], - } - } - return resp +def _mock_429_response(retry_delay="30s"): + return _mock_error_response(429, "Rate limited", details=[{"retryDelay": retry_delay}]) + + +@pytest.fixture +def mock_google_api(): + with patch("dlt.sources.helpers.requests.post") as mock_post: + yield mock_post + +@pytest.fixture +def mock_sleep(): + with patch("time.sleep") as mock: + yield mock -class TestGoogleAds: - def test_ads_success(self, mock_google_api): + +class TestGoogleAdsConnector: + def test_get_ads_mapping(self, mock_google_api): mock_google_api.return_value = _mock_search_response([GOOGLE_AD_ROW]) from run_google import get_ads - results = list(get_ads("123-456-7890", "dev_token", "oauth_token")) + results = list(get_ads("123-456-7890", access_token="oauth_token")) assert len(results) == 1 r = results[0] assert r["ad_id"] == "4928103746" + assert r["ad_name"] == "Google Ad Test" + assert r["ad_status"] == "ENABLED" assert r["ad_group_id"] == "8573629104" assert r["campaign_id"] == "1298475620" - assert r["ad_name"] == "Google Ad Test" - assert r["status"] == "ENABLED" - assert r["spend_usd"] == 2.50 + assert r["spend"] == 2.5 assert r["impressions"] == 45000 assert r["clicks"] == 2250 assert r["ctr"] == 5.0 - assert r["average_cpc"] == 0.001111 + assert r["cpc"] == 0.0 assert r["conversions"] == 15.5 - assert r["cost_per_conversion"] == 0.16129 + assert r["cost_per_conversion"] == 0.16 assert r["date"] == "2024-06-15" - def test_campaigns_success(self, mock_google_api): + # Verify no developer-token header is sent (sunset September 2026) + call_headers = mock_google_api.call_args[1]["headers"] + assert "developer-token" not in call_headers + assert call_headers["Authorization"] == "Bearer oauth_token" + + def test_get_campaigns_mapping(self, mock_google_api): mock_google_api.return_value = _mock_search_response([GOOGLE_CAMPAIGN_ROW]) from run_google import get_campaigns - results = list(get_campaigns("123-456-7890", "dev_token", "oauth_token")) + results = list(get_campaigns("123-456-7890", access_token="oauth_token")) assert len(results) == 1 r = results[0] assert r["campaign_id"] == "1298475620" assert r["campaign_name"] == "Google Campaign Test" assert r["status"] == "ENABLED" - assert r["advertising_channel_type"] == "SEARCH" - assert r["budget"] == 50.00 - assert r["budget_type"] == "DAILY" - assert r["spend_usd"] == 2.50 + assert r["channel_type"] == "SEARCH" + assert r["daily_budget"] == 50.0 + assert r["spend"] == 2.5 assert r["impressions"] == 45000 assert r["clicks"] == 2250 assert r["ctr"] == 5.0 - assert r["average_cpc"] == 0.001111 + assert r["cpc"] == 0.0 assert r["conversions"] == 15.5 - assert r["cost_per_conversion"] == 0.16129 + assert r["cost_per_conversion"] == 0.16 assert r["start_date"] == "2024-06-01" assert r["end_date"] == "2024-06-30" assert r["date"] == "2024-06-01" @@ -126,7 +136,7 @@ def test_rate_limit_retry(self, mock_google_api, mock_sleep): mock_google_api.side_effect = [error, error, error, error, success] from run_google import get_ads - results = list(get_ads("123-456-7890", "dev_token", "oauth_token")) + results = list(get_ads("123-456-7890", access_token="oauth_token")) assert len(results) == 1 assert mock_google_api.call_count == 5 @@ -135,7 +145,25 @@ def test_token_expired(self, mock_google_api): from run_google import get_ads with pytest.raises(Exception, match="Token expired"): - list(get_ads("123-456-7890", "dev_token", "oauth_token")) + list(get_ads("123-456-7890", access_token="oauth_token")) + + def test_token_manager_auto_refresh_on_401(self, mock_google_api): + """When token_manager is passed and a 401 is encountered, it refreshes and retries.""" + manager = MagicMock(spec=GoogleTokenManager) + manager.get_token.side_effect = ["expired_token", "fresh_token"] + + err401 = _mock_error_response(401, "Unauthorized") + success = _mock_search_response([GOOGLE_AD_ROW]) + mock_google_api.side_effect = [err401, success] + + from run_google import get_ads + + results = list(get_ads("123-456-7890", token_manager=manager)) + assert len(results) == 1 + assert mock_google_api.call_count == 2 + # Second call should use the freshly refreshed token + second_headers = mock_google_api.call_args_list[1][1]["headers"] + assert second_headers["Authorization"] == "Bearer fresh_token" def test_pagination(self, mock_google_api): row1 = copy.deepcopy(GOOGLE_AD_ROW) @@ -147,7 +175,7 @@ def test_pagination(self, mock_google_api): mock_google_api.side_effect = [page1, page2] from run_google import get_ads - results = list(get_ads("123-456-7890", "dev_token", "oauth_token")) + results = list(get_ads("123-456-7890", access_token="oauth_token")) assert len(results) == 2 assert results[0]["ad_id"] == "1" assert results[1]["ad_id"] == "2" @@ -160,14 +188,3 @@ def test_client_not_found(self): with pytest.raises(SystemExit) as exc: main() assert exc.value.code == 1 - - def test_developer_token_error(self, mock_google_api): - mock_google_api.return_value = _mock_error_response( - 403, - "Access denied", - details=[{"errors": [{"reason": "developer-token-does-not-belong-to-this-account"}]}], - ) - from run_google import get_ads - - with pytest.raises(Exception, match="Invalid developer token"): - list(get_ads("123-456-7890", "dev_token", "oauth_token")) diff --git a/tests/test_run_gtm.py b/tests/test_run_gtm.py index 842ddbd..c7e73df 100644 --- a/tests/test_run_gtm.py +++ b/tests/test_run_gtm.py @@ -29,6 +29,15 @@ ] } +SAMPLE_WORKSPACES_RESPONSE = { + "workspace": [ + { + "workspaceId": "w1", + "name": "Default Workspace", + } + ] +} + SAMPLE_TAGS_RESPONSE = { "tag": [ { @@ -115,6 +124,27 @@ def test_token_expired(self, mock_get): with pytest.raises(Exception, match="Token expired"): list(get_containers("accounts/123456789", "bad-token")) + @patch("run_gtm.requests.get") + def test_token_manager_refresh_on_401(self, mock_get): + err_resp = MagicMock() + err_resp.status_code = 200 + err_resp.json.return_value = {"error": {"code": 401, "message": "Token expired"}} + + ok_resp = MagicMock() + ok_resp.status_code = 200 + ok_resp.json.return_value = SAMPLE_CONTAINERS_RESPONSE + + mock_get.side_effect = [err_resp, ok_resp] + + mock_manager = MagicMock() + mock_manager.get_token.side_effect = ["expired_tok", "new_tok"] + + rows = list(get_containers("accounts/123456789", token_manager=mock_manager)) + assert len(rows) == 2 + assert mock_get.call_count == 2 + second_call_headers = mock_get.call_args_list[1][1]["headers"] + assert second_call_headers["Authorization"] == "Bearer new_tok" + class TestTags: @patch("run_gtm.requests.get") @@ -123,11 +153,15 @@ def test_tags_success(self, mock_get): containers_resp.status_code = 200 containers_resp.json.return_value = SAMPLE_CONTAINERS_RESPONSE + ws_resp = MagicMock() + ws_resp.status_code = 200 + ws_resp.json.return_value = SAMPLE_WORKSPACES_RESPONSE + tags_resp = MagicMock() tags_resp.status_code = 200 tags_resp.json.return_value = SAMPLE_TAGS_RESPONSE - mock_get.side_effect = [containers_resp, tags_resp, tags_resp] + mock_get.side_effect = [containers_resp, ws_resp, tags_resp, ws_resp, tags_resp] rows = list(get_tags("accounts/123456789", "token")) assert len(rows) == 2 @@ -147,6 +181,10 @@ def test_multiple_containers(self, mock_get): containers_resp.status_code = 200 containers_resp.json.return_value = SAMPLE_CONTAINERS_RESPONSE + ws_resp = MagicMock() + ws_resp.status_code = 200 + ws_resp.json.return_value = SAMPLE_WORKSPACES_RESPONSE + tags_resp_1 = MagicMock() tags_resp_1.status_code = 200 tags_resp_1.json.return_value = { @@ -177,7 +215,7 @@ def test_multiple_containers(self, mock_get): ] } - mock_get.side_effect = [containers_resp, tags_resp_1, tags_resp_2] + mock_get.side_effect = [containers_resp, ws_resp, tags_resp_1, ws_resp, tags_resp_2] rows = list(get_tags("accounts/123456789", "token")) assert len(rows) == 2 @@ -194,11 +232,15 @@ def test_rate_limit_on_containers(self, mock_get): containers_resp.status_code = 200 containers_resp.json.return_value = SAMPLE_CONTAINERS_RESPONSE + ws_resp = MagicMock() + ws_resp.status_code = 200 + ws_resp.json.return_value = SAMPLE_WORKSPACES_RESPONSE + tags_resp = MagicMock() tags_resp.status_code = 200 tags_resp.json.return_value = SAMPLE_TAGS_RESPONSE - mock_get.side_effect = [rate_resp, containers_resp, tags_resp, tags_resp] + mock_get.side_effect = [rate_resp, containers_resp, ws_resp, tags_resp, ws_resp, tags_resp] rows = list(get_tags("accounts/123456789", "token")) assert len(rows) == 2 @@ -211,11 +253,15 @@ def test_triggers_success(self, mock_get): containers_resp.status_code = 200 containers_resp.json.return_value = SAMPLE_CONTAINERS_RESPONSE + ws_resp = MagicMock() + ws_resp.status_code = 200 + ws_resp.json.return_value = SAMPLE_WORKSPACES_RESPONSE + triggers_resp = MagicMock() triggers_resp.status_code = 200 triggers_resp.json.return_value = SAMPLE_TRIGGERS_RESPONSE - mock_get.side_effect = [containers_resp, triggers_resp, triggers_resp] + mock_get.side_effect = [containers_resp, ws_resp, triggers_resp, ws_resp, triggers_resp] rows = list(get_triggers("accounts/123456789", "token")) assert len(rows) == 2 @@ -236,11 +282,15 @@ def test_empty_triggers(self, mock_get): containers_resp.status_code = 200 containers_resp.json.return_value = SAMPLE_CONTAINERS_RESPONSE + ws_resp = MagicMock() + ws_resp.status_code = 200 + ws_resp.json.return_value = SAMPLE_WORKSPACES_RESPONSE + triggers_resp = MagicMock() triggers_resp.status_code = 200 triggers_resp.json.return_value = {"trigger": []} - mock_get.side_effect = [containers_resp, triggers_resp, triggers_resp] + mock_get.side_effect = [containers_resp, ws_resp, triggers_resp, ws_resp, triggers_resp] rows = list(get_triggers("accounts/123456789", "token")) assert len(rows) == 0