API for External Link Error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have been asked to develop a course navigation link to another Canvas course on our instance that houses instructor resources. This link is to be visible to faculty and admins only.
I have the app created and deployed on my test instance and the visibility is acting correctly. When I click on the link, it opens in a new tab (as programmed). However, I get an error that the course cannot be found in the new tab.
When I refresh the new tab, which has the correct URL populated into it, the page is then found with no problems. The behavior is observed in all browsers. Any ideas?
This is the script that I have written so far:
import requests
API_URL = "https://myuniversity.test.instructure.com/api/v1"
ACCESS_TOKEN = "my-super-secret-access-token"
HEADERS = {
"Authorization": f"Bearer {ACCESS_TOKEN}",
"Content-Type": "application/json"
}
courses_response = requests.get(f"{API_URL}/courses", headers=HEADERS)
if courses_response.status_code != 200:
print(f"Error fetching courses: {courses_response.status_code}, {courses_response.text}")
exit()
courses = courses_response.json()
# Navigation item configuration
NAVIGATION_ITEM = {
"name": "Instructor Resources",
"url": "https://myuniversity.test.instructure.com/courses/16277/",
"visibility": "admins",
"default": "disabled",
"enabled": True,
"windowTarget": "_blank"
}
for course in courses:
course_id = course["id"]
response = requests.post(
f"{API_URL}/courses/{course_id}/external_tools",
headers=HEADERS,
json={
"name": NAVIGATION_ITEM["name"],
"consumer_key": "your_consumer_key",
"shared_secret": "your_shared_secret",
"privacy_level": "public",
"url": NAVIGATION_ITEM["url"],
"custom_fields": {"visibility": NAVIGATION_ITEM["visibility"]},
"course_navigation": {
"text": NAVIGATION_ITEM["name"],
"visibility": NAVIGATION_ITEM["visibility"],
"default": NAVIGATION_ITEM["default"],
"enabled": NAVIGATION_ITEM["enabled"],
"windowTarget": NAVIGATION_ITEM["windowTarget"]
}
}
)
if response.status_code == 200:
print(f"Navigation item created successfully for course {course_id}!")
else:
print(f"Error for course {course_id}: {response.status_code}, {response.text}")