4.2: Enabling Google Sheets and Obtaining a Key
- Page ID
- 433536
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)In order to connect a sensor to a Google sheet you need to enable a sheet in your Drive and then get a key that your code can use to access the sheet. The key will be in the form of a Json file.
Enable Google APIs
1. Go to the Navigation menu/APIs & Services and choose Enabled APIs & services
![clipboard_e1eac4c9ea845eee4a6d838ccc4293f99.png](https://chem.libretexts.org/@api/deki/files/435658/clipboard_e1eac4c9ea845eee4a6d838ccc4293f99.png?revision=1)
2. Click +ENABLE APIS AND SERVICES. Note the large selection of APIs and services Google offers
![clipboard_ec474aefd331f6c1d194b7bdf4026fe63.png](https://chem.libretexts.org/@api/deki/files/435659/clipboard_ec474aefd331f6c1d194b7bdf4026fe63.png?revision=1)
3. You will need to enable both Google Drive and Google Sheets APIs, and enable both. These are in the Google Workspace and you may need to click view all (31)
![clipboard_ef986b4c9b9971f54adc54af8bdba3c85.png](https://chem.libretexts.org/@api/deki/files/435660/clipboard_ef986b4c9b9971f54adc54af8bdba3c85.png?revision=1)
Obtain Key
1. Go to the Navigation menu/APIs & Services and choose Credentials
![clipboard_e45c447aa9b2bd92b2348f41abfb741db.png](https://chem.libretexts.org/@api/deki/files/435661/clipboard_e45c447aa9b2bd92b2348f41abfb741db.png?revision=1)
2. Make a new key in the form of a JSON file. To create a JSON file navigate to Credentials. You should see your newly created service account. Click on the edit icon (pencil). Then go to the Keys tab, then click "Add Key" --> "Create new key"
![clipboard_edd9f48a4882925f8fd0f01e772f510e7.png](https://chem.libretexts.org/@api/deki/files/435663/clipboard_edd9f48a4882925f8fd0f01e772f510e7.png?revision=1)
3. The key will be downloaded to your computer.
![clipboard_e81b081bcebf55769f6389aaab8b97f2f.png](https://chem.libretexts.org/@api/deki/files/435664/clipboard_e81b081bcebf55769f6389aaab8b97f2f.png?revision=1)
4. You will want to rename the key and move it to the folder with your Python program that will be using it.
![clipboard_e13e42a0edeb7cc10b91e915a8bfbab5e.png](https://chem.libretexts.org/@api/deki/files/435665/clipboard_e13e42a0edeb7cc10b91e915a8bfbab5e.png?revision=1)
Take a look at the json file. Note that it has information related to your project, your account and the authentication service.
![clipboard_ef11fc8397db6afc8d5601c4bbee3ea34.png](https://chem.libretexts.org/@api/deki/files/435666/clipboard_ef11fc8397db6afc8d5601c4bbee3ea34.png?revision=1)
Note, if you go to your credentials you will see that your Service Account for this project has the same email as is in the json file. In the next step you will create a Google Sheet and share it with this email, so that your Python code that uses the key can upload data through the Google sheet API.
![clipboard_e8f0e64fe39f27784af3f90b32bf6b2b6.png](https://chem.libretexts.org/@api/deki/files/435667/clipboard_e8f0e64fe39f27784af3f90b32bf6b2b6.png?revision=1)
Create a Google Sheet
I suggest you create a folder in your Google Drive for your class projects, that is, your school Google Drive and not the one you used to create this account with. Right click in that folder and create a new Google Sheet, in this tutorial we will call this "classTest". But you should name it as appropriate to your assignment. Once you have created the sheet you need to share it with the email associated with your service account. Be sure to give it editor access.
![clipboard_e22e78e1516344aac291b18228f83bbd3.png](https://chem.libretexts.org/@api/deki/files/435668/clipboard_e22e78e1516344aac291b18228f83bbd3.png?revision=1)
You should now be ready to write some code that will allow you to upload data to the Google Sheet in real time.