issues with ssl certificates using android (via buildozer)

283 views
Skip to first unread message

magowiz

unread,
Jan 22, 2023, 12:33:53 PM1/22/23
to Kivy users support
Hi,
I don't know if it is a bug or mine misconfiguration : I'm trying to use meteostat package to get weather  data in my app, that is built by buildozer for android. In this mobile version while getting data I get SSL verification error, I noticed that this lib uses urllib library because the excepion I get is "urllib.errors.URLError". I followed other threads about certificates issues in kivy on mobile but no suggestion seemed to fix for me.

buildozer.spec file:
[app]
title = Travel Lint
package.name = travel_lint
package.domain = net.magowiz
source.dir = .
source.include_exts = py,png,jpg,kv,atlas,md,rst,txt,json,ini,tsv
source.exclude_dirs = tests, bin, .buildozer, .pytest_cache, htmlcov, kivymd_example,
                      features, megalinter-reports
source.exclude_patterns = *.spec, .coverage*, pylintrc, *.sh, requirements.txt,
                          pytest.ini, token.json
version.regex = __version__ = ['"](.*)['"]
version.filename = main.py
requirements = libbz2, python3, kivy==2.1.0, kivymd==1.1.1, Pillow, pygments, m2rr,
               docutils==0.18.1, mistune==0.8.4, sdl2_ttf==2.0.15, httplib2, pyparsing,
               uritemplate, google-api-python-client,google, six==1.12.0,
               google-auth-httplib2, google-auth-oauthlib, google-api-core,
               google-auth, requests, urllib3, chardet, certifi, idna,
               cachetools, pyasn1, pyasn1_modules, rsa, requests_oauthlib,
               oauthlib, dropbox, stone, country_list, geopy, forex_python, simplejson,
               countryinfo, pyowm, geojson, attr, O365, stringcase, tzlocal,
               pytz_deprecation_shim, backports-zoneinfo, pytz, dateutil, beautifulsoup4,
               currency_converter_with_rate, pandas, meteostat, markdown2, cython, overpy,
               pycountry
presplash.filename = logo.png
icon.filename = logo.png
orientation = portrait
osx.python_version = 3
osx.kivy_version = 1.9.1
fullscreen = 0
android.permissions = INTERNET, STORAGE
android.api = 30
android.minapi = 30
android.ndk_api = 30
android.accept_sdk_license = True
android.archs = armeabi-v7a, x86_64, x86, arm64-v8a
p4a.fork = kivy
p4a.branch = develop
ios.kivy_ios_url = https://github.com/kivy/kivy-ios
ios.kivy_ios_branch = master
ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
ios.ios_deploy_branch = 1.7.0
[buildozer]
log_level = 2
warn_on_root = 1
build_dir = /srv/buildozer_build_dir

android logcat

01-22 18:09:44.336  2002  2032 I python  : [INFO   ] [Video       ] Provider: null(['video_ffmpeg', 'video_ffpyplayer'] ignored)
01-22 18:09:45.601  2002  2032 I python  : [INFO   ] [GL          ] NPOT texture support is available
01-22 18:09:47.381  2002  2032 I python  :  InsecureRequestWarning: Unverified HTTPS request is being made to host 'api.open-elevation.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
01-22 18:09:47.663  2002  2032 I python  : [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)
01-22 18:09:47.663  2002  2032 I python  :  Traceback (most recent call last):
01-22 18:09:47.663  2002  2032 I python  :    File "/srv/buildozer_build_dir/android/app/main.py", line 832, in <module>
01-22 18:09:47.664  2002  2032 I python  :    File "/srv/buildozer_build_dir/android/platform/build-armeabi-v7a_x86_64_x86_arm64-v8a/build/python-installs/travel_lint/x86/kivy/app.py", line 954, in run
01-22 18:09:47.664  2002  2032 I python  :    File "/srv/buildozer_build_dir/android/platform/build-armeabi-v7a_x86_64_x86_arm64-v8a/build/python-installs/travel_lint/x86/kivy/app.py", line 949, in _run_prepare
01-22 18:09:47.664  2002  2032 I python  :    File "kivy/_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
01-22 18:09:47.664  2002  2032 I python  :    File "/srv/buildozer_build_dir/android/app/main.py", line 684, in on_start
01-22 18:09:47.664  2002  2032 I python  :    File "/srv/buildozer_build_dir/android/app/ui/extra_widgets.py", line 189, in set_weather_location
01-22 18:09:47.664  2002  2032 I python  :    File "/srv/buildozer_build_dir/android/app/ui/extra_widgets.py", line 54, in set_weather_location
01-22 18:09:47.665  2002  2032 I python  :    File "/srv/buildozer_build_dir/android/app/lib/weather/weather_getter.py", line 319, in get_normals_in_dates
01-22 18:09:47.665  2002  2032 I python  :  UnboundLocalError: local variable 'data' referenced before assignment
01-22 18:09:47.665  2002  2032 I python  : Python for android ended.

sample code

weather.py

from geopy.geocoders import Nominatim
from meteostat import Normals, Point

def _locating_place(place, country) -> Point:
        geolocator = Nominatim(user_agent='myapplication')
        location = geolocator.geocode(f"{place} {country}")
        coord = {'lon': float(location.raw['lon']),
                 'lat': float(location.raw['lat'])}
        coord = Point(coord['lat'], coord['lon'])
        return coord
       
def get_normals(place: str, country: str):
        coord = self._locating_place(place, country)
        # Get Normals data
        data = Normals(coord)
        data = data.fetch()

Main file of the application: main.py

import certifi
import os
import weather

os.environ['SSL_CERT_FILE'] = certifi.where()

class MyApp(MDApp):
        def build(self):
        ColorUtils.set_look_and_feel(self, True)
        self.root = Builder.load_file('design.kv')
        weather.get_normals('Berlin', 'Germany')
        return self.root

if __name__ == "__main__":
    MyApp().run()

Questions
Is there a way I can disable certificate verification until I fix my app/environment? I saw preferred way is using certifi, I put it inside my code and imported, what can I do to try using it (also) for that request?

The GoodGameCrafter

unread,
Aug 23, 2023, 1:44:14 PM8/23/23
to Kivy users support
import ssl
ssl._create_default_https_context = ssl._create_unverified_context -> bypass https verification

Robert

unread,
Aug 23, 2023, 3:12:50 PM8/23/23
to Kivy users support
Reply all
Reply to author
Forward
0 new messages