I am currently using the lambda to send a API details to get a access token. I then want to save that access token to use later on. It seems it doesn't save the access key. Any tips? Lambda:
var axios = require('axios');
var qs = require('qs');
exports.handler = async (event) => {
var data = qs.stringify({
'client_id': 'theID',
'client_secret': 'theSecret',
'scope': 'ski',
'grant_type': 'client_credentials'
});
var config = {
method: 'post',
url: 'myendpoint',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data : data,
};
const postData = await axios.post('myendpoint',data, config)
console.log(postData);
const response = {
statusCode: 200,
body: JSON.stringify(postData.response.data.access_token),
};
var mykey = data.access_token;
return response;
};
Here is my Error Report:
Response
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'data' of undefined",
"trace": [
"TypeError: Cannot read property 'data' of undefined",
" at Runtime.exports.handler (/var/task/index.js:26:48)",
" at processTicksAndRejections (internal/process/task_queues.js:93:5)"
]
}
Here is my logs:
Function Logs
gOutput],
agent: Agent {
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
scheduling: 'fifo',
maxTotalSockets: Infinity,
totalSocketCount: 1,
maxCachedSessions: 100,
_sessionCache: [Object],
[Symbol(kCapture)]: false
},
socketPath: undefined,
method: 'POST',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: 'API/connect/token',
_ended: true,
res: IncomingMessage {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 3,
_maxListeners: undefined,
socket: [TLSSocket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Array],
trailers: {},
rawTrailers: [],
aborted: false,
upgrade: false,
url: '',
method: null,
statusCode: 200,
statusMessage: 'OK',
client: [TLSSocket],
_consuming: false,
_dumped: false,
req: [Circular *1],
responseUrl: 'myendpoint',
redirects: [],
[Symbol(kCapture)]: false,
[Symbol(RequestTimeout)]: undefined
},
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'myhost',
protocol: 'https:',
_redirectable: Writable {
_writableState: [WritableState],
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
_options: [Object],
_ended: true,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 81,
_requestBodyBuffers: [],
_onNativeResponse: [Function (anonymous)],
_currentRequest: [Circular *1],
_currentUrl: 'myendpoint',
[Symbol(kCapture)]: false
},
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype] {
accept: [Array],
'content-type': [Array],
'user-agent': [Array],
'content-length': [Array],
host: [Array]
}
},
data: {
access_token: 'ThisIsMyAccessKeyIGetBackAndWantToSave',
expires_in: 3600,
token_type: 'Bearer'
}
}
2021-04-10T10:41:22.858Z e5e3bc3a-f08d-4077-8897-709225abd5f8 ERROR Invoke Error {"errorType":"TypeError","errorMessage":"Cannot read property 'data' of undefined","stack":["TypeError: Cannot read property 'data' of undefined"," at Runtime.exports.handler (/var/task/index.js:26:48)"," at processTicksAndRejections (internal/process/task_queues.js:93:5)"]}
END RequestId: e5e3bc3a-f08d-4077-8897-709225abd5f8
REPORT RequestId: e5e3bc3a-f08d-4077-8897-709225abd5f8 Duration: 1631.22 ms Billed Duration: 1632 ms Memory Size: 128 MB Max Memory Used: 19 MB
Request ID
e5e3bc3a-f08d-4077-8897-709225abd5f8
What I am doing is sending client details to this endpoint and it returns a access key that is valid for a certain period. It does give a massive body back that is not needed and I only need to save the info in Data: but it keeps giving me the mentioned error even though I do get the correct response.
Thanks seems like all I had to do was define my key correctly
var mykey = postData.data.accesskey
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.