Skip to content

Latest commit

 

History

History
75 lines (60 loc) · 3.78 KB

1-Canvas-API-Token.md

File metadata and controls

75 lines (60 loc) · 3.78 KB

Table of Contents

  1. Create env file in backend
  2. Generate your Canvas access token
  3. Start the server
  4. Create an API call to Canvas from within server.js
  5. Next step

Create env file in backend.

  1. Create a .env file inside the backend folder. It's easiest to create this file using a code editor like VSCode. env
  2. Copy the snippet below into .env.
    CANVAS_API_TOKEN=
    CANVAS_API_DOMAIN=https://ubc.instructure.com/api/v1
    
  3. In the next step, we'll create a token and paste it in the CANVAS_API_TOKEN= field.

Generate your Canvas access token

  1. Log into Canvas at canvas.ubc.ca. Click Account in the left menu, and then click Settings. Setting
  2. Scroll to Approved Integration and click + New Access Token. New Access Token
  3. Fill in the Purpose field. For added security, set an expiry date for your token. This way, if you accidentally share your token or your token is stolen, at the very least it won’t be valid forever.
  4. Click Generate Token. Now copy your freshly generated token.
  5. In the .env, CANVAS_API_TOKEN={Paste your token here}
  6. Save .env.

Never share your token with anyone. If you think your token may have been exposed (for example, by accidentally posting it to GitHub), delete your token from Canvas right away. Instructions for creating and deleting access tokens as a student are available on the Canvas Guides.

Start the server

  1. Type npm start into terminal in backend directory. If your server is already running, kill it first: control + c, then start the server. If you see any strange error messages, you might have forgotten to install dependencies using npm install.
  2. Navigate to http://localhost:4001/.

You should see a Hello World! displayed.

Create an API call to Canvas from within server.js

Not sure what an API (application programming interface) is? Here's a guide.

To ensure that you are able to make API calls to Canvas, let's create a call and print the result.

Navigate to server.js in backend folder, and you'll see the following line:

const canvasAPI = require('node-canvas-api')

This line imports a Canvas API object that comes with a bunch of useful functions. The one we want to use is the simplest: getSelf. This returns information about the person who makes the request.

So add the following line below // Make API call to Canvas API here :

canvasAPI.getSelf()
  .then(self => console.log(self))

Once you save the file, the server will automatically restart.

You should receive response back that looks something like this printed in the terminal:

{
  id: 50,
  name: 'Justin Lee',
  created_at: '2017-07-12T10:38:49-07:00',
  sortable_name: 'Lee, Justin',
  short_name: 'Justin Lee',
  avatar_url: 'https://ubc.instructure.com/images/messages/avatar-50.png',
  locale: null,
  effective_locale: 'en-CA',
  permissions: { can_update_name: false, can_update_avatar: true }
}

If you encounter an error, please ask for help!

Next step

Now you're ready to go to Step 2: Create API endpoints in backend.