FrederickG
FrederickG

Reputation: 31

Requesting access token to Zoom API via Oauth - error 'missing grant type'

I'm trying to receive an access token from the Zoom api via Oauth. No matter what form I try and send the body as, 'Content-Type': 'application/json' or Content-Type:application/x-www-form-urlencoded, it always errors to { reason: 'Missing grant type', error: 'invalid_request' }.

var options = {
  method: "POST",
  url: "https://zoom.us/oauth/token",
  body: JSON.stringify({
    grant_type: "authorization_code",
    code: process.env.AUTH_CODE,
  }),
  redirect_uri: "https://zoom.us",
};

var header = {
  headers: {
    Authorization:
      "Basic " +
      Buffer.from(process.env.ID + ":" + process.env.SECRET).toString("base64"),
  },
  "Content-Type": "application/json",
};

var tokCall = () =>
  axios
    .post("https://zoom.us/oauth/token", options, header)
    .then((response) => {
      console.log(response);
    })
    .catch((error) => {
      console.log(error.response);
    });

tokCall();

I'm fairly certain the answer lies in either the data type in which Oauth is receiving the data, or where/if it's receiving the body at all. Any suggestions would be gratefully received.

Upvotes: 3

Views: 3613

Answers (2)

Ahmed Rashad
Ahmed Rashad

Reputation: 507

Though it's a late answer, I'd like to share it since it took me some time to complete this using Axios.
So to make Zoom authorization, you need to do:

  1. Base64 encode the secret and client id
const base64EncodedBody = 
 Buffer.from(`${ZOOM_CLIENT_ID}:${ZOOM_CLIENT_SECRET}`).toString('base64');
  1. URI encode the grant_type, code and redirect_uri
const data = 
 encodeURI(`grant_type=authorization_code&code=${code}&redirect_uri=${redirectUri}`);
  1. Send the request
    const response = await axios.post('https://zoom.us/oauth/token', data, {
      headers: {
        Authorization: `Basic ${base64EncodedBody}`,
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(data),
      },
    });

Upvotes: 0

Kojo Agyeman-Budu
Kojo Agyeman-Budu

Reputation: 131

The error is being thrown because you're sending the data as the body of the post request when the Request Access Token Zoom API is expecting to find them as query parameters which you might know as query strings.

Reference

https://marketplace.zoom.us/docs/guides/auth/oauth#local-test

Image of page from link to highlight the use of query parameters and content-type requirement for API call

Change

var options = {
  method: "POST",
  url: "https://zoom.us/oauth/token",
  body: JSON.stringify({
    grant_type: "authorization_code",
    code: process.env.AUTH_CODE,
  }),
  redirect_uri: "https://zoom.us",
};

to

var options = {
      method: "POST",
      url: "https://zoom.us/oauth/token",
      params: {
        grant_type: "authorization_code",
        code: process.env.AUTH_CODE,
        redirect_uri: "<must match redirect uri used during the app setup on zoom>"
      },
    };

The Content-Type header should be set to application/x-www-form-urlencoded as this is a requirement of the zoom API itself.

BTW, axios requires you to name the body field/object of your request as data and also there's no need for JSON.stringify() method since axios does that for you under-the-hood

Upvotes: 6

Related Questions