我被困在这里太久了。如何使用aws-sdk
为特定REST方法启用API密钥?我可以使用控制台启用它,但是找不到使用nodejs sdk实现此目的的方法。所以基本上想要为指定的API Endpoint + Resource + Method设置密钥。
在以下快照中,我从控制台启用了true
所需的api-key。
提及的文档:AWS Nodejs Doc
以下是我迄今为止所做的事情:
// CREATE API KEY
async function create() {
try {
const apiKey = await apigateway.createApiKeyAsync({
enabled: true,
generateDistinctId: true,
name: NAME_OF_KEY
});
/**
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/APIGateway.html#createUsagePlan-property
*/
// CREATE USAGE PLAN AND LINK API
const usagePlan = await apigateway.createUsagePlanAsync({
name: NAME_OF_USAGE_PLAN,
apiStages: [
{
apiId: API_ID,
stage: STAGE
},
/* more items */
],
quota: QUOTA_INFO,
throttle: THROTTLE_INFO
});
/**
* Creates a usage plan key for adding an existing API key to a usage plan.
*/
// LINK API KEY AND USAGE PLAN
await apigateway.createUsagePlanKeyAsync({
keyId: apiKey.id,
keyType: 'API_KEY',
usagePlanId: usagePlan.id
});
return Promise.resolve(apiKey);
} catch (err) {
return Promise.reject(err);
}
}
答案 0 :(得分:1)
您需要调用函数updateMethod
来更新方法请求:
var params = {
httpMethod: 'STRING_VALUE', /* required */
resourceId: 'STRING_VALUE', /* required */
restApiId: 'STRING_VALUE', /* required */
patchOperations: [
{
from: 'STRING_VALUE',
op: add | remove | replace | move | copy | test,
path: 'STRING_VALUE',
value: 'STRING_VALUE'
},
/* more items */
]
};
apigateway.updateMethod(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
您可以执行以下操作:
var apigateway = new AWS.APIGateway({apiVersion: '2015-07-09'});
var params = {
httpMethod: 'POST',
resourceId: 'resource id',
restApiId: API_ID,
patchOperations: [
{
op: 'replace',
path: '/apiKeyRequired',
value: 'true' || 'false'
},
]
};
apigateway.updateMethod(params, function(err, data) {
if (err) cb(err, err.stack); // an error occurred
else cb(null, data); // successful response
});
希望有所帮助!