on 02-23-2022 4:14 AM
Hi,
I am reading business partners in course 'Developing with SAP Extension Suite' in excercise 'Add an External Service'. I am getting error 'Invalid ApiKey'. For me it seems the apikey is empty. Why I am getting this error, and what should be done? Thanks in advance for your help!
Below is the application log. Variable 'apikey' is empty.
_______________________
[cds] - model loaded from 7 file(s):
./db/schema.cds
./srv/risk-service.cds
./app/common.cds
./app/services.cds
./srv/external/API_BUSINESS_PARTNER.csn
./node_modules/@sap/cds/common.cds
./app/risks/annotations.cds
[cds] - connect using bindings from: { registry: '~/.cds-services.json' }
[cds] - connect to db > sqlite { database: ':memory:' }
> filling riskmanagement.Mitigations from ./db/data/riskmanagement-Mitigations.csv
> filling riskmanagement.Risks from ./db/data/riskmanagement-Risks.csv
/> successfully deployed to sqlite in-memory db
[cds] - connect to API_BUSINESS_PARTNER > odata-v2 {
url: 'https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER/'
}
[cds] - serving RiskService { at: '/service/risk', impl: './srv/risk-service.js' }
[cds] - server listening on { url: 'http://localhost:4004' }
[cds] - launched at 2/23/2022, 11:20:00 AM, in: 2.207s
[cds] - [ terminate with ^C ]
[cds] - GET /service/risk/BusinessPartners
[remote] - Error: Error during request to remote service: Request failed with status code 401
at createError (/home/user/projects/risk-management4/node_modules/axios/lib/core/createError.js:16:15)
at settle (/home/user/projects/risk-management4/node_modules/axios/lib/core/settle.js:17:12)
at IncomingMessage.handleStreamEnd (/home/user/projects/risk-management4/node_modules/axios/lib/adapters/http.js:269:11)
at IncomingMessage.emit (events.js:412:35)
at endReadableNT (internal/streams/readable.js:1317:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
config: {
url: "/A_BusinessPartner?$top=1000&$orderby=BusinessPartner%20asc&$filter=LastName%20ne%20''%20and%20FirstName%20ne%20''&$select=BusinessPartner,LastName,FirstName",
method: 'get',
proxy: false,
baseURL: 'https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER/',
timeout: 60000,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
data: undefined
},
request: {
method: 'GET',
url: "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER//A_BusinessPartner?$t...",
headers: {
Accept: 'application/json,text/plain',
'accept-language': 'en-US,en;q=0.9,fi;q=0.8',
apikey: '',
'User-Agent': 'axios/0.21.4'
}
},
response: {
status: 401,
statusText: 'Unauthorized',
headers: {
date: 'Wed, 23 Feb 2022 11:20:45 GMT',
'content-type': 'application/json',
'content-length': '90',
connection: 'close'
},
body: {
fault: {
faultstring: 'Invalid ApiKey',
detail: { errorcode: 'oauth.v2.InvalidApiKey' }
}
}
},
isAxiosError: true,
correlationId: '8e485dcc-25bb-4262-9a52-29eee66ca19c'
}
[cds] - Error: Error during request to remote service: Request failed with status code 401
at run (/home/user/projects/risk-management4/node_modules/@sap/cds/libx/_runtime/remote/utils/client.js:250:25)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async RemoteService.<anonymous> (/home/user/projects/risk-management4/node_modules/@sap/cds/libx/_runtime/remote/Service.js:179:20)
at async next (/home/user/projects/risk-management4/node_modules/@sap/cds/lib/serve/Service-dispatch.js:74:17)
at async RemoteService.handle (/home/user/projects/risk-management4/node_modules/@sap/cds/lib/serve/Service-dispatch.js:72:10)
at async RemoteService.handle (/home/user/projects/risk-management4/node_modules/@sap/cds/libx/_runtime/remote/Service.js:230:22)
at async ApplicationService.<anonymous> (/home/user/projects/risk-management4/srv/risk-service.js:37:12)
at async next (/home/user/projects/risk-management4/node_modules/@sap/cds/lib/serve/Service-dispatch.js:74:17)
at async ApplicationService.handle (/home/user/projects/risk-management4/node_modules/@sap/cds/lib/serve/Service-dispatch.js:72:10)
at async _readCollection (/home/user/projects/risk-management4/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/handlers/read.js:270:19) {
statusCode: 502,
innererror: Error: Error during request to remote service: Request failed with status code 401
at createError (/home/user/projects/risk-management4/node_modules/axios/lib/core/createError.js:16:15)
at settle (/home/user/projects/risk-management4/node_modules/axios/lib/core/settle.js:17:12)
at IncomingMessage.handleStreamEnd (/home/user/projects/risk-management4/node_modules/axios/lib/adapters/http.js:269:11)
at IncomingMessage.emit (events.js:412:35)
at endReadableNT (internal/streams/readable.js:1317:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
config: {
url: "/A_BusinessPartner?$top=1000&$orderby=BusinessPartner%20asc&$filter=LastName%20ne%20''%20and%20FirstName%20ne%20''&$select=BusinessPartner,LastName,FirstName",
method: 'get',
proxy: false,
baseURL: 'https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER/',
timeout: 60000,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
data: undefined
},
request: {
method: 'GET',
url: "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER//A_BusinessPartner?$t...",
headers: {
Accept: 'application/json,text/plain',
'accept-language': 'en-US,en;q=0.9,fi;q=0.8',
apikey: '',
'User-Agent': 'axios/0.21.4'
}
},
response: {
status: 401,
statusText: 'Unauthorized',
headers: {
date: 'Wed, 23 Feb 2022 11:20:45 GMT',
'content-type': 'application/json',
'content-length': '90',
connection: 'close'
},
body: {
fault: {
faultstring: 'Invalid ApiKey',
detail: { errorcode: 'oauth.v2.InvalidApiKey' }
}
}
},
isAxiosError: true,
correlationId: '8e485dcc-25bb-4262-9a52-29eee66ca19c'
},
id: '1834004',
level: 'ERROR',
timestamp: 1645615245379
}
user: risk-management4 $
I suppose you are following this tutorial
in risk-service.js, can you try replace "process.env.apikey" with your actual apikey string and try again.
If it works, that means you might need to check your .env file which is holding the apikey as a environment variable.
You have to pass the apikey on either as a config in package.json or an environment variable when consuming api from api.sap.com, like this for example
{ "cds": { "API_BUSINESS_PARTNER": { "[sandbox]": { "credentials": { "headers": { "APIKey": "<Copied API Key>" } } } } } }
and then run
cds watch --profile sandbox
Check this tutorial for details.
https://developers.sap.com/tutorials/btp-app-ext-service-add-consumption.html
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I closed the original workspace, and started to do the exercises again. I got the same error. Below is the
original result after calling the external service 'BusinessPartners'.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Subject | Kudos |
---|---|
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.