on 2024 Jan 09 10:04 AM
I am using below code to fetch the token from Event Mesh server, it succeeded with no error.
However, this token is incorrect, the length is only 1601, and the correct token length shall be 1869.
This causes the next code block of publishing an event to SAP event mesh failed with unauthorized error message.
Did anyone succeed to get a correct token before? Please help, thanks.
const fetch = require('node-fetch');
async function getEMAccessToken() {
const EMTokenURL = 'your-token-url';
const EMClientID = 'your-client-id';
const EMClientSecret = 'your-client-secret';
const credentials = Buffer.from(`${EMClientID}:${EMClientSecret}`).toString('base64');
const response = await fetch(EMTokenURL, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${credentials}`
},
body: 'grant_type=client_credentials'
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.access_token;
}
Request clarification before answering.
Don't know if it makes a difference, but I usually attach then credentials to the body, not the header...
// specify form parameters
const formParams = new URLSearchParams()
formParams.append('grant_type', 'client_credentials')
formParams.append('client_id', '[clientIdValue]')
formParams.append('client_secret', '[client secret value]')
// Assemble the post options
const postOptions = {
method: 'POST',
headers: {
'Content-type': 'application/x-www-form-urlencoded'
},
body: formParams
}
// console.log('Post options: ', tokenUrl, post_options);
fetch(tokenUrl, postOptions)
.then(res => res.json())
...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Martin,
Thanks for your suggestion, I follow your example, below is my revised codes. However, after testing, the result is same, it will retrieve an incorrect access token string of 1601 length.
Note: Below codes block is for reference, it does not solve the token issue.
async function getEMAccessToken() {
const EMTokenURL = tokenUrl;
const EMClientID = clientId;
const EMClientSecret = clientSecret;
// specify form parameters
const formParams = new URLSearchParams();
formParams.append('grant_type', 'client_credentials');
formParams.append('client_id', EMClientID);
formParams.append('client_secret', EMClientSecret);
// Assemble the post options
const postOptions = {
method: 'POST',
headers: {
'Content-type': 'application/x-www-form-urlencoded'
},
body: formParams
};
const response = await fetch(EMTokenURL, postOptions);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.access_token;
}
Dear all,
I finally find the root cause of my problem. It's purely my own mistake. Sorry for the confusion caused.
When I wrote the codes in the Javascript, I copied the clientid and clientsecret incorrectly. In fact, I copied the ClientID and ClientSecret of Cloud Foundry Authentication instead of Event Mesh.
When I created the testing project with .NET, I unconsciously used the correct id & secret as I reused the encrypted strings for clientid and clientsecret in a config file from another project.
A big thank you to Dinu, Gregor, and Martin for sharing your insightful comments. Because of your comments, I start to think it would be my own problem instead of some weird behavior from the system. Lesson learned from this experience:
Solution 1:
// Solution 1 from Martin Stenzig
async function getEMAccessToken() {
const EMTokenURL = tokenUrl;
const EMClientID = clientId;
const EMClientSecret = clientSecret;
// specify form parameters
const formParams = new URLSearchParams();
formParams.append('grant_type', 'client_credentials');
formParams.append('client_id', EMClientID);
formParams.append('client_secret', EMClientSecret);
// Assemble the post options
const postOptions = {
method: 'POST',
headers: {
'Content-type': 'application/x-www-form-urlencoded'
},
body: formParams
};
const response = await fetch(EMTokenURL, postOptions);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.access_token;
}
Solution 2:
// Alternative solution from mine
async function getEMAccessToken() {
const EMTokenURL = 'your-token-url';
const EMClientID = 'your-client-id';
const EMClientSecret = 'your-client-secret';
const credentials = Buffer.from(`${EMClientID}:${EMClientSecret}`).toString('base64');
const response = await fetch(EMTokenURL, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${credentials}`
},
body: 'grant_type=client_credentials'
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.access_token;
}<br>
Thanks Martin for sharing the codes, I have quoted them above, it could be a good reference for others who are interested in this topic.
Thanks Gregor for reminding me of the CAP way of messaging handling which I will try later and post another question on it. For special use case, the current codes works fine, but I will rethink it and may consult you again if I encounter issue.
Thanks Dino for sharing the online tool of "JSON Web Tokens - jwt.io", it will be useful for my future exploration.
Last I am not sure what shall I do for this question, shall I delete this question? In fact, my initial assumption (I have encountered an weird behavior from system) is incorrect. If it still offer some value for others, I will leave it there. To consider the relevance to the topic of this question, I may accept Martin's answer as the best answer. I need some advice from the admin or regular members in this community.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
61 | |
8 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.