Earth Engine initialization error: authentication token not getting detected from Streamlit app's Secrets

See original GitHub issue

First off, thank you so much for this excellent free and open resource! Your efforts here are much appreciated! I am currently looking to build a simple Earth Engine app and am using this repo to learn how to do so.

The issue

I am getting a “Earth Engine client library not initialized. Run ee.Initialize()” error on my web app.

Although I have generated and saved the Earth Engine authentication token in the app’s “Secrets”, as you have described in your blog post, the app seems to not pick it up from there.

How do I fix this?

What I have done

I began with trying to replicate this multi-app (https://streamlit.geemap.org/) on my Streamlit account, so I forked this repo and published it as-is on my Streamlit.

Error as seen on the web app

But when I select an app working with Earth Engine (eg. Google Earth Engine (GEE), Awesome GEE Community Datasets), I get the following error.

ee.ee_exception.EEException: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you're on Streamlit Cloud, click on 'Manage app' in the lower right of your app).
Traceback:

File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/scriptrunner/script_runner.py", line 475, in _run_script
    exec(code, module.__dict__)
File "/app/streamlit-geospatial/app.py", line 47, in <module>
    apps.run()
File "/app/streamlit-geospatial/multiapp.py", line 64, in run
    functions[titles.index(title)]()
File "/app/streamlit-geospatial/apps/gee.py", line 121, in app
    nlcd()
File "/app/streamlit-geospatial/apps/gee.py", line 37, in nlcd
    Map.addLayer(getNLCD(year), {}, "NLCD " + year)
File "/app/streamlit-geospatial/apps/gee.py", line 22, in getNLCD
    dataset = ee.ImageCollection("USGS/NLCD_RELEASES/2016_REL")
File "/home/appuser/venv/lib/python3.7/site-packages/ee/computedobject.py", line 32, in __call__
    return type.__call__(cls, *args, **kwargs)
File "/home/appuser/venv/lib/python3.7/site-packages/ee/imagecollection.py", line 38, in __init__
    self.initialize()
File "/home/appuser/venv/lib/python3.7/site-packages/ee/imagecollection.py", line 72, in initialize
    super(ImageCollection, cls).initialize()
File "/home/appuser/venv/lib/python3.7/site-packages/ee/collection.py", line 32, in initialize
    apifunction.ApiFunction.importApi(cls, 'Collection', 'Collection')
File "/home/appuser/venv/lib/python3.7/site-packages/ee/apifunction.py", line 183, in importApi
    cls.initialize()
File "/home/appuser/venv/lib/python3.7/site-packages/ee/apifunction.py", line 154, in initialize
    signatures = data.getAlgorithms()
File "/home/appuser/venv/lib/python3.7/site-packages/ee/data.py", line 1055, in getAlgorithms
    call = _get_cloud_api_resource().projects().algorithms().list(
File "/home/appuser/venv/lib/python3.7/site-packages/ee/data.py", line 285, in _get_cloud_api_resource
    'Earth Engine client library not initialized. Run `ee.Initialize()`')

Error as seen in “Manage App” log, from inside my Streamlit account

The most recent log in “Manage App” ends with the following—I’m guessing the app is not finding the authentication token eventhough I have generated and save it in the app’s “Secrets”.

(I have purposely truncated the authentication web link below.)

Installing collected packages: click
  Attempting uninstall: click
    Found existing installation: click 8.0.4
    Uninstalling click-8.0.4:
      Successfully uninstalled click-8.0.4
Successfully installed click-8.0.0
WARNING: You are using pip version 22.0.3; however, version 22.1 is available.
You should consider upgrading via the '/home/appuser/venv/bin/python -m pip install --upgrade pip' command.
[manager] Python dependencies were installed from /app/streamlit-geospatial/requirements.txt using pip.
Check if streamlit is installed
Streamlit is already installed
[manager] Processed dependencies!
To authorize access needed by Earth Engine, open the following URL in a web browser and follow the instructions. If the web browser does not start automatically, please manually browse the URL below.
    https://code.earthengine.google.com/client-auth?scopes=https%3A//www.googleapis.com/auth/earthengine%20https%3A//www.googleapis.com/auth/ ...
The authorization workflow will generate a code, which you should paste in the box below. 
2022-05-19 10:54:04.502 Uncaught app exception
Traceback (most recent call last):
  File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/scriptrunner/script_runner.py", line 475, in _run_script
    exec(code, module.__dict__)
  File "/app/streamlit-geospatial/app.py", line 47, in <module>
    apps.run()
  File "/app/streamlit-geospatial/multiapp.py", line 64, in run
    functions[titles.index(title)]()
  File "/app/streamlit-geospatial/apps/gee_datasets.py", line 182, in app
    eval(datasets[category][dataset])
  File "<string>", line 1, in <module>
  File "/app/streamlit-geospatial/apps/gee_datasets.py", line 75, in global_mangrove_watch
    gmw2007 = ee.FeatureCollection("projects/sat-io/open-datasets/GMW/GMW_2007_v2")
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/computedobject.py", line 32, in __call__
    return type.__call__(cls, *args, **kwargs)
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/featurecollection.py", line 44, in __init__
    self.initialize()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/featurecollection.py", line 91, in initialize
    super(FeatureCollection, cls).initialize()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/collection.py", line 32, in initialize
    apifunction.ApiFunction.importApi(cls, 'Collection', 'Collection')
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/apifunction.py", line 183, in importApi
    cls.initialize()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/apifunction.py", line 154, in initialize
    signatures = data.getAlgorithms()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/data.py", line 1055, in getAlgorithms
    call = _get_cloud_api_resource().projects().algorithms().list(
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/data.py", line 285, in _get_cloud_api_resource
    'Earth Engine client library not initialized. Run `ee.Initialize()`')
ee.ee_exception.EEException: Earth Engine client library not initialized. Run `ee.Initialize()`
2022-05-19 10:54:05.079 Uncaught app exception
Traceback (most recent call last):
  File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/scriptrunner/script_runner.py", line 475, in _run_script
    exec(code, module.__dict__)
  File "/app/streamlit-geospatial/app.py", line 47, in <module>
    apps.run()
  File "/app/streamlit-geospatial/multiapp.py", line 64, in run
    functions[titles.index(title)]()
  File "/app/streamlit-geospatial/apps/gee_datasets.py", line 182, in app
    eval(datasets[category][dataset])
  File "<string>", line 1, in <module>
  File "/app/streamlit-geospatial/apps/gee_datasets.py", line 75, in global_mangrove_watch
    gmw2007 = ee.FeatureCollection("projects/sat-io/open-datasets/GMW/GMW_2007_v2")
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/computedobject.py", line 32, in __call__
    return type.__call__(cls, *args, **kwargs)
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/featurecollection.py", line 44, in __init__
    self.initialize()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/featurecollection.py", line 91, in initialize
    super(FeatureCollection, cls).initialize()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/collection.py", line 32, in initialize
    apifunction.ApiFunction.importApi(cls, 'Collection', 'Collection')
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/apifunction.py", line 183, in importApi
    cls.initialize()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/apifunction.py", line 154, in initialize
    signatures = data.getAlgorithms()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/data.py", line 1055, in getAlgorithms
    call = _get_cloud_api_resource().projects().algorithms().list(
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/data.py", line 285, in _get_cloud_api_resource
    'Earth Engine client library not initialized. Run `ee.Initialize()`')
ee.ee_exception.EEException: Earth Engine client library not initialized. Run `ee.Initialize()`

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
giswqscommented, May 20, 2022

Yes, you need to use the token from .config/earthengine/credentials. Do NOT use the token you copy from the browser. It will not work.

Also, use geemap.ee_initialize() or geemap.Map() to initialize Earth Engine. Never use ee.Initialize() with a streamlit web app. It can’t detect the secret and the initialization will fail.

As long as the web app is working properly, you can ignore the warning message.

0reactions
pskoulgicommented, May 20, 2022

I tried it locally, and it worked (screenshot of the local URL on a browser tab below).

[I think I got the app to work on the cloud—by using the token stored on my local machine instead of the one generated by following the link the build log on Streamlit takes me to. But, strangely, “client not initialized” error in the build log persists. More details below.]

image

Before trying it locally, I got the Earth Engine token by following the authorization link that came up in the “Manage app” build log for the app in my Streamlit. This is in the log I pasted in my message above, see screenshot below where I have highlighted it.

It did not give me the box to paste the token into, like it said it would. I went ahead and saved the token into the app’s “Secrets”. I thought it’ll get picked up on rebooting the app, but that never happened, I get shown a (new) authorization link each time in the build log and I get the same errors.

githubss

On seeing that the app runs fine locally, I also took the token in my local machine (in my .config/earthengine/credentials) and saved that into my “Secrets” on Streamlit (they were not the same). With this, the app has started working.

But, strangely, the build log in “Manage app” on Streamlit hasn’t stopped saying “Earth Engine client library not initialized. Run ee.Initialize()

With the app now started to work, perhaps I can ignore this message and move on—or would that be inadvisible, any suggestions?

The last few lines of the log copied over, are as follows:

WARNING: You are using pip version 22.0.3; however, version 22.1 is available.
You should consider upgrading via the '/home/appuser/venv/bin/python -m pip install --upgrade pip' command.
[manager] Python dependencies were installed from /app/streamlit-geospatial/requirements.txt using pip.
Check if streamlit is installed
Streamlit is already installed
[manager] Processed dependencies!
2022-05-20 08:40:56.344 Uncaught app exception
Traceback (most recent call last):
  File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/scriptrunner/script_runner.py", line 475, in _run_script
    exec(code, module.__dict__)
  File "/app/streamlit-geospatial/app.py", line 47, in <module>
    apps.run()
  File "/app/streamlit-geospatial/multiapp.py", line 64, in run
    functions[titles.index(title)]()
  File "/app/streamlit-geospatial/apps/gee_datasets.py", line 182, in app
    eval(datasets[category][dataset])
  File "<string>", line 1, in <module>
  File "/app/streamlit-geospatial/apps/gee_datasets.py", line 75, in global_mangrove_watch
    gmw2007 = ee.FeatureCollection("projects/sat-io/open-datasets/GMW/GMW_2007_v2")
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/computedobject.py", line 32, in __call__
    return type.__call__(cls, *args, **kwargs)
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/featurecollection.py", line 44, in __init__
    self.initialize()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/featurecollection.py", line 91, in initialize
    super(FeatureCollection, cls).initialize()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/collection.py", line 32, in initialize
    apifunction.ApiFunction.importApi(cls, 'Collection', 'Collection')
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/apifunction.py", line 183, in importApi
    cls.initialize()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/apifunction.py", line 154, in initialize
    signatures = data.getAlgorithms()
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/data.py", line 1055, in getAlgorithms
    call = _get_cloud_api_resource().projects().algorithms().list(
  File "/home/appuser/venv/lib/python3.7/site-packages/ee/data.py", line 285, in _get_cloud_api_resource
    'Earth Engine client library not initialized. Run `ee.Initialize()`')
ee.ee_exception.EEException: Earth Engine client library not initialized. Run `ee.Initialize()`
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Not signed up for Earth Engine -------from the server ...
G:\Paper\npm\earthengine-api\demos\server-auth-nodejs>node server Authenticating Earth Engine API using private key... Authentication successful. Error: Not ...
Read more >
Google Earth Engine Authentication problems
I have installed Idle Shell in Order to authenticate GEE, however with no success. I'm having trouble finding online any kind of tutorial,...
Read more >
Using Google Earth Engine in a Streamlit app
Connect your app to Streamlit and set up secrets in the app settings. At this step, your Key is not publicly exposed, and...
Read more >
Google Earth Engine Initialization Woes
Since your main error is Permission denied , it means that there is an issue with the authentication of your service account.
Read more >
How to Fix : “ImportError: Cannot import name X” in Python
In Python "ImportError: cannot import name" error generally occurs when the imported class is not accessible, or the imported class is in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found