<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Getting a reliable Oauth2 session id for JES front end html page running inside of Visual Analyt in Developers</title>
    <link>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/823230#M6108</link>
    <description>&lt;P&gt;Hi Joe,&lt;/P&gt;&lt;P&gt;Using the post to create a new session was my original path. I tried again from scratch, going back to Postman to check to see that everything was working OK. I'm able to create and use a session outside of VA. I get an http 403 error on the create session request when trying to run the same code when the job's web page is launched inside of VA. As a side note, once the error occurs, I cannot go back into SAS Studio and run the job again. The session request hits an http 403 error again. I must log off completely to clear and start over again.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Ed&lt;/P&gt;</description>
    <pubDate>Wed, 13 Jul 2022 21:07:51 GMT</pubDate>
    <dc:creator>eneidam</dc:creator>
    <dc:date>2022-07-13T21:07:51Z</dc:date>
    <item>
      <title>Getting a reliable Oauth2 session id for JES front end html page running inside of Visual Analytics</title>
      <link>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/823000#M6098</link>
      <description>&lt;P&gt;I'm trying to create a front end html page which contains a form to collect information necessary to submit a JES job. The form contains dynamically generated select lists based on lookup tables stored in CAS. The oauth "client_credentials" flow works fine when executed in SAS Studio, but fails to get a valid session id when running the same job html within a web content object in Visual Analytics. It seems the session information returned from the request is different depending on where the job's html is run. In SAS Studio, a single session id is returned and works fine. In VA, a collection is returned and I'm trying to get a session id, but the id is not valid for any downstream data requests.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code used to get the session Id with a valid access token (Viya 3.5):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;async function getCASSession(accessToken) {
    // Define request header object
    var reqHeader = new Headers();
        reqHeader.append("Authorization", "Bearer " + accessToken);
        reqHeader.append("Content-Type", "application/vnd.sas.cas.session+json");            
        reqHeader.append("Accept", "application/json");
       

        // Fetch the session id with a valid access token
        try {
            const response = await fetch('https://MyReportServer/casManagement/servers/cas-shared-default/sessions', {
                method: 'GET', 
                headers: reqHeader
            });

            // Test the fetch response
            if (!response.ok) {
                const message = `Fetch error with status code: ${response.status}`;
                throw new Error(message);
            }

            // Process and return the response
            const sessionData = await response.json();

            // The session id may be returned in a collection when running inside VA
            if (sessionData.id === undefined) {
                var sessionId = sessionData.items[0].id;               
            } else {
                var sessionId = sessionData.id; 
            }
            // Log out data and session id
            // console.log(sessionData);
            console.log(`The session identifier is: ${sessionId}`);

            // Return the session id
            return sessionId;

        // Trap errors in the fetched data
        } catch (err) {
        console.log(`Error: Could not acquire the CAS session. ${err}`);
        }

} // end getCASSession   &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In SAS Studio a single session id is returned as is used for subsequent api calls to get data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In VA a collection is returned but the session is not valid when trying to use it.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;{
  "error": "Unknown session.",
  "code": "SessionUnknown",
  "details": "59f4b7eb-c000-2e49-8b02-f4b3fcde3b51",
  "disposition": null
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; Any insight or help would be much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jul 2022 21:52:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/823000#M6098</guid>
      <dc:creator>eneidam</dc:creator>
      <dc:date>2022-07-12T21:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a reliable Oauth2 session id for JES front end html page running inside of Visual Analyt</title>
      <link>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/823225#M6106</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/82851"&gt;@eneidam&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;When you're running the form from within JES web app or SAS Studio, you're connecting directly to SAS and hence there's only one session id. When you move to the web content VA object, you're now considered outside and multiple session ids are created when calling the html form. So you get a collection on sessions back instead of one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One thing to try is to create the session yourself and then use that session id. In the JavaScript code you presented you do a GET on&amp;nbsp;&lt;/P&gt;
&lt;PRE class="lia-code-sample  language-javascript"&gt;&lt;CODE&gt;/casManagement/servers/cas-shared-default/sessions&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you change the GET to a POST, you'll create a session. Could you try that? Then capture the session id created and use it in your subsequent calls to CAS?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope this helps. Let me know the results and we'll proceed from there.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 19:49:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/823225#M6106</guid>
      <dc:creator>joeFurbee</dc:creator>
      <dc:date>2022-07-13T19:49:27Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a reliable Oauth2 session id for JES front end html page running inside of Visual Analyt</title>
      <link>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/823230#M6108</link>
      <description>&lt;P&gt;Hi Joe,&lt;/P&gt;&lt;P&gt;Using the post to create a new session was my original path. I tried again from scratch, going back to Postman to check to see that everything was working OK. I'm able to create and use a session outside of VA. I get an http 403 error on the create session request when trying to run the same code when the job's web page is launched inside of VA. As a side note, once the error occurs, I cannot go back into SAS Studio and run the job again. The session request hits an http 403 error again. I must log off completely to clear and start over again.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Ed&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 21:07:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/823230#M6108</guid>
      <dc:creator>eneidam</dc:creator>
      <dc:date>2022-07-13T21:07:51Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a reliable Oauth2 session id for JES front end html page running inside of Visual Analyt</title>
      <link>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/823243#M6111</link>
      <description>&lt;P&gt;Thanks for the info, Ed. I'll run your scenario by a few VA SMEs and see what they say / recommend.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2022 02:08:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/823243#M6111</guid>
      <dc:creator>joeFurbee</dc:creator>
      <dc:date>2022-07-14T02:08:12Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a reliable Oauth2 session id for JES front end html page running inside of Visual Analyt</title>
      <link>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/824467#M6118</link>
      <description>I've tried all 3 authentication flows: password, client credentials and auth code. I could get all working OK in SAS Studio and the JobExecution web app. No go if trying to run the job form in VA. Opening a track with support.</description>
      <pubDate>Wed, 20 Jul 2022 19:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/824467#M6118</guid>
      <dc:creator>eneidam</dc:creator>
      <dc:date>2022-07-20T19:43:30Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a reliable Oauth2 session id for JES front end html page running inside of Visual Analyt</title>
      <link>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/829945#M6142</link>
      <description>&lt;P&gt;I worked with Ed on this one through a support ticket.&amp;nbsp; The 403 errors were&amp;nbsp;due to CSRF protections on the microservice, as the service was then expecting a CSRF token for the POST request.&amp;nbsp; To allow the call to work, it was necessary to modify the code a bit to retrieve a CSRF token from /casManagement via a HEAD request, and then include it on the subsequent POST request to the same endpoint.&amp;nbsp; The code snippet added was below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;const csrfResponse = await fetch('/casManagement', {&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;method: 'HEAD' &lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;});&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;// Test the fetch response&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;if (!csrfResponse.ok) {&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;const message = `Fetch error with status code: ${csrfResponse.status}`;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;throw new Error(message);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;var csrfToken = csrfResponse.headers.get("X-CSRF-TOKEN");&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;// Define request header object&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;var reqHeader = new Headers();&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs746A5FAB"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp; reqHeader.append("Authorization", "Bearer " + accessToken);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;reqHeader.append("Content-Type", "application/vnd.sas.cas.session+json");&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;reqHeader.append("Accept", "application/vnd.sas.cas.session+json");&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;reqHeader.append("X-CSRF-TOKEN", csrfToken );&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;// Fetch the session id with a valid access token&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;try {&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;const response = await fetch('/casManagement/servers/cas-shared-default/sessions', {&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;method: 'POST', &lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;headers: reqHeader,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;redirect: 'follow'&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs2654AE3A"&gt;&lt;SPAN class="csCE72B627"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;});&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ......&lt;/P&gt;</description>
      <pubDate>Tue, 23 Aug 2022 17:49:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Getting-a-reliable-Oauth2-session-id-for-JES-front-end-html-page/m-p/829945#M6142</guid>
      <dc:creator>Mickey_SAS</dc:creator>
      <dc:date>2022-08-23T17:49:50Z</dc:date>
    </item>
  </channel>
</rss>

