English 中文(简体)
利用Node js从Lambda传来基因生物体
原标题:Calling the Genesys API from Lambda using Node js

我需要从基因组获得一份用户名单,并在亚马孙天体上主办的React/NodeJS台上展示。 我想利用Lambda的功能来获取名单,并将其归还给他人。

The Code here https://github.com/PrinceMerluza/prince-purecloud-aws-lambda-Function/blob/master/main.py 在我从沙里德翻译出来后,我似乎确实需要做些什么,但我试图在这里进行认证,这又使大家无法肯定地认识到:

 const base64 = require( ../../../../../node_modules/base-64 );
//import { base64 } from  js-base64 ;
const { resolve } = require( path );
const platformClient = require( purecloud-platform-client-v2/dist/node/purecloud-platform-client-v2.js );
const requests = require( ../../../../../node_modules/request );
   
const usersApi = new platformClient.UsersApi();
const routingApi = new platformClient.RoutingApi();
const client = platformClient.ApiClient.instance;

const CLIENT_ID =  691982c9-ef27-4c8a-b41e-181db6aecc39 ;
const CLIENT_SECRET =  U2gaFlTTkou46hEF-Rwi5pFho1nu0GCnh67nHdwX_3k ;

//const authorization = base64.b64encode(`${CLIENT_ID}:${CLIENT_SECRET}`).decode( ascii );

const authorization = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString( base64 );
const aut2 = Buffer.from(authorization,  base64 ).toString( ascii );

const requestHeaders = {
  Authorization: `Basic ${aut2}`,
   Content-Type :  application/x-www-form-urlencoded ,
};
const requestBody = {
  grant_type:  client_credentials ,
};

exports.handler = async (event) => {

    const response = requests.post( https://login.mypurecloud.com/oauth/token , { data: requestBody, headers: requestHeaders });

    const accessTokenInfo = response.json().access_token;

    console.log("Access Token", accessTokenInfo);
    
    return "success";
};

我认为,这个问题已经到了64年批准的基础,但我已尝试过几个事情,没有任何工作。

是否甚至有可能从兰巴达职能中提一个基因器? 我仅举一个例子,那就没有工作。

问题回答

因此,我将如何这样做:

import platformClient from  purecloud-platform-client-v2 ;

exports.handler = async (event, context, callback) => {
    // using your values
    const clientId =  691982c9-ef27-4c8a-b41e-181db6aecc39 ;
    const clientSecret =  U2gaFlTTkou46hEF-Rwi5pFho1nu0GCnh67nHdwX_3k ;
    
    const client = platformClient.ApiClient.instance;
    client.setEnvironment(platformClient.PureCloudRegionHosts.ap_southeast_2);
    await client.loginClientCredentialsGrant(clientId,clientSecret);
    
    let apiInstance = new platformClient.OrganizationApi();
    const organization = await apiInstance.getOrganizationsMe();
    console.log(organization);
    
    return organization;
}

但是,如果你想按要求行事(档案名称为test.handler<>/code>):

import request from  request ;

exports.handler = async (event, context, callback) => {

    // I do this so that I can use await on the request    
    const httpsRequest = async function(options) {
        try {
            return new Promise((resolve, reject) => {
                request(options, function (error, response) {
                    if (error) return reject(new Error(error));
                    console.log( Response:  , response.statusCode);
                    resolve(response.body);
                });
            });
        }
        catch(err) {
            return {error: err};
        }
    };
    
    // using your values
    const CLIENT_ID =  691982c9-ef27-4c8a-b41e-181db6aecc39 ;
    const CLIENT_SECRET =  U2gaFlTTkou46hEF-Rwi5pFho1nu0GCnh67nHdwX_3k ;
    
    const authorization = Buffer
        .from(`${CLIENT_ID}:${CLIENT_SECRET}`)
        .toString( base64 );
    
    const loginHeaders = {
      Host:  login.mypurecloud.com.au ,
      Authorization: `Basic ${authorization}`,
       Content-Type :  application/x-www-form-urlencoded ,
    };
    
    const loginBody = {
      grant_type:  client_credentials ,
    };
    
    const login_response = await httpsRequest({
        method:  POST ,
        url:  https://login.mypurecloud.com.au/oauth/token ,
        form: loginBody,
        headers: loginHeaders
    });
    
    const login = JSON.parse(login_response);
    
    const access_token = login.access_token;
    
    const requestHeaders = {
       Host:  api.mypurecloud.com.au ,
       Authorization: `Bearer ${access_token}`,
       Content-Type :  application/x-www-form-urlencoded ,
    };
    
    const organization_response = await httpsRequest({
        method:  GET ,
        url:  https://api.mypurecloud.com.au/api/v2/organizations/me ,
        headers: requestHeaders
    });
    
    const organization = JSON.parse(organization_response);
    
    return organization;
}

如果您确实想利用服务器来部署这一档案(如我所做的那样),那么你还需要<编码>。

{
  "devDependencies": {
    "serverless": "^3.17.0",
    "serverless-bundle": "^4.4.0",
    "serverless-better-credentials": "^1.1.3"
  },
  "dependencies": {
    "request": "^2.88.2"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "babel-eslint"
    },
    "rules": {
      "no-unused-vars": "off"
    }
  }
}

a serverless.json:

service: test

frameworkVersion: ">=2.25.0"
useDotenv: true

plugins:
  - serverless-bundle
  - serverless-better-credentials

package:
  individually: true

provider:
  name: aws
  runtime: nodejs18.x
  stage: ${opt:stage,  dev }
  profile: ${env:SERVERLESS_PROFILE}
  region: ${opt:region,  ${env:AWS_DEFAULT_REGION} }
  deploymentBucket:
    name: ${env:SERVERLESS_DEPLOYMENT_BUCKET}
  deploymentPrefix: ${self:provider.region}
  iam:
    deploymentRole: ${env:SERVERLESS_DEPLOYMENT_ROLE}
  logRetentionInDays: 7

functions:
  test:
    handler: test.handler
    description: test - Created by Serverless
    name: ${self:service}-${self:provider.stage}-test
    memorySize: 512
    timeout: 8

<代码>env.dev 配置财产文档:

AWS_DEFAULT_REGION=...
SERVERLESS_PROFILE=...
SERVERLESS_DEPLOYMENT_ROLE=....
SERVERLESS_DEPLOYMENT_BUCKET=...




相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...

热门标签