Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

jeffdavies avatar image
jeffdavies asked jeffdavies answered

Getting CORS error when attempting to get Astra auth token via REST API

Getting an auth token from a windows web page

I have a small HTML file that I'm using to test connectivity with DataStax. When I make the POST call to get the auth token, I get a CORS error and a response code of 400, Here is the error code:

  1. Response {type: "cors", url: "https://190b5REDACTED82478-us-east1.apps.astra.datastax.com/api/rest/v1/auth", redirected: false, status: 400, ok: false, …}

    1. body: (...)
    2. bodyUsed: false
    3. headers: Headers {}
    4. ok: false
    5. redirected: false
    6. status: 400
    7. statusText: "Bad Request"
    8. type: "cors"
    9. url: "https://190b562f-adf8-4e48-9ae0-484eabc82478-us-east1.apps.astra.datastax.com/api/rest/v1/auth"

Is this even possible to do? It would have to be because many people will create web sites that need to call DataStax REST api in the background. Any guidance will be greatly appreciated!

astraauthentication
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Erick Ramirez avatar image
Erick Ramirez answered

Yes, this is possible to do. But we need more information in order to diagnose the problem.

If you provide a minimal amount of code that would allow us to replicate the problem, we would be in a better position to assist. Cheers!

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

jeffdavies avatar image
jeffdavies answered

Thanks for getting back to me. Here is my partially redacted HTML file:


<!DOCTYPE html>

<html>

<head>

<meta charset=utf-8 />

<script type="text/javascript">

var authToken = ''; // Gotta get one first!

function getAuthToken() {

var ASTRA_DB_ID='190b562f-adf8-4e48-9ae0-484eabc82478';

var ASTRA_DB_REGION='us-east1'

var ASTRA_DB_USERNAME='jeffdavies'

var ASTRA_DB_KEYSPACE='jd';

var ASTRA_DB_PASSWORD='REDACTED'

var authURL = 'https://' + ASTRA_DB_ID + '-' + ASTRA_DB_REGION + '.apps.astra.datastax.com/api/rest/v1/auth';

console.log("authURL = " + authURL);

var postParams = {

method: 'POST',

headers: { 'Content-Type' : 'application/json', 'Accept' : 'application/json'},

body: { 'username' : ASTRA_DB_USERNAME, 'password' : ASTRA_DB_PASSWORD }

};

console.log(postParams);

fetch(authURL, postParams)

.then(response => response.json())

.then(data => console.log(data));

}

</script>

</head>

<body>

<h1>Test Page</h1>

<div id="foo">

<p>Nothing yet</p>

</div>

<button type="button" onclick="getAuthToken()">Click me!</button>

</body>

</html>


I hope that helps! @Erick Ramirez

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.