user15190177
user15190177

Reputation:

Access API tokens using Fetch

I'm complete beginner at using API's, but I'm trying to exchange my secret for a JWT, using a Token endpoint. (removed the client_credentials on purpose)

fetch('', {
    method: 'POST',
    body: 'grant_type=client_credentials&client_id=' + key + '&client_secret=' + secret,
    headers: {
    "Content-Type": "Bearer [JWT]",
    }
}).then(function (resp) {

    // Return the response as JSON
    return resp.json();

}).then(function (data) {

    // Log the API data
    console.log('token', data);

}).catch(function (err) {

    // Log any errors
    console.log('something went wrong', err);

});

I get this error:

"token {error: "invalid_request", error_description: "grant_type missing"}"

Upvotes: 0

Views: 132

Answers (1)

Shikhar
Shikhar

Reputation: 253

Please try below code to correct your header section

 headers: {
    "Content-Type": "application/json",
    "Authorization":"Bearer [value of JWT Token]"}
  • I am assuming that you are passing JSON as content.
  • Also I am passing the JWT authorization token against Authorization Key.

Let me know if that works for you. Thanks.

Upvotes: 1

Related Questions