Unable to use the oauth2 official example

292 views
Skip to first unread message

Ronen Melamed

unread,
Dec 13, 2021, 7:31:55 AM12/13/21
to Freesound API
Hi Frederic!

I'm trying to sequentially download animal audio files in the original quality/format by some filters using a python script, for academic research purposes.

I'm trying to use this example:

but I get errors.
I'm new to OAuth2.

from what i understood from the script's comment and the docs, my credentials should be:Capture1.JPG

so I modified the script as in the images below. an error KeyError: 'oauth_state' is received (line 49) after visiting http://127.0.0.1:5000/ and grant access to application.
Any help will be much appreciated, Thanks in advance! :)

c2.JPG


c3.JPG



Console:
 * Serving Flask app 'freesound_flask' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 769-334-499
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
authorization_url:  https://freesound.org/apiv2/oauth2/authorize/?response_type=code&client_id=jmnAgSbsJqK3fIXiqAR4&state=u6nK10rvrud6xjDKZStdGhg5nOMLKJ
127.0.0.1 - - [13/Dec/2021 12:57:36] "GET / HTTP/1.1" 302 -
127.0.0.1 - - [13/Dec/2021 12:57:39] "GET /callback?state=u6nK10rvrud6xjDKZStdGhg5nOMLKJ&code=N5gIKsv9A6AJKBtOEJMGfgVZIMFsdm HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Git\Research\Species\util_files\freesound_dir\freesound_flask.py", line 55, in callback
    freesound = OAuth2Session(client_id, state=session['oauth_state'])
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\sessions.py", line 79, in __getitem__
    return super().__getitem__(key)
KeyError: 'oauth_state'
127.0.0.1 - - [13/Dec/2021 12:57:40] "GET /callback?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [13/Dec/2021 12:57:40] "GET /callback?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [13/Dec/2021 12:57:40] "GET /callback?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [13/Dec/2021 12:57:40] "GET /callback?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
 * Detected change in 'C:\\Git\\Research\\Species\\util_files\\freesound_dir\\freesound_flask.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 769-334-499
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Frederic Font Corbera

unread,
Dec 15, 2021, 1:05:04 PM12/15/21
to freeso...@googlegroups.com
Hi,

Looks like "oauth_state" is not saved properly in the flask session.  Can you print the "state" variable returned in line 31 of the original example and make sure that this variable is there in line 54 (my code) where the error happens?

frederic

--
Frederic Font - ffont.github.io
Music Technology Group, UPF - mtg.upf.edu
Freesound - freesound.org



--

---
You received this message because you are subscribed to the Google Groups "Freesound API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to freesound-ap...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/freesound-api/c8080883-4135-4695-a824-46e273150190n%40googlegroups.com.

Ronen Melamed

unread,
Dec 20, 2021, 3:47:16 PM12/20/21
to Freesound API
Hi Frederic, sorry for the delay, I thought I would get a notification when answered. thanks for the reply.

I changed the script so it will be identical to the original (besides client_id and client secret values).
Highlighted below, you can see the value of the "state" variable at line 31.

I didn't understand fully what do you mean by "make sure that this variable is there in line 54 (my code)", the variable "state"
is not in the scope of the function "callback" and trying to print it results in runtime error: NameError: name 'state' is not defined.

Regards,
Ronen


C:\Git\Research\Species\venv\Scripts\python.exe C:/Git/Research/Species/util_files/freesound_dir/freesound_flask_src.py
 * Serving Flask app 'freesound_flask_src' (lazy loading)

 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 769-334-499
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
state in line 31:  GfwlmD2KwRunpdU8TZZUcAtF3DkIXQ
127.0.0.1 - - [20/Dec/2021 22:40:13] "GET / HTTP/1.1" 302 -
127.0.0.1 - - [20/Dec/2021 22:40:17] "GET /callback?state=GfwlmD2KwRunpdU8TZZUcAtF3DkIXQ&code=cnUU48bcIjKywRvkoThBsKetcVpTAT HTTP/1.1" 500 -

Traceback (most recent call last):
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Git\Research\Species\util_files\freesound_dir\freesound_flask_src.py", line 55, in callback

    freesound = OAuth2Session(client_id, state=session['oauth_state'])
  File "C:\Git\Research\Species\venv\Lib\site-packages\flask\sessions.py", line 79, in __getitem__
    return super().__getitem__(key)
KeyError: 'oauth_state'
127.0.0.1 - - [20/Dec/2021 22:40:17] "GET /callback?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [20/Dec/2021 22:40:17] "GET /callback?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [20/Dec/2021 22:40:17] "GET /callback?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [20/Dec/2021 22:40:17] "GET /callback?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -



Frederic Font Corbera

unread,
Dec 21, 2021, 5:14:37 AM12/21/21
to freeso...@googlegroups.com
Hi,

It is strange that “state” is not in the scope, you should try to add it there somehow.

Cheers,

frederic


--

Ronen Melamed

unread,
Dec 28, 2021, 9:14:34 AM12/28/21
to Freesound API
if "state" variable is defined in a function "demo()" how can i it also be in the scope of the function "callback()"

Thanks,
Ronen




Frederic Font Corbera

unread,
Dec 30, 2021, 7:03:36 AM12/30/21
to freeso...@googlegroups.com
Hi,

What I'm saying is that, as a test, you should try to make the state variable available in the callback function. Maybe save it as a variable in the global scope or find a way to pass it (this is a python/flask thing, not a freesound thing). The code example was recently reported to be working, so maybe there are some issues with the versions of libraries/python that you're using.

Cheers,


frederic

--
Frederic Font - ffont.github.io
Music Technology Group, UPF - mtg.upf.edu
Freesound - freesound.org


Reply all
Reply to author
Forward
0 new messages