Emotiv / cortex-example

Example with Cortex V2/V3 API
https://emotiv.com
MIT License
200 stars 115 forks source link

Authorize issue after successful access request #174

Closed MHormes closed 2 years ago

MHormes commented 2 years ago

So i'm trying to use the free version of the API with the Emotiv BCI. I am able to request the access with my client id and client secret. When I try to authorize and get a cortexToken, the authentication keeps failing with code -32021 . There is a message that the credentials are incorrect. I looked at the documentation and my authorize request seems to be the correct. The same id and client are used in the request access method, which does work. I'm not able to figure out why it keeps failing.

A snippet of the code can be found below. Any help is much appreciated!

const SocketSetup = () => {

const [webSocket, setWebSocket] = useState();
const [headsetId, setHeadsetId] = useState();
const [sessionId, setSessionId] = useState();
const [cortexToken, setCortexToken] = useState();

useEffect(() => {
    const ws = new WebSocket(varb.apiUrl);
    setWebSocket(ws);
}, [])

///method to request access to the BCI data
const requestAccess = () => {
    const request_access_id = 1;
    let ws = webSocket;

    //request access only called once ever
    let accessCall = {
        "id": request_access_id,
        "jsonrpc": "2.0",
        "method": "requestAccess",
        "params": {
            "clientId": varb.clientId,
            "clientSecret": varb.clientSecret
        }
    }

    ws.send(JSON.stringify(accessCall))
    ws.onmessage = (event) => {
        try {
            let data = JSON.parse(event.data);
            if (data.id === request_access_id) {
                console.log(data);
            }
        }
        catch (error) {
            console.log(error);
        }
    }
}

//method to get cortex token for later usage
const authorize = () => {
    const request_token_id = 4;
    let ws = webSocket;

    //get cortex token for later use
    let tokenCall = {
        "id": request_token_id,
        "jsonrpc": "2.0",
        "method": "authorize",
        "params": {
            "clientId": varb.clientId,
            "clientSecret": varb.clientSecret,
            "debit": 100
        }
    }

    ws.send(JSON.stringify(tokenCall))
    ws.onmessage = (event) => {
        try {
            let data = JSON.parse(event.data);
            console.log(data);
            if (data.id === request_token_id) {
                setCortexToken(data.result.cortexToken)
            }
        }
        catch (error) {
            console.log(error);
        }
    }
}

return (
    <>
        <button onClick={() => requestAccess()}>Request access to Emotiv Launcher</button>
        <button onClick={() => queryHeadsets()}>Query the headset</button>
        <button onClick={() => controlHeadset()}>Request headset control</button>
        <button onClick={() => authorize()}>Get auth token</button>
        <button onClick={() => createSession()}>Start session</button>
        <button onClick={() => subscribe()}>subscribe to mental command streaming</button>
    </>
)

} export default SocketSetup;`

Edit: Managed to solve the issue by creating a new client id and secret. Code does work now