NodeJS ConnectyCube用户会话令牌创建问题-“意外签名”

mmvthczy  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(125)

我在使用ConnectyCube REST API进行用户管理时遇到了一个令人困惑的问题。我的代码成功地为操作生成了签名,例如创建app session token,但当试图创建user session token时,服务器总是返回“unexpected signature”错误。
下面是我用来生成签名的方法:

const getSignature = () => {
  const nonce = Math.floor(Math.random() * 1000000);
  const timestamp = Math.floor(Date.now() / 1000);
  const stringForSignature = `application_id=${config.applicationId}&auth_key=${config.authKey}&nonce=${nonce}&timestamp=${timestamp}`;
  const signature = crypto.createHmac('sha1', config.authSecret).update(stringForSignature).digest('hex');
  return { signature: signature, timestamp: timestamp, nonce: nonce };
}

字符串
签名生成似乎正常工作,因为我可以使用它成功创建应用会话令牌:

const getAppSessionToken = async () => {
  const signature = getSignature();
  const response = await axios.post(
    'https://api.connectycube.com/session', {
    'application_id': config.applicationId,
    'auth_key': config.authKey,
    'nonce': signature.nonce,
    'timestamp': signature.timestamp,
    'signature': signature.signature
  }, { headers: { 'Content-Type': 'application/json' } });
  return response.data.session.token;
}


但是,当尝试创建用户会话令牌时,服务器会返回“unexpected signature”错误:

const createSessionWithLogin = async (emailAddress, password) => {
  const signature = getSignature();
  return await axios.post(
    'https://api.connectycube.com/session',
    {
      'application_id': config.applicationId,
      'auth_key': config.authKey,
      'nonce': signature.nonce,
      'timestamp': signature.timestamp,
      'signature': signature.signature,
      'user': {
        'email': emailAddress,
        'password': password
      }
    },
    {
      headers: {
        'Content-Type': 'application/json'
      }
    }
  );
}


我将非常感谢您对可能导致此问题的原因的任何见解或故障排除的建议。谢谢!

nhjlsmyf

nhjlsmyf1#

您可以通过此代码获取用户令牌

const crypto = require('crypto');
const axios = require('axios');

// Config object with your application credentials
const config = {
  applicationId: 'your App ID',
  authKey: 'your authKey',
  authSecret: 'your authSecret',
  emailAddress: 'your login id',
  password: 'your password'
};

// Function to generate the signature
const getSignature = () => {
  const nonce = Math.floor(Math.random() * 1000000);
  const timestamp = Math.floor(Date.now() / 1000);
  const stringForSignature = `application_id=${config.applicationId}&auth_key=${config.authKey}&nonce=${nonce}&timestamp=${timestamp}`;
  const signature = crypto.createHmac('sha1', config.authSecret).update(stringForSignature).digest('hex');
  return { signature: signature, timestamp: timestamp, nonce: nonce };
};

// Function to get the application session token
const getAppSessionToken = async () => {
  const signature = getSignature();
  const response = await axios.post(
    'https://api.connectycube.com/session', {
      'application_id': config.applicationId,
      'auth_key': config.authKey,
      'nonce': signature.nonce,
      'timestamp': signature.timestamp,
      'signature': signature.signature
    }, { headers: { 'Content-Type': 'application/json' } }
  );
  return response.data.session.token;
};

// Function to create a session with user login
const createSessionWithLogin = async (login, password) => {
    const signature = getSignature();
    const userData = {
      'user[login]': login,
      'user[password]': password
    };
  
    const requestData = {
      'application_id': config.applicationId,
      'auth_key': config.authKey,
      'nonce': signature.nonce,
      'timestamp': signature.timestamp,
      'signature': signature.signature,
      ...userData
    };
  
    return await axios.post(
      'https://api.connectycube.com/session',
      requestData,
      {
        headers: {
          'Content-Type': 'application/json'
        }
      }
    );
  };
  

// Example usage
getAppSessionToken()
  .then(token => console.log("Session Token:", token))
  .catch(error => console.error("Error fetching token:", error));

// Example usage of createSessionWithLogin
createSessionWithLogin(config.emailAddress, config.password)
  .then(response => console.log("Session with login:", response.data))
  .catch(error => console.error("Error creating session with login:", JSON.stringify(error)));

字符串
结果

相关问题