因此,我将如何这样做:
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=...