Skip to content

Instantly share code, notes, and snippets.

@mamigot
Last active July 14, 2023 13:05
Show Gist options
  • Save mamigot/f65baff9a2f5b16682f1fe26cd69dbbb to your computer and use it in GitHub Desktop.
Save mamigot/f65baff9a2f5b16682f1fe26cd69dbbb to your computer and use it in GitHub Desktop.
Calling an Open edX API
# Open edX APIs often require an access token that is obtained through the OAuth2 protocol. Here are the general steps you would take:
# Request an access token: The first step is to request an access token. For this, you would send a POST request to the /oauth2/access_token endpoint with your client ID and secret. Here’s an example of how to do it with requests in Python:
import requests
import json
url = "https://your-open-edx-instance.com/oauth2/access_token"
headers = {
"Content-Type": "application/json"
}
data = {
"client_id": "<your_client_id>",
"client_secret": "<your_client_secret>",
"grant_type": "client_credentials"
}
response = requests.post(url, headers=headers, data=data)
access_token = response.json().get("access_token")
# Replace <your_client_id> and <your_client_secret> with your actual client ID and secret. The URL should point to your Open edX instance.
# Call the Open edX API: Once you have the access token, you can use it to authenticate your requests to the Open edX API. You do this by including it in the Authorization header of your requests. Here’s an example:
api_url = "https://your-open-edx-instance.com/api/courses/v1/courses/"
headers = {
"Authorization": f"Bearer {access_token}"
}
response = requests.get(api_url, headers=headers)
courses = response.json()
# This example retrieves a list of courses. Replace the URL with the endpoint for the specific resource you’re interested in.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment