Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Invalid request token using Flask example #137

Open
ChemGuy88 opened this issue Mar 26, 2019 · 2 comments
Open

Invalid request token using Flask example #137

ChemGuy88 opened this issue Mar 26, 2019 · 2 comments

Comments

@ChemGuy88
Copy link

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
Copy link
Member

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
Copy link
Contributor

jb-1980 commented Apr 24, 2019

@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.

jb-1980 added a commit to jb-1980/khan-api that referenced this issue Oct 29, 2019
After reviewing Khan#137, I thought it may be a good idea to include a README for the example so that API users have a better reference.
jb-1980 added a commit to jb-1980/khan-api that referenced this issue Oct 30, 2019
After reviewing Khan#137, I thought it may be a good idea to include a README for the example so that API users have a better reference.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants