I'm using the following code to try and connect to the API, but I'm getting this error response:
Uncaught Reference Error: state is not defined
function ConnectToApi() {
const client = platformClient.ApiClient.instance;
const redirectUri = "https://apps.mypurecloud.com/api/v2/conversations/callbacks";
// Method1: Let loginPKCEGrant generate the code verifier
client.loginPKCEGrant(clientId, redirectUri, { state: state })
.then((data) => {
console.log(data);
// Do authenticated things
})
.catch((err) => {
// Handle failure response
console.log(err);
});
}
I couldn't find info on the redirectUri, so I figured it was what I used, but since the state isn't being overwritten, I assume that means that my Uri is wrong. Please let me know.
You have to provide your own value for state, or don't provide that option at all if your app doesn't need to use the state. Examples:
// Example using the current URI fragment for a SPA that uses fragment-based navigation
client.loginPKCEGrant(clientId, redirectUri, { state: window.location.hash })
// Example not using state
client.loginPKCEGrant(clientId, redirectUri)
This is not an appropriate redirect URI. Nor is it an appropriate API endpoint. The redirect URI should be your app. When making an API request later on, you need to use one of the API servers: https://developer.genesys.cloud/platform/api/.
No, that's the URI for the Genesys Cloud web UI. The redirect URI should be the URI of your application. That might be a localhost address if you're developing locally otherwise would be something on the hostname of the server that's hosting your page/application.
I'm developing locally on a non-hosted web file, an htm and js combo in a folder on my machine, so it makes sense for there to be a problem if I try to redirect to the local address, and my external IP doesn't provide routing.
Does there have to be a redirect? I'd rather just be able to execute without having to redirect.
If you're still getting that error, it's one of those two things. Either the client ID has a typo or the redirect URI you're sending is not an exact match for what's configured on the oauth client.
I'm not sure what you mean. You said you're working on a HTML file, so it will have an absolute URI as served by your locally running web server. It would be something like http://localhost:8080/path/to/file.html.
No, it's part of the OAuth flow. See the links I posted previously or the OAuth RFC directly for documentation on that flow.
Thanks for your help on this. I realized that my understanding on this must be frustrating when paired with my relentless pursuit. I appreciate your assistance.
That error means that the client you're using in the OAuth redirect is not authorized in the org you're trying to authenticate with. If you've double checked that info and are absolutely sure you haven't made any mistakes, please open a case with Genesys Cloud Care to investigate.
When your app does't have a valid auth token, the call to loginImplicitGrant will result in redirecting the user to the auth server to authenticate. Once authentication is complete, it will redirect back to your app's redirect URI.
The multiple traces of "API Connected!" are presumably because you're calling that function multiple times.
Thanks for your help on this and though your responses grew terser over time, the information you were able to provide at the beginning helped my look in the right direction.
I'm now able to host a local page where I can trigger api calls.