How to Link Discord OAuth with Memberstack 2.0 for Automatic User Profile Sync Answered

Does anyone happen to know if its possible to setup Discord OAuth with Memberstack 2.0?

Comments

13 comments

  • Comment author
    Julian Galluzzo

    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

    0
  • Comment author
    Nathan Hovatter

    Ok thank you very much

    0
  • Comment author
    JustHire Admin

    We’re very interested in Discord signing/signup,
    Is it something planned? What is a possible workaround to support Discord?

    0
  • Comment author
    Raquel Lopez

    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 😅

    0
  • Comment author
    Igor Zeljkovic

    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*'."}
    0
  • Comment author
    Raquel Lopez

    Have you tried emulating your request using postman to see if it goes thru? 🤔
    What method are you using to update the customer fields?

    0
  • Comment author
    Igor Zeljkovic
    I didn't try postman. I tested it myself from another PC and whole process works until part where it should update Memberstack field.

    (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)
     
    Here is part of my code that should update customer fields:
     
    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;
      }
    }
    0
  • Comment author
    Raquel Lopez

    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 })
    0
  • Comment author
    Igor Zeljkovic

    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

    0
  • Comment author
    Raquel Lopez

    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.

    0
  • Comment author
    Igor Zeljkovic

    Is there any API endpoint for test environment, because I am still testing the whole process?

    0
  • Comment author
    Raquel Lopez

    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.

    0
  • Comment author
    Igor Zeljkovic

    Thank you so much for fast and helpful response. I solved my issue.

    0

Please sign in to leave a comment.