How to integrate Discord OAuth with Memberstack 2.0 API for automated user field updates? Answered
Does anyone happen to know if its possible to setup Discord OAuth with Memberstack 2.0?
Does anyone happen to know if its possible to setup Discord OAuth with Memberstack 2.0?
Comments
13 comments
I don't know if its possible, but I do know a direct integration is on the wishlist https://docs.memberstack.com/hc/en-us/community/posts/10751498679067--Wishlist-Integration-Discord
Ok thank you very much
We’re very interested in Discord signing/signup,
Is it something planned? What is a possible workaround to support Discord?
I was doing some research about this topic, and I got to the conclusion that this solution should be developed by Memberstack. 🤔
The CustomSSO integration allows Memberstack users to be authorized in different applications, but what is really needed in this scenario is Discord users to be authorized in Memberstack applications.
I thought about assigning the login responsibility using the admin package to create a user with a custom server, but still, even if I manage to obtain the token from Discord authorizing the user, there's no method to login to MS via that provider (only the existing providers or by password). So I wouldn't be able to override it unless I also generate and save the password in a database, that would defeat the purpose of using Memberstack 😅
Hey guys,
I have problems regarding Memberstack API. I have bot code that is on GitHub and deployed on Heroku, that bot gives user "Authorization" Discord page (OAuth2) and when user authorizes he gets added to my Discord server, but when he joins Discord server his discord username should be grabbed and put in Memberstack field "discord" of that user that clicked button - this is done by his memberstack user ID.
In heroku logs its showing that all the process works perfectly until its time to update Memberstack field. I have tried multiple methods and these are the errors that showed up:
OAuth2 error: HTTP error! status: 403, body: {"message":"Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=*my secret key*"}
OAuth2 error: HTTP error! status: 403, body: {"message":"'*my secret key*' not a valid key=value pair (missing equal-sign) in Authorization header: 'Bearer *my secret key*'."}
Have you tried emulating your request using postman to see if it goes thru? 🤔
What method are you using to update the customer fields?
(It grabs Memberstack ID of current user - adds him to the server - and then attempt to update field in memberstack but then those errors pop out)
async function updateMemberstackProfile(memberId, discordUsername, retries = 3) {
try {
console.log('Attempting to update Memberstack profile', {
memberId,
discordUsername,
apiKeyPrefix: MEMBERSTACK_SECRET_KEY.substring(0, 5)
});
const updateUrl = `https://api.memberstack.com/v1/members/${memberId}`;
const updateBody = JSON.stringify({
customFields: {
discord: discordUsername
}
}); console.log('Sending update request to Memberstack');
console.log('Update URL:', updateUrl);
console.log('Update body:', updateBody);
const response = await fetch(updateUrl, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': MEMBERSTACK_SECRET_KEY
},
body: updateBody
}); if (!response.ok) {
const responseText = await response.text();
throw new Error(`HTTP error! status: ${response.status}, body: ${responseText}`);
} const responseData = await response.json();
console.log(`Updated Memberstack profile for ${memberId}. Response status: ${response.status}`);
console.log('Response data:', JSON.stringify(responseData, null, 2));
} catch (error) {
console.error('Error updating Memberstack profile:', error);
if (retries > 0) {
console.log(`Retrying... (${retries} attempts left)`);
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second before retrying
return updateMemberstackProfile(memberId, discordUsername, retries - 1);
}
throw error;
}
}
Oh wait, are you using the REST library, right? But where did you get those endpoints? Are you using Memberstack v1? 🤔
In the official docs, the base url is https://admin.memberstack.com/members
var axios = require('axios');
const API_KEY = process.env.MEMBERSTACK_SECRET_KEY
const BASE_URL = 'https://admin.memberstack.com/members'
const headers = { "X-API-KEY": API_KEY }
const data = {
customFields: {
country: "Sweden"
},
metaData: {
language: "Swedish"
},
json: {
friends: ["mem_123...", "mem_456..."]
},
}
await axios.patch(
$BASE_URL}/mem_..., data, { headers })Im using node-fetch library, and you are right about endpoints when I changed them Im not getting those errors but now I got this error:
OAuth2 error: HTTP error! status: 400, body: {"code":"validation/invalid-secret-key","message":"The provided secret key is invalid."}And Im sure I used proper secret key
Remember that Memberstack handles two different environments, test and production. Make sure your secret key is NOT from the test enviroment.
You can identify if the secret key or public key are from test environment if they have
_sb_which stands for sandbox.Is there any API endpoint for test environment, because I am still testing the whole process?
It's the same endpoint, but different keys that set which environment you are using. If your using pk_sb_... and sk_sb_... means you are working with your test users. And if you're using pk_... and sk_... you are using production users.
Check your headers, use this documentation as your example, it uses x-api-key: sk_... header to authorize connections, instead of Authorization header.
I always recommend testing your endpoints in Postman (or similar tools) before implementing it in code.
Thank you so much for fast and helpful response. I solved my issue.
Please sign in to leave a comment.