Khan / khan-api

Documentation for (and examples of) using the Khan Academy API
http://www.khanacademy.org
377 stars 75 forks source link

Invalid request token using Flask example #137

Open ChemGuy88 opened 5 years ago

ChemGuy88 commented 5 years ago

I used the Flask example in this repo, only editing the values CONSUMER_KEY and CONSUMER_SECRET on lines 14 and 15 of khan_api.py, respectively. I then run:

[In] kapi = KhanAPI(CONSUMER_KEY,CONSUMER_SECRET)
[In] kapi.user()
##################################################
Status Code:  401
Content-Type:  text/plain; charset=utf-8
Text:
OAuth error. Invalid access token: abc123
##################################################

where abc123 is my actual consumer key.

I tried troubleshooting it, and got the following three results:

[In] ka = KhanAcademySignIn()
[In] ka.get_callback_url()
.
.
.
RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.

[In] ka.authorize()
.
.
.
RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.

[In] ka.callback()
.
.
.
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

I read the docstring for KhanAcademySignIn and the comment block that follows it, but I don't understand it. Do I need to flesh out the example more, or is there some server-side issue going on?

Thanks for teaching the kids,

csilvers commented 5 years ago

I'm no expert on this code, or flask, but I believe the error means that you have to run this code actually as part of a flask request -- you can't just call the functions via the CLI. So you'll need to write a program that uses this in its flask route, and then visit the appropriate url in your browser (or maybe use curl).

jb-1980 commented 5 years ago

@csilvers is right in that you should be using this as part of a flask request. It is not designed to be used via the CLI.

Your first line kapi = KhanAPI(CONSUMER_KEY,CONSUMER_SECRET) is not correct because the arguments must be the token and secret returned from the oauth process, not the CONSUMER_KEY and CONSUMER_SECRET. The token and secret are retrieved by completing the oath process in the browser.

This example bootstraps getting the token and secret via a Flask application, and is useful if you are trying to create a UI via the web for people to consume their Khan Academy data. In that case, you can start the server via the CLI by going to the example root and entering

python server.py

Assuming you have installed required dependencies (Flask and rauth), that will run a server that can be accessed at http://localhost:5000 on your development machine, which will allow you to interact with the API via a webpage.

If you just want to use a command line to access your own students' data, you could look at my package at khan_api_wrapper.