|
1 | 1 | # khan-api-wrapper
|
2 | 2 | A simple wrapper around the Khan Academy API for use in node
|
| 3 | + |
| 4 | + |
| 5 | +------------------------------------------------ |
| 6 | + |
| 7 | +## About |
| 8 | +This is a simple implementation of using the Khan Academy API with node. If you |
| 9 | +are only interested in viewing your personal or students' data, you can use it without a browser by calling the `authorizeSelf` method. This will use the alternative method |
| 10 | +of logging in with your own account. This also supports browser based authentication |
| 11 | +for use with a node server like [Express](https://expressjs.com/). |
| 12 | + |
| 13 | +#### Dependencies |
| 14 | +* `axios` to handle http requests |
| 15 | +* `oauth-1.0a` to create the oauth params and signature |
| 16 | + |
| 17 | +#### Set up: |
| 18 | + |
| 19 | +Install: |
| 20 | + |
| 21 | +``` |
| 22 | +$ yarn add khan-api-wrapper |
| 23 | +``` |
| 24 | + |
| 25 | +or |
| 26 | + |
| 27 | +``` |
| 28 | +$ npm install khan-api-wrapper |
| 29 | +``` |
| 30 | + |
| 31 | + |
| 32 | +[Register your app with Khan Academy](https://www.khanacademy.org/api-apps/register), to get the necessary tokens. That is it, you should now be able to use the wrapper in your node application. |
| 33 | + |
| 34 | +#### General use: |
| 35 | +Without a browser: |
| 36 | + |
| 37 | +```javascript |
| 38 | + |
| 39 | +const { KhanOauth, KhanAPIWrapper } = requires("khan-api-wrapper") |
| 40 | + |
| 41 | +// Config variables |
| 42 | +const KHAN_PASSWORD = "password_of_account_used_to_register_app" |
| 43 | +const KHAN_IDENTIFIER = "username_of_account_used_to_register_app" |
| 44 | +const KHAN_CONSUMER_SECRET = "Secret from registering app" |
| 45 | +const KHAN_CONSUMER_KEY = "Key from registering app" |
| 46 | + |
| 47 | +// Instantiate the oauth class that will be used to get the authentication tokens |
| 48 | +const kauth = new KhanOauth( |
| 49 | + KHAN_CONSUMER_KEY, |
| 50 | + KHAN_CONSUMER_SECRET, |
| 51 | + KHAN_IDENTIFIER, |
| 52 | + KHAN_PASSWORD |
| 53 | +) |
| 54 | + |
| 55 | +// First get tokens that can be used to access protected data |
| 56 | +kauth.authorizeSelf() |
| 57 | +.then(async ([token, secret]) => { |
| 58 | + // With fresh tokens, we now instantiate the wrapper |
| 59 | + const kapi = new KhanAPIWrapper( |
| 60 | + KHAN_CONSUMER_KEY, |
| 61 | + KHAN_CONSUMER_SECRET, |
| 62 | + token, |
| 63 | + secret |
| 64 | + ) |
| 65 | + |
| 66 | + // Use a convenience method to fetch the user. Check out the details in |
| 67 | + // "./lib/api-v1.js" |
| 68 | + const user = await kapi.user() |
| 69 | + console.log(user) // should see your user metadata |
| 70 | + |
| 71 | + // Use an undocumented endpoint |
| 72 | + const missionUrl = "/api/internal/user/missions" |
| 73 | + const missions = await kapi.fetchResource(missionUrl, true) |
| 74 | + console.log(missions) // should show your missions |
| 75 | +}) |
| 76 | +``` |
| 77 | + |
| 78 | +The available helper methods can be found in `./lib/api-v1.js` and `./lib/api-v2.js`. |
| 79 | + |
| 80 | +Checkout the `examples` folder for ideas on how to use in your application. |
| 81 | + |
| 82 | +#### Token freshness: |
| 83 | + |
| 84 | +Through trial I have discovered that the access token and secret are valid for 2 weeks. So you may consider storing them in a separate file or database, and write a function to only fetch tokens if they are expired. |
| 85 | + |
| 86 | +```javascript |
| 87 | +const fs = require("fs") |
| 88 | +const { promisify } = require("util") |
| 89 | +const readAsync = promisify(fs.readFile) |
| 90 | +const writeAsync = promisify(fs.writeFile) |
| 91 | + |
| 92 | +const getFreshTokens = async () => { |
| 93 | + const kauth = new KhanOauth( |
| 94 | + KHAN_CONSUMER_KEY, |
| 95 | + KHAN_CONSUMER_SECRET, |
| 96 | + KHAN_IDENTIFIER, |
| 97 | + KHAN_PASSWORD |
| 98 | + ) |
| 99 | + |
| 100 | + // get fresh tokens from Khan Academy. |
| 101 | + const [token, secret] = await kauth.authorizeSelf() |
| 102 | + |
| 103 | + // Save those tokens to the disk, and return them |
| 104 | + return await writeAsync( |
| 105 | + "tokens.json", |
| 106 | + JSON.stringify({ |
| 107 | + token, |
| 108 | + secret, |
| 109 | + timestamp: new Date().getTime(), |
| 110 | + }), |
| 111 | + { encoding: "utf8" } |
| 112 | + ) |
| 113 | + .then(() => [token, secret]) |
| 114 | + .catch(err => { |
| 115 | + throw err |
| 116 | + }) |
| 117 | +} |
| 118 | + |
| 119 | +const getAccessTokens = async () => { |
| 120 | + // Fetch token data from saved json file |
| 121 | + return await readAsync("tokens.json", { encoding: "utf8" }) |
| 122 | + .then(jsonString => JSON.parse(jsonString)) |
| 123 | + .then(({ token, secret, timestamp }) => { |
| 124 | + // Check if tokens are expired |
| 125 | + const now = new Date().getTime() |
| 126 | + if (now - timestamp > 14 * 24 * 3600 * 1000) { |
| 127 | + return getFreshTokens() |
| 128 | + } |
| 129 | + |
| 130 | + // Otherwise just return valid tokens from disk |
| 131 | + return [token, secret] |
| 132 | + }) |
| 133 | + .catch(err => { |
| 134 | + if (err.code === "ENOENT") { |
| 135 | + // file not found, which should happen the first time |
| 136 | + return getFreshTokens() |
| 137 | + } |
| 138 | + |
| 139 | + throw err |
| 140 | + }) |
| 141 | +} |
| 142 | + |
| 143 | +// Then use the function to ensure we only use fresh tokens when necessary |
| 144 | +getAccessTokens() |
| 145 | +.then(([token, secret]) => { |
| 146 | + const kapi = new KhanAPIWrapper( |
| 147 | + KHAN_CONSUMER_KEY, |
| 148 | + KHAN_CONSUMER_SECRET, |
| 149 | + token, |
| 150 | + secret |
| 151 | + ) |
| 152 | + |
| 153 | + ... |
| 154 | +}) |
| 155 | +``` |
0 commit comments