Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending POST Request to Get Token Returns Error #6

Open
chialuka opened this issue Sep 25, 2023 · 6 comments
Open

Sending POST Request to Get Token Returns Error #6

chialuka opened this issue Sep 25, 2023 · 6 comments

Comments

@chialuka
Copy link

I've been trying to send a POST request to get the token but I keep getting an error from the API saying the request is invalid. I'm using Node.js.

  const token = await axios({
    method: 'post',
    url: 'https://icdaccessmanagement.who.int/connect/token',
    data: {
	client_id: ICD_CLIENT_ID,
	client_secret: ICD_CLIENT_SECRET,
	scope: 'icdapi_access',
	grant_type: 'client_credentials',
    },
  });
  return token;

The request returns a 400 status code with response: data: { error: 'invalid_request' }

I'm not sure where the problem is from and I'd appreciate any help.

@d-cifuentes
Copy link

Hello,

I am experiencing the same error as @chialuka. I have already tried the same code he shared and an alternative explained in the ICD API Authentication docs shared here.

import fetch from "node-fetch";
import 'dotenv/config'

const scope = "icdapi_access";

const clientID = process.env.CIE_CLIENT_ID;
const clientSecret = process.env.CIE_CLIENT_SECRET;
const tokenEndpoint = process.env.TOKEN_ENDPOINT;

let base64encodedData = Buffer.from(clientID + ':' + clientSecret).toString('base64');

const getNewAuthToken = async () => {
    const token = await fetch(tokenEndpoint, {
        method: 'POST',
        headers: {
            "Content-Type": "application/json",
            'Authorization': 'Basic ' + base64encodedData,
            grant_type: "client_credentials",
            scope
        },
    })

    return token;
}

Does anyone knows how to solve this?

@chialuka
Copy link
Author

chialuka commented May 9, 2024

@d-cifuentes, I finally solved it by setting "Content-type" as "application/x-www-form-urlencoded"

I found this by searching through the headers sent in their Swagger API documentation.

This is what my final request looks like:

const response = await axios({
	method: 'post',
	url: icdTokenUrl,
	headers: {
		'Content-Type': 'application/x-www-form-urlencoded',
	},
	data: {
		client_id: ICD_CLIENT_ID,
		client_secret: ICD_CLIENT_SECRET,
		scope: 'icdapi_access',
		grant_type: 'client_credentials',
	},
});

@d-cifuentes
Copy link

Thank you so much!

You have no idea how many hours i had spent until this moment.

@chialuka
Copy link
Author

chialuka commented May 9, 2024

@d-cifuentes, you're welcome!

And I totally understand. I spent weeks on it. Had to leave it and then found the solution when I came back to it after about 6 months :)

@mzhadigerov
Copy link

where do you guys get the information?

client_id, client_secret

I'm very new to ICD-10 stuff.

@MITEL-UNIUD
Copy link
Collaborator

Register here: https://icd.who.int/icdapi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants