0

我正在尝试访问我在 okta 中拥有的授权服务器,因此我可以从 alexa 技能中查看用户个人资料(在本例中为用户名和最喜欢的颜色),但不断收到错误消息。

我只想检索用户信息(以okta名称/颜色声明)。我已经在alexa中设置了帐户链接,因此可能有一种更简单的方法,因为已经配置了clientID和clientSecret。

错误信息

{
    "error": "access_denied",
    "error_description": "The requested feature is not enabled in this environment."
}

这是我必须在 okta 中启用的吗?我可以在 okta 开发人员控制台中使用免费层吗?

代码

function validateAccessToken(token, callback) {
    console.log("token: ", token);
    //start
    var clientId = '**okta open id connect client id generated earlier**';
    var clientSecret = '**okta open id connect client secret generated earlier**';
    var auth = "Basic " + new Buffer.from(clientId + ":" + clientSecret).toString("base64");
    var https = require('https');
    var tokenParam = '?token=' + token;
    var tokenHintParam = '&token_type_hint=access_token';
    var tokenQuery = tokenParam + tokenHintParam;
    var optionspost = {
        host: '**your okta org**.oktapreview.com',
        port: 443,
        path: '/oauth2/**your authorization server id**/v1/introspect' + tokenQuery,
        method: 'POST',
        headers: {
            'Authorization': auth,
            'Accept': 'application/json',
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    };
    console.log(optionspost);
    var jsonResponseUser;

    var firstName;
    var favoriteColor;
    console.log("pre Req Post");
    var jsonObject;
    var reqPost = https.request(optionspost, function(res) {
        console.log("statusCode: ", res.statusCode);
        // uncomment it for header details
        console.log("headers: ", res.headers);

        res.on('data', function(d) {
            console.info('POST result:\n');
            process.stdout.write(d);
            console.info('\n\nPOST completed');
            jsonResponseUser = JSON.parse(d);

            firstName = jsonResponseUser['FirstNameClaim'];
            favoriteColor = jsonResponseUser['FavoriteColorClaim'];
            let responseVoice = firstName + '\'s Favorite Color is ' + favoriteColor;
            console.log("responseVoice :" + responseVoice);
            callback(responseVoice);
        });
    });

    reqPost.end();
    reqPost.on('error', function(e) {
        console.error(e);
    });

}


const FavoriteColorHandler = {
    canHandle(handlerInput) {
        const request = handlerInput.requestEnvelope.request;
        return request.type === 'IntentRequest' &&
            request.intent.name === 'FavoriteColorIntent';

    },
    handle(handlerInput) {
        let favoriteColor = '';
        let request = handlerInput.requestEnvelope.request;
        console.log(handlerInput.requestEnvelope.context.System.user.accessToken);
        let token = handlerInput.requestEnvelope.context.System.user.accessToken;
        return new Promise((resolve) => {
            validateAccessToken(token, function(result) {
                // setTimeout(1000000000000);
                console.log("inside Opp Handler, responseVoice: " + result);
                resolve(handlerInput.responseBuilder.speak(result).getResponse());
            });
        });

    },
};
4

0 回答 0