Setting up server code

To get started, make sure you are working on the latest code or get a copy from https://github.com/PacktPublishing/Getting-Started-with-Google-Cloud-AI-Services and then install the required dependencies by running:

$ npm install
or
$ yarn install

Now that we have our code base ready, we will continue. Open your .env file placed at the root of the SmartExchange code, and add the GCP Service Key that we created earlier, as shown here:

// SNIPP SNIPP
GCP_SK_CREDENTIALS={"type": "service_account", "project_id": "smart-exchange-b10882", "private_key_id": "29e9aed0a8aa92c9cc9fcdfa3bd28f7999108c69", "private_key": "-----BEGIN PRIVATE KEY-----nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCvedVDQQFIM/Q3n1a9LcurR1eC9QBGGZzpijB0mHlLhkCZ7V4T16VwkQyoVs3dn67eK/ov4+i4AzW9inOqC8qbt2jJtXYoPiq+0RP7B+kF8RGNSF41xilvha7dekXAOk7yd1lY8j141DAi1Cn/lwIDDVPWjXzXHeNtP+0J0aJ+wASOqDeVHbbPdQnneHB76jJZjSO88oHOlQ1UK8HnDR08YImyaLLAGYv0g4qkZYzgIwRBXE13IctS+fD7JtQrHImn9sxSKqrUp0VZx0RDn4+4LrYWo4BYOWTcgSQjNuzmjPWeAol8DeS5roYlJaggOi7gwJa7SBcTbUFV9Hqz2nokJryj2PAgMBAAECggEAG13n6cZAme8oXnDgFudEQheWJk3QreJ5k5cAkMpX6L/fnkIr0vJl5elpTPPExZS65AF90mw8Bj7R6c5prKSFbkECblYRSx05C+dayAvsnKHZzn5rGv3dxnvV6srACBvP5jbA44NcHPUS0MvA8JcWPeCJMsKNk5+ArLemBJuwk/WJg0nCKiwamDtUV3EJQtd69CJXGqo07jDODaoXaStsseWhK+vbKN9IeU0msupuW9qIlxznXlSMc2iuk3FSQGGqSqfTK0WxJawhu4d59ftYKujhvlPN6rqcAOEQg/BcRVOm+S5vn93pX2tJvJhC5yqSTiecSfQL9ndjye55tunDfGSUnUQKBgQDb71zxruFngzmtD4FTn81/lOhnoiS4tZ1MU8P1jn9etBqj58OKTst07z8aylcPEu+SbX0Kg/xLneGfK2wQcn7D+ZPOI11U7BTasmUcNSxGWGN3fnPY4QNROQisTxeyKfG8ptj5q73nFj35YX0a6/nip/akejOx7SWEdX3ImQL888BmQKBgQDMQB/X2ZlR6QRD2tfaYRijttUXQfI63hLTnqRVG3LJOcz0tSt8P90DOnhzZNgfllciAm14oDLEpnf/W2gdGrfy7olK9mRldDvW7nxvbzVeHRIHfUr15Oh7KRKF2kthHoqG8FEO13joIfvYlNs7MQYO4jS4W3A31IbyS1ntQUktdcBZwKBgEXWCcNMbpJ53rSPOde4bfjRrCxkct9D8eOyaiNaPBfbB4jl7mxLn3WgCn+tRFuq/ZuXXJ8cLd6s8VmjZhsLQWZmE9ad2Zh2HVhNC3yzG5B3sOwyYRSXsnVJwPFF5BZUr0fiCv+sgw5/x4oKZLlJuJnxy+PBh83WDRpQlmSDJrZo3xAoGBAKh0nHo8kTPosRzM7c5kNSYgEi5zB6+i3LSnaIs0tyfU/v+3x+SSu1IlUCBDxKfQk45eGnDFLVnOvo/o8RhLy7VFzgFIOAmFWSuAKlpxir9TFIKOyFsi7sZc6oTwtAimarO90enx+s54oIRDQPDuGRCvUTozaBSRWqPF+SxJkvPmULzAoGADdIjNfkScWwGuOYx7iAynksvWY2vLlzh0enFpTWJt6mBRSN5kabWxP3iotwnlf4TmNIsSmRobn581DWs+syiun7fijYqEPhCc4gAA51sxF61i55CdpbcXQVzxoEReVFChMGkLDBNbdJb+tas84c+pLn6igyeYEObc15xeh8mccXZww=n-----END PRIVATE KEY-----n", "client_email": "smart-exchange-service-account@smart-exchange-b10882.iam.gserviceaccount.com", "client_id": "112231488660260011184", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/smart-exchange-service-account%40smart-exchange-b10882.iam.gserviceaccount.com"}

Earlier in this chapter, we created a service account key, which we downloaded as JSON and set up in our environment variables. To keep thing simple, I am not going to read the service account key from the environment variables of my machine, but we are going to add them explicitly for this project using the .env file. This way, I can have multiple projects on my machine using different Google Project's service account keys.

In the previous snippet, we created a new environment variable named GCP_SK_CREDENTIALS and assigned the JSON as one long string, without any line breaks. To convert a JSON string to a single-line string, you can use an online resource such as http://removelinebreaks.net/. If you copy/paste the service account key as is, the application will crash.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.26.138