cancel
Showing results for 
Search instead for 
Did you mean: 

Issue Consuming OpenAPI OData from SAP Datasphere in Node.js – Access Token Returns Login Link Inste

LavaGB
Newcomer
0 Kudos
154

Hello, community!

I’m working on a project where I need to consume data from SAP Datasphere via OData using OpenAPI in Node.js. I've already set up an application to obtain credentials, successfully made the authentication request, and retrieved the access token.

The issue now is that when I use the access token to pull the data, instead of receiving the JSON data as expected, I get a login link in the response. I've double-checked the request structure and authentication configurations, but I still can't resolve this issue.

Has anyone encountered this before or could suggest any necessary adjustments to get the API to return data in JSON format?

Any guidance or practical examples would be greatly appreciated!

Code:

const { executeHttpRequest } = require('@sap-cloud-sdk/core');
const axios = require('axios');

async function getAccessToken(clientId, clientSecret) {
    const tokenUrl = 'https://url_url_url.authentication.us10.hana.ondemand.com/oauth/token'; // URL de token correta
    const response = await axios.post(tokenUrl,
      new URLSearchParams({
        client_id: clientId,
        client_secret: clientSecret,
        grant_type: 'client_credentials'
      }),
      {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
      }
    );
    return response.data.access_token;
  }
 
  async function executeRequest(accessToken) {
    const response = await axios.get(
      {
        headers: {
          Authorization: `Bearer ${accessToken}`
        }
      }
    );
    console.log(response.data);
  }

(async () => {
  try {
    const accessToken = await getAccessToken('Client ID', 'Client Secret');
    await executeRequest(accessToken);
  } catch (error) {
    console.error('Erro na execução da API:', error);
  }
})();


RETURN:
<html><head><link rel="shortcut icon" href="data&colon;image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /><script>document.cookie="fragmentAfterLogin="+encodeURIComponent(location.hash)+";path=/;Secure;SameSite=None";document.cookie="locationAfterLogin="+encodeURIComponent(location.href.split('#')[0].substring(location.href.split('#')[0].indexOf(location.host) + location.host.length))+";path=/;Secure;SameSite=None";document.cookie="signature=J3wPRHL%2FOV5vrzdIeTdo1n2w9%2F8%3D;path=/;Secure;SameSite=None";location="https://url_url_url.authentication.us10.hana.ondemand.com/oauth/authorize?response_type=code&client_..."</script></head></html>

Accepted Solutions (0)

Answers (0)