Package: django-admob-ssv
Django Admob server-side verification (SSV)A Django app providing a view for handling Admob server-side verification callbacks.
Successfully verified callbacks trigger a custom Django signal.
Apps in your project may listen to that signal and reward the user
based on the information received via the callback.Taken from theAdmob SSV documentation:Server-side verification callbacks are URL requests, with query parameters expanded by Google, that are sent by Google to an external system to notify it that a user should be rewarded for interacting with a rewarded video ad. Rewarded video SSV (server-side verification) callbacks provide an extra layer of protection against spoofing of client-side callbacks to reward users.Installationpipinstalldjango-admob-ssvAdd apathfor theadmob_ssv.views.AdmobSSVViewview to yoururlpatterns.fromdjango.urlsimportpathfromadmob_ssv.viewsimportAdmobSSVViewurlpatterns=[path('admob-ssv/',AdmobSSVView.as_view()),]Listen to theadmob_ssv.signals.valid_admob_ssvsignal and make sure
youconnect your receiver properly, otherwise it won't
get called. Take a look at the "Where should this code live?" box.fromdjango.dispatchimportreceiverfromadmob_ssv.signalsimportvalid_admob_ssv@receiver(valid_admob_ssv)defreward_user(sender,query,**kwargs):ad_network=query.get('ad_network')ad_unit=query.get('ad_unit')custom_data=query.get('custom_data')# ...Reference the official Admob SSV documentation for alist of all SSV callback parameters.SettingsYou may optionally set the following options in your Djangosettings.pyfile.
The code snippet below shows the default values used.fromdatetimeimporttimedeltaADMOB_SSV_KEY_SERVER_URL="https://www.gstatic.com/admob/reward/verifier-keys.json",ADMOB_SSV_KEYS_CACHE_TIMEOUT=timedelta(days=1)ADMOB_SSV_KEYS_CACHE_KEY="admob_ssv.public_keys"Usage without Django signalsIf you don't want to use Django signals, you may subclass theadmob_ssv.views.AdmobSSVViewview and override thehandle_valid_ssvmethod.Note that unless you callsuper().handle_valid_ssv(request),
theadmob_ssv.signals.valid_admob_ssvsignal won't be sent.fromadmob_ssv.viewsimportAdmobSSVViewclassMyAdmobSSVView(AdmobSSVView):defhandle_valid_ssv(self,request)->None:query=request.GET.dict()ad_network=query.get('ad_network')ad_unit=query.get('ad_unit')custom_data=query.get('custom_data')# ...Finally add apathfor your custom view to yoururlpatterns.fromdjango.urlsimportpathfrommy_app.viewsimportMyAdmobSSVViewurlpatterns=[path('admob-ssv/',MyAdmobSSVView.as_view()),]Using a custom ECDSA libraryThis project uses theecdsaPython
package to verify the signature of incoming Admob SSV callbacks.If you want to use a different ECDSA library, you may subclass theadmob_ssv.views.AdmobSSVViewview and override theverify_signaturemethod.fromadmob_ssv.viewsimportAdmobSSVViewclassMyAdmobSSVView(AdmobSSVView):defverify_signature(self,public_key:str,signature:bytes,content:bytes)->bool:# Verify the signature using your custom ECDSA library.# Return True if the signature is valid, False otherwise.passFinally add apathfor your custom view to yoururlpatterns.fromdjango.urlsimportpathfrommy_app.viewsimportMyAdmobSSVViewurlpatterns=[path('admob-ssv/',MyAdmobSSVView.as_view()),]Example projectTake a look at our Django example project undertests/project.
You can run it by executing these commands:poetry installpoetry run python tests/project/manage.py migratepoetry run python tests/project/manage.py createsuperuserpoetry run python tests/project/manage.py runserverLive testingThe example project can be used to test Admob server-side verification
live from your local machine. You may use a service likengrokto forward requests from the internet to
your local machine and the Django webserver running at port8000.ngrokhttp8000You are now ready to send test Admob server-side verification callbacks
from the Admob console.