{"cells":[{"metadata":{"colab_type":"text","id":"ZqQ0WNGv608i"},"cell_type":"markdown","source":"# **The flow to get the secret and token working is clear as mud\n\nAlways bad when the directions are incomplete - after an afternoon of clicking I think we have nailed down all the steps to get access.  To avoid sharing my personal information there will be some error looking cells right now in the kernel - they should all go away when you fork and step thru the script.\n\nDO NOT COMMIT this kernel to get things working - step thru it - the intent is to teach you how to get your token and generate the secret.\n\nI have taken the discussion post by Eric to generate this kernel following the steps described by Paul.\n\n[http://https://www.kaggle.com/c/ds4g-environmental-insights-explorer/discussion/129997]\n\nWhen it works and you have your refresh- token than you can commit to save the kernel for reference later.\n"},{"metadata":{},"cell_type":"markdown","source":"Update:  Added section at the end to shown the steps to get Earth Engine running on your local Ubuntu machine."},{"metadata":{},"cell_type":"markdown","source":"Step Zero - sign up for access to Google Earth Engine - COPY the link below into a new browser tab.  In top right there should be a spot to sign up.\n\nUse the same gmail login for this as you will use below.  You should quickly get a email \"Welcome to Google Earth Engine\n\nhttps://earthengine.google.com/"},{"metadata":{"colab_type":"text","id":"6DZldYs2608k"},"cell_type":"markdown","source":"Step 1: Import libraries"},{"metadata":{"_kg_hide-input":true,"colab":{},"colab_type":"code","id":"weDvKvFh608l","trusted":false},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport rasterio as rio\nimport folium\n\nimport ee\nfrom kaggle_secrets import UserSecretsClient\nfrom google.oauth2.credentials import Credentials\n\n","execution_count":null,"outputs":[]},{"metadata":{"colab_type":"text","id":"-13q1iOg608z"},"cell_type":"markdown","source":"Step 2: Get a token\n\nThe code step below will generate a message with a link.\n\nCOPY the link and paste it into a new browser tab (YES - leave this kernel and start a new tab) and enter to run the link.\n\nOnce the link is pasted into the new page it should walk you thru a couple of dialog pages gathering your google user name and getting your permissions (ALLOW).\n\nAt the end of those steps a \"token\" is generated.  Copy that token.   Come back to this kernel.\n\nPASTE the token in the box that was generated when you ran the next cell. \n\nYou should get a message of success."},{"metadata":{"trusted":false},"cell_type":"code","source":"ee.Authenticate() ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The code generated in the above steps is not the final token - run the next cell.  This will generate the value for the secret."},{"metadata":{"trusted":false},"cell_type":"code","source":"!cat ~/.config/earthengine/credentials","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"NOW THE SECRET CODE STUFF NEEDS TO TAKE PLACE\n\nClick on Add-ons\n\nClick on Secrets\n\nAdd a new secret.  Pick anything you want for the label - this label will be visible in all the kernels you use so don't get cheeky.\n\nIn the VALUE box paste your refresh_token from above.  (only the value within the quotation marks)\n\n> Copy the code snippet into the empty cell below.  Than run this next cell.\n\n "},{"metadata":{},"cell_type":"markdown","source":"FOR KERNELS ONCE YOU HAVE COMPLETED THIS FULL SCRIPT WITH SUCCESS - ANY FUTURE\nKERNELS ONLY NEED THE FOLLOWING STEPS AND SHOULD NOT NEED TO REPEAT ANY OF THE ABOVE CELLS."},{"metadata":{"trusted":false},"cell_type":"code","source":"from kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nsecret_value_0 = user_secrets.get_secret(\"jimmy\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"from google.oauth2.credentials import Credentials\nimport ee\nimport folium\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"user_secret = \"jimmy\" # Your user secret, defined in the add-on menu of the notebook editor\nrefresh_token = UserSecretsClient().get_secret(user_secret)\nrefresh_token\ncredentials = Credentials(\n        None,\n        refresh_token=refresh_token,\n        token_uri=ee.oauth.TOKEN_URI,\n        client_id=ee.oauth.CLIENT_ID,\n        client_secret=ee.oauth.CLIENT_SECRET,\n        scopes=ee.oauth.SCOPES)\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"ee.Initialize(credentials=credentials)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Access to Google Earth Engine on a Ubuntu 19.10 local PC.\n\nSteps are very similiar but some changes.\n1.  You still need to be signed up for access per step 0 above.\n"},{"metadata":{},"cell_type":"markdown","source":"2.  Pip to install the need libaries used in the example code in the github\n\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"# pip install rasterio\n# pip install folium\n# pip install google-auth >=1.11.0\n# pip install earthengine-api","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"On a one time basis you need to authenticate the local machine - much like the above it will step you through a couple of dialogs to get your gmail login info and for you to allow it access.\n\nFrom terminal - run"},{"metadata":{"trusted":true},"cell_type":"code","source":"earthengine authenticate","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"# Import, authenticate and initialize the Earth Engine library.\nimport ee\n# ee.Authenticate()   # only need this the first time its run on a PC\nee.Initialize()","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"how-to-get-started-with-the-earth-engine-data.ipynb","provenance":[]},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.6"}},"nbformat":4,"nbformat_minor":1}