Help

401 error on fetch()

Topic Labels: Scripting extentions
Solved
Jump to Solution
10029 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Noah_Coleman
6 - Interface Innovator
6 - Interface Innovator

I’m trying to import data from an API. I’m able to authenticate via my terminal, but I’m trying to write a script to automate that process (the access token is only good for 24 hours, so they recommend refreshing the authentication before every request or once per day).

As I understand it, fetch() is the way to do an HTTP POST. I’ve written a function to do this, and I keep getting a 401 Unauthorized error. It looks like nothing after the url is getting passed on. I’m sure this is some kind of syntax error on my part, but I’m not familiar enough with JavaScript or API’s to know what it is. Any help would be appreciated. Code is below:

async function authenticate() {

    let authentication = await fetch(authenticate_url, {
        method: 'POST',
        header: 'content-type: application/x-www-form-urlencoded',
        data: `grant_type=refresh_token&client_id=${clientID}&client_secret=${client_secret}&refresh_token=${refresh_token}&username=${username}&password=${password}`
    });

Error is as follows:

{type: "basic", url: "https://api.formlabs.com/developer/v1/o/token/", status: 401, statusText: "Unauthorized", ok: false…}
type: "basic"
url: "https://api.formlabs.com/developer/v1/o/token/"
status: 401
statusText: "Unauthorized"
ok: false
headers: Object
redirected: false
1 Solution

Accepted Solutions
Noah_Coleman
6 - Interface Innovator
6 - Interface Innovator

I was able to figure it out. I borrowed some code from this gist to format the body correctly and now it works.

async function authenticate() {
    let response = await fetch(authenticate_url, {
        method: 'POST',
        headers: { 'content-type': 'application/x-www-form-urlencoded' },
        body: JSON_to_URLEncoded(authenticateBodyData)
    });
}

That was tricky!

See Solution in Thread

3 Replies 3

Troubleshooting issues with API requests is can be very tricky.
Based on the error message that you received it looks like you need to authenticate into the service. So, you need to look at the documentation for the service and see what type of credentials you need to pass in with your request.

Noah_Coleman
6 - Interface Innovator
6 - Interface Innovator

From the documentation:

This is achieved by send an HTTP POST to https://api.formlabs.com/developer/v1/o/token/ with the following parameters in application/x-www-form-urlencoded content type:

  • “grant_type”: “refresh_token”
  • “client_id”: “your_client_id”
  • “client_secret”: “your_client_secret”
  • “refresh_token”: “your_refresh_token”
  • “username”: “your_username”

I have all of these elements in the data portion of the fetch function, but I don’t think they’re getting passed in correctly for some reason. I’m able to authenticate with no problems with CURL on the terminal using the following:

curl --request POST --url https://api.formlabs.com/developer/v1/o/token/ --header 'content-type:application/x-www-form-urlencoded' --data 'grant_type=refresh_token&client_id=my_clientID&client_secret=my_secret&refresh_token=my_token&username=my_username'
Noah_Coleman
6 - Interface Innovator
6 - Interface Innovator

I was able to figure it out. I borrowed some code from this gist to format the body correctly and now it works.

async function authenticate() {
    let response = await fetch(authenticate_url, {
        method: 'POST',
        headers: { 'content-type': 'application/x-www-form-urlencoded' },
        body: JSON_to_URLEncoded(authenticateBodyData)
    });
}

That was tricky!