{"id":2608,"date":"2023-01-22T13:24:42","date_gmt":"2023-01-22T11:24:42","guid":{"rendered":"https:\/\/www.h-hennes.fr\/blog\/?p=2608"},"modified":"2023-01-22T13:24:44","modified_gmt":"2023-01-22T11:24:44","slug":"utilisation-dune-google-cloud-function-avec-authorisation-en-php","status":"publish","type":"post","link":"https:\/\/www.h-hennes.fr\/blog\/2023\/01\/22\/utilisation-dune-google-cloud-function-avec-authorisation-en-php\/","title":{"rendered":"Utilisation d&rsquo;une google cloud function avec authorisation en php"},"content":{"rendered":"\n<p>Petit tutoriel qui sors un peu de mes sujets habituels mais qui reste dans le cadre du d\u00e9veloppement php.<br \/>Nous allons voir comment appeler une google cloud fonction, <strong>qui n\u00e9cessite une authentification<\/strong>.<\/p>\n<p>La difficult\u00e9 pour moi a vraiment \u00e9t\u00e9 l&rsquo;authentification et c&rsquo;est un sujet qui m&rsquo;a fait perdre un temps fou car la documentation de Google n&rsquo;\u00e9tait absolument pas <br \/>claire sur le sujet.<br \/>L\u2019authentification est r\u00e9alis\u00e9e via un fichier json reprenant les informations d&rsquo;un compte de service. ( Cf. exemple ci-dessous )<br \/><br \/>Comme vous pouvez le juger sur la page suivante :\u00a0 <a href=\"https:\/\/cloud.google.com\/functions\/docs\/securing\/authenticating\" target=\"_blank\" rel=\"nofollow noopener\">https:\/\/cloud.google.com\/functions\/docs\/securing\/authenticating\u00a0<\/a><\/p>\n<p>Des exemples sont donn\u00e9s en NodeJs, Go , Python et Java, mais en Php nous sommes invit\u00e9s \u00e0 utiliser des biblioth\u00e8ques tiers pour g\u00e9n\u00e9rer des jetons d&rsquo;authentification, sans explications d\u00e9taill\u00e9es.<\/p>\n<p>Voici un exemple de contenu du fichier d&rsquo;authentification json<\/p>\n\n\n\n<pre lang=\"json\">\n{\n  \"type\": \"service_account\",\n  \"project_id\": \"PROJECT-ID\",\n  \"private_key_id\": \"PRIVATE-KEY-ID\",\n  \"private_key\": \"PRIVATE-KEY\",\n  \"client_email\": \"customer@email.com\",\n  \"client_id\": \"CLIENT-ID\",\n  \"auth_uri\": \"https:\/\/accounts.google.com\/o\/oauth2\/auth\",\n  \"token_uri\": \"https:\/\/oauth2.googleapis.com\/token\",\n  \"auth_provider_x509_cert_url\": \"https:\/\/www.googleapis.com\/oauth2\/v1\/certs\",\n  \"client_x509_cert_url\": \"https:\/\/www.googleapis.com\/robot\/v1\/metadata\/x509\/CUSTOMER-URL\"\n}\n<\/pre>\n\n\n<p>Apr\u00e8s pas mal de recherches j&rsquo;ai finalement trouv\u00e9 que contrairement \u00e0 ce que d\u00e9crit la documentation, Google fournit la libraire Google_Auth qui fait le n\u00e9cessaire &#8230;<\/p>\n<p>Et c&rsquo;est relativement simple \u00e0 mettre en place. ( cf. la page github de l&rsquo;extension : <a href=\"https:\/\/github.com\/googleapis\/google-auth-library-php\" target=\"_blank\" rel=\"nofollow noopener\">https:\/\/github.com\/googleapis\/google-auth-library-php<\/a> )<\/p>\n<p>Voici un donc un exemple concret d&rsquo;appel \u00e0 une google cloud fonction avec l&rsquo;authentification.<br \/>Il est n\u00e9cessaire d&rsquo;utiliser composer et de charger la libraire <strong>google\/auth<\/strong><\/p>\n<p>Voici ensuite le code \u00e0 appeler ( c&rsquo;est presque le m\u00eame que l&rsquo;exemple par d\u00e9faut )<\/p>\n\n\n<pre lang=\"php\">\n\nrequire_once __DIR__.'vendor\/autoload.php';\n\nuse Google\\Auth\\ApplicationDefaultCredentials;\nuse GuzzleHttp\\Client;\nuse GuzzleHttp\\HandlerStack;\n\n\/\/ CHEMIN Vers le fichier JSON d\u00e9fini plus haut sur votre poste\nputenv('GOOGLE_APPLICATION_CREDENTIALS=\/path\/to\/my\/credentials.json');\n\n\/\/D\u00e9finition des urls \u00e0 utiliser\n$apiBaseUrl = 'https:\/\/identifiant.cloudfunctions.net'; \n$functionPath = '\/Nom_de_la_fonction';\n$targetAudience = $apiBaseUrl.$functionPath;\n\n\/\/Cr\u00e9ation du client Guzzle avec le middleware qui va g\u00e9rer l'authentification\n$middleware = ApplicationDefaultCredentials::getIdTokenMiddleware($targetAudience);\n$stack = HandlerStack::create();\n$stack->push($middleware);\n$client = new Client([\n    'handler' => $stack,\n    'auth' => 'google_auth',\n    'base_uri' => $apiBaseUrl,\n]);\n\n\/\/ Ensuite on peut envoyer la requ\u00eate, par ex du JSON\n$response = $client->post(\n    $functionPath,\n    [\n        \\GuzzleHttp\\RequestOptions::JSON => ['test' => 'data'], \/\/Encodage en json du tableau donn\u00e9\n        'debug' => true, \/\/Flag d\u00e9bug si n\u00e9cessaire\n    ]\n);\n\n\/\/Et voir le r\u00e9sulat de l'appel\nprint_r((string) $response->getBody());\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Petit tutoriel qui sors un peu de mes sujets habituels mais qui reste dans le cadre du d\u00e9veloppement php.Nous allons voir comment appeler une google cloud fonction, qui n\u00e9cessite une authentification. La difficult\u00e9 pour moi a vraiment \u00e9t\u00e9 l&rsquo;authentification et c&rsquo;est un sujet qui m&rsquo;a fait perdre un temps fou car la documentation de Google [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[6],"tags":[591,590,74],"class_list":["post-2608","post","type-post","status-publish","format-standard","hentry","category-trucs-et-astuces","tag-google-auth","tag-google-cloud-function","tag-php"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/comments?post=2608"}],"version-history":[{"count":1,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2608\/revisions"}],"predecessor-version":[{"id":2609,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2608\/revisions\/2609"}],"wp:attachment":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/media?parent=2608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/categories?post=2608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/tags?post=2608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}