cancel
Showing results for 
Search instead for 
Did you mean: 

Add an External Service - Reading Business Partners: Invalid ApiKey

Jarmo_Tuominen
Participant
0 Kudos

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 $

Accepted Solutions (1)

Accepted Solutions (1)

kevin_hu
Active Participant

I suppose you are following this tutorial

https://learning.sap.com/learning-journey/developing-with-sap-extension-suite/add-an-external-servic...

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.

Answers (3)

Answers (3)

kevin_hu
Active Participant

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

 

tomjohn_br
Explorer
0 Kudos
I had the same issue and your hint solved my problem! Thanks a lot!
J_U
Newcomer
0 Kudos

J_U_0-1661600847376.png

 

Jarmo_Tuominen
Participant
0 Kudos

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'.

<code>502</code>
<message>Error during request to remote service: Request failed with status code 401</message>
</error>
🤔
kevin_hu
Active Participant
0 Kudos
I suppose you are following this tutorial