0

Defined an Named credential to get the access token and status of the named credential is 'Authenticated' but when I do call the Apex call out it doesn't return success response. I am not sure what I am missing here?

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:OKTA_Authentication');
req.setHeader('Accept', 'application/json');
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setMethod('POST');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());

enter image description here

Error response on calling Named credential

    <!DOCTYPE html><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="robots" content="noarchive"/><meta name="googlebot" content="noarchive"/><meta name="robots" content="noindex" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta name="apple-mobile-web-app-capable" content="yes"><meta name="googlebot" content="noindex" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="none" />

<link href="https://ok6static.oktacdn.com/assets/img/icons/favicons/favicon-16x16.c55b69ae49b08edc7c000d12b8e5483f.png" rel="icon" type="image/png" sizes="16x16"/><link href="https://ok6static.oktacdn.com/assets/img/icons/favicons/favicon-32x32.99bc356b6e293b927f9e3a2b69761c26.png" rel="icon" type="image/png" sizes="32x32"/><link href="https://ok6static.oktacdn.com/assets/img/icons/favicons/favicon-96x96.de98828614fa33ca04fcfaa07679f345.png" rel="icon" type="image/png" sizes="96x96"/><meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/img/icons/favicons/ms-icon-144x144.png">
<meta name="application-name" content="Okta"/>
<meta name="theme-color" content="#ffffff">
<meta name="msapplication-config" content="/img/icons/favicons/browserconfig.xml"/>

<title>
     - Page Not Found</title>
<link href="https://ok6static.oktacdn.com/assets/css/saasure-min.b2b2ac191046797c70dcac96526d6c99.css" type="text/css" rel="stylesheet"/><link href="https://ok6static.oktacdn.com/assets/css/okta.30984382ae34d45ba5cb2e042185c723.css" type="text/css" rel="stylesheet"/><link href="https://ok6static.oktacdn.com/assets/css/sections/errors.b2abd427f382d7a7c6648d7d71a06a7f.css" type="text/css" rel="stylesheet"/><!--[if IE]><link href="https://ok6static.oktacdn.com/assets/css/ie/ie.67af4e98a9276b3eedc54211bb17ace8.css" type="text/css" rel="stylesheet"/><![endif]-->
<!--[if gte IE 9]><link href="https://ok6static.oktacdn.com/assets/css/ie/ie9.e98bfbcf44b614a6d63c04328b8b7b5e.css" type="text/css" rel="stylesheet"/><![endif]-->

<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>

<script>
    var okta = {
        migrateMute: true,
        locale: 'en',
        debug: false,
        deployEnv: '',
        userId: '',
        settings: {
            orgId: '',
            orgName: '',
            serverStatus: 'ACTIVE',
            persona: '',
            isDeveloperConsole: '' === 'true',
            isPreview: 'false' === 'true',
            permissions: []
        },
        logHasFeatureError: function(message) {
            var xhr = new XMLHttpRequest();
            xhr.open('POST', '/api/internal/client-logging/has-feature-error', true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.send(JSON.stringify({
              message: message
            }));
        }

    };
</script>
<script>window.okta &#124;&#124; (window.okta = {}); okta.cdnUrlHostname = "//ok6static.oktacdn.com"; okta.cdnPerformCheck = false;</script><script>window.okta &#124;&#124; (window.okta = {});window.okta.mixpanel = true;window.okta.mixpanelTrackingSamplingFactors = {"_DEFAULT":1.0};</script><script>if (window) { window.__OKTA_EXPORT_JQUERY__ = true; }</script>

                <script src="https://ok6static.oktacdn.com/assets/js/mvc/dll-common/dll.common.hbs.pacpack.8b8889c6e766308429a208de867effde.js" crossorigin="anonymous" integrity="sha384-Izj1uPhYkYPHbVxrKGX885K5SW1khm+mTkvDQ9mSQX8U+hPmS2cz3tx1QTD1XNhm" type="text/javascript"></script><script src="https://ok6static.oktacdn.com/assets/js/mvc/init-dll/main-init-dll.pack.a47d53539d4c4e0aaf81de0af19137df.js" crossorigin="anonymous" integrity="sha384-SuwhJv3QJeVuGGDVasNQVRM5zSKFqabyXrc1olquFHaAUqmdEk31CSpUYF0vdWJm" type="text/javascript"></script><script src="https://ok6static.oktacdn.com/assets/js/saasure.nojquery.min.11832dc981c84effec1e66cca4dd7bde.js" crossorigin="anonymous" integrity="sha384-j3aQGS5bIr8MkWGkoqjzDDNHhMjL12C4AuIQ2x+l/CXyhSzaLSg3Dm5itCdtQK0b" type="text/javascript"></script><!--[if lt IE 9]><script src="https://ok6static.oktacdn.com/assets/enduser/js/vendor/css3-mediaqueries.fa295f0132f5335f352071ca3613a94a.js" crossorigin="anonymous" integrity="sha384-7pU2GSgyec3nzQMUNSuzanfJelP9UCOyHil0bOv+WnPKSS9lNA/tcxPyr7NV2w6c" type="text/javascript"></script><![endif]-->

<script>if (window.module) module = window.module;</script>

</head>
<body id="app" class="no-app   ">
<noscript><div id="noscript-mask"></div><div id="noscript-msg" class="infobox infobox-warning infobox-compact"><span class="icon warning-16"></span><h3>Javascript is disabled on your browser.</h3><p>Please enable Javascript and refresh this page to use Okta.</p></div></noscript>

<div id="container">
<iframe id="download-iframe" style="display: none;"><p>Your browser does not support iFrames</p></iframe>
<span class="hide" id="_xsrfToken">null</span><script>
    function trackAdminHelpEvent() {
        // Check if OktaMetrics has been initialized and has component,
        // then tracks the click of global help link in top right of page for appropriate section
        if (typeof OktaMetrics !== "undefined" && OktaMetrics.component) {
            OktaMetrics.track('Help');
        }
    }
</script>

<div id="header" class="clearfix">
        <div class="primary-nav clearfix">
            <div class="container primary-nav-wrap">
                <div class="logo clearfix">
                    <a href="/admin/dashboard"><span class="icon oktamainlogo"></span></a>
                                        </div>

                </div>
        </div>

        </div>

<!--[if gte IE 8]>
  <![if lte IE 9]>

    <style>
    .unsupported-browser-banner-wrap {
      padding: 20px;
      border: 1px solid #ddd;
      background-color: #f3fbff;
    }
    .unsupported-browser-banner-inner {
      position: relative;
      width: 735px;
      margin: 0 auto;
      text-align: left;
    }
    .unsupported-browser-banner-inner .icon {
      vertical-align: text-bottom;
      margin-right: 5px;
      display: inline-block;
      position: static !important;
    }
    .unsupported-browser-banner-inner a {
      text-decoration: underline;
    }
    </style>

    <div class="unsupported-browser-banner-wrap">
      <div class="unsupported-browser-banner-inner">
        <span class="icon icon-16 icon-only warning-16-yellow"></span>You are using an unsupported browser. For the best experience, update to <a href="https://support.okta.com/help/articles/Knowledge_Article/24532952-Platforms---Browser-and-OS-Support">a supported browser</a>.</div>
    </div>

  <![endif]>
<![endif]-->
<div id="content" class="clearfix">
    <div><a href="#" id="startcontent" class="skiplink"></a></div>
<span class="hide" id="error-code">404</span>

  <div class="error-wrap clearfix">
    <div class="error">
      <img src="https://ok6static.oktacdn.com/assets/img/ui/http-error-404.e21dc7337e238d67a501b4bd3d3c1f42.png" width="340" height="145" alt="404: Page Not Found"/><p class="error-title">Page Not Found</p>
    </div>
    <div class="error-content">
      <h1>We couldn't find the page you were looking for.</h1>
      <div class="nav-button-bar">
        <a class="nav-button button-primary" href="/">Go to Homepage</a>
      </div>
    </div>
  </div>

<span class="hide" id="st-app">NONE</span>

</body>
</html>
8
  • So this is the response you're getting from System.debug(res.getBody()) Commented Oct 18, 2019 at 13:43
  • Looks like URL is not correct. Please check once. Commented Oct 18, 2019 at 14:18
  • @Bryan: Yes that is the response I am getting.
    – user28452
    Commented Oct 18, 2019 at 16:29
  • 1
    Are you able to show this callout working in another too like cURL or POSTMAN? Commented Oct 18, 2019 at 18:00
  • @user28452 - is the URL in the named credential the base URL, or the full REST endpoint? (ie, acme.okta.com vs. acme.okta.com/api/v1/authn )
    – Kevin Br
    Commented Apr 15, 2020 at 21:32

2 Answers 2

1

I was able to achieve through normal design.

    public class PDS_OKTA_GetAccessToken_Controller {

    public String invokeOKTATokenService(){
        String client_Id,client_secret,scope,end_point,outputJson;
        for(PDS_API_Settings__c apiCSettings : PDS_API_Settings__c.getall().values()){
            if(string.isNotBlank(apiCSettings.Name) && apiCSettings.Name.equalsIgnoreCase('OKTA Token for Mulesoft')){
                client_Id = apiCSettings.Client_Id__c ;
                client_secret = apiCSettings.Client_Secret__c;
                scope = apiCSettings.Scope__c;
                end_point = apiCSettings.End_Point__c;
                break;
            }
        }
        try {
            HttpRequest req = new HttpRequest();
            Http http = new Http();
            string body = 'grant_type=client_credentials&scope='+scope;
            Blob headerValue = Blob.valueOf(client_id + ':' + client_secret);
            String authorizationHeader = 'Basic ' +  EncodingUtil.base64Encode(headerValue);
            System.debug('@@authHeader==>'+authorizationHeader);
            req.setEndpoint(end_point);
            req.setHeader('Accept', 'application/json');
            req.setHeader('Content-Type','application/x-www-form-urlencoded');
            req.setHeader('Authorization', authorizationHeader);
            req.setHeader('cache-control','no-cache');
            req.setBody(body);
            req.setMethod('POST');
            HttpResponse res = http.send(req);
            outputJson = res.getBody();
        } catch (Exception e) {
            system.debug('@@@ exception in invoke_OKTA_Token_Service: ' + e.getMessage());
        }
        return outputJson;
    }

}
0

I was able to get the NamedCredential working for OKTA. If you see my original post I had scope defined only as openid. It appears you have to provide scope as "openid offline_access" based on this article.

OKTA Refresh Token

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .