You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Khan Academy Scratchpad API抓取Scratchpad内容?

How to Fetch Khan Academy Scratchpad Content via the API

Hey there! I’ve wrestled with Khan Academy’s Scratchpad API hurdles before, so let me walk you through actionable steps to get this working. Here’s a breakdown of what you need to do:

1. First, Set Up Your Developer Credentials

Khan Academy requires authenticated requests for accessing user-specific content like Scratchpads. Start here:

  • Head to Khan Academy’s developer portal to register a new application. You’ll get a client ID and client secret—save these, you’ll need them later.
  • When registering, make sure to request the appropriate OAuth scopes. For reading Scratchpads, you’ll need something like read_scratchpads (double-check the exact scope name in the portal, as it might be updated occasionally).

2. Get an Access Token via OAuth2

Since Scratchpads are tied to a user account, you’ll need to go through the OAuth2 authorization flow to get an access token:

  • Redirect the user (or yourself, if it’s your own account) to Khan Academy’s authorization URL, including your client ID, desired scopes, and redirect URI.
  • Once authorized, you’ll receive an authorization code, which you can exchange for an access token using your client secret.
  • This access token will be included in all your API requests as a header.

3. Use the Scratchpad API Endpoints

Once you have your access token, you can start fetching Scratchpad data. Here are the key endpoints to use:

Fetch All Scratchpads for a User

Send a GET request to this endpoint, with your access token in the Authorization header:

curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" "https://www.khanacademy.org/api/v1/user/scratchpads"

The response will be a JSON array of Scratchpad objects, each containing an id, title, created, and other metadata.

Fetch a Single Scratchpad’s Content

Use the id from the previous response to get the full content of a specific Scratchpad:

curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" "https://www.khanacademy.org/api/v1/scratchpad/{SCRATCHPAD_ID}"

This will return the full details, including the content field (the actual Scratchpad text/code) and any associated resources.

4. Troubleshooting Common Issues

  • Invalid Scope Errors: If you get a permission denied response, double-check that you requested the correct scopes during app registration and authorization.
  • Token Expiration: Access tokens typically expire after a set time. You can use a refresh token (if you requested it during the OAuth flow) to get a new access token without re-authenticating.
  • Endpoint Changes: Khan Academy’s API can be updated, so if an endpoint isn’t working, verify the latest endpoint structure in their official developer documentation.

Alternative: If API Access Feels Too Complex

If you’re only trying to fetch your own Scratchpads, you can temporarily use your browser’s network inspector to mimic the requests Khan Academy’s website makes. Just:

  1. Open your Scratchpad in the browser
  2. Open the DevTools Network tab
  3. Look for requests to /api/v1/scratchpad/...
  4. Copy the request headers (including cookies) and replicate the request in a tool like curl or Postman

Note: This is a quick workaround, not a long-term solution. It violates Khan Academy’s terms of service if used for automated scraping, and cookies will expire regularly. Stick to the official API for any ongoing use.

Always make sure to follow Khan Academy’s API usage policies—avoid bulk scraping, and only access content you have permission to use.

内容的提问来源于stack exchange,提问作者Kaspenpalo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:35:53