我的访问令牌授权失败

时间:2017-07-06 12:19:17

标签: node.js twilio

const Twilio = require('twilio');

const config = require('./config');
const nameGenerator = require('../name_generator');

// Access Token used for Video, IP Messaging, and Sync
const AccessToken = Twilio.jwt.AccessToken;
const IpMessagingGrant = AccessToken.IpMessagingGrant;
const VideoGrant = AccessToken.VideoGrant;
const SyncGrant = AccessToken.SyncGrant;

/**
 * Generate an Access Token for an application user - it generates a random
 * username for the client requesting a token, and takes a device ID as a query
 * parameter.
 *
 * @return {Object}
 *         {Object.identity} String random indentity
 *         {Object.token} String token generated
 */
function tokenGenerator() {
  // Create an access token which we will sign and return to the client
  const token = new AccessToken(
    config.TWILIO_ACCOUNT_SID,
    config.TWILIO_API_KEY,
    config.TWILIO_API_SECRET
  );

  // Assign the generated identity to the token
  token.identity = nameGenerator();

  // Grant the access token Twilio Video capabilities
  const videoGrant = new VideoGrant();
  token.addGrant(videoGrant);

  if (config.TWILIO_CHAT_SERVICE_SID) {
    // Create a "grant" which enables a client to use IPM as a given user,
    // on a given device
    const ipmGrant = new IpMessagingGrant({
      serviceSid: config.TWILIO_CHAT_SERVICE_SID
    });
    token.addGrant(ipmGrant);
  }

  if (config.TWILIO_SYNC_SERVICE_SID) {
    // Create a "grant" which enables a client to use Sync as a given user,
    // on a given device
    const syncGrant = new SyncGrant({
      serviceSid: config.TWILIO_SYNC_SERVICE_SID
    });
    token.addGrant(syncGrant);
  }

  // Serialize the token to a JWT string and include it in a JSON response
  return {
    identity: token.identity,
    token: token.toJwt()
  };
}

module.exports = tokenGenerator;

上面是令牌生成器。下面是它如何带入代码

    // Initialize the Chat client
    new Twilio.Chat.Client.create(data.token).then(function(chatClient) {
      joinChannels(chatClient);
      // create an on click event handler on a button or buttons

有谁知道如何解决这个问题?

Error message

内容

  

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0.eyJqdGkiOiJTS2FmMjI0ZWM4MTNhODEwMmFhNzY4NTQzMDRlNTNhZDVhLTE0OTkzNDU2NDMiLCJpc3MiOiJTS2FmMjI0ZWM4MTNhODEwMmFhNzY4NTQzMDRlNTNhZDVhIiwic3ViIjoiQUMyODdhZmI5NWM5NTFhZjJhZWNhMTEwMWE3MGI0M2JhOCIsImV4cCI6MTQ5OTM0OTI0MywiZ3JhbnRzIjp7ImlkZW50aXR5IjoiSmFyZWQiLCJpcF9tZXNzYWdpbmciOnsic2VydmljZV9zaWQiOiJJUzMwN2I2MGEzZDgxMTQ4YmM4ZDY3Y2QyODA3MmQ0NmVjIiwiZW5kcG9pbnRfaWQiOiJpcC1tZXNzYWdpbmctZGVtbzpKYXJlZDpkZW1vLWRldmljZSJ9fX0.y0hCs09Kkx6KbQmhPblMY-LwS5Pi02-wwU0ijR8tI28

0 个答案:

没有答案