Epic Authorize API - The request is invalid

2,517 views
Skip to first unread message

Fhiruser

unread,
Jan 31, 2018, 3:30:54 AM1/31/18
to SMART on FHIR
Am trying to call the Authorize method of Epic, but it gives 
        <h1>An error has occured.</h1>
<h3>The request is invalid.</h3>

Am calling the method from my MVC Web Application using HttpWebRequest , i use non-prod client id only.

Code snippet below.

  public ActionResult Index()
        {
           
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            
            var postStream = request.GetRequestStream();
            var response = (System.Net.HttpWebResponse)request.GetResponse();
            var reader = new System.IO.StreamReader(response.GetResponseStream());
            var jsonResponseString = reader.ReadToEnd();
            reader.Close();
            response.Close();

            return View();
        }

Michele Mottini

unread,
Jan 31, 2018, 9:09:40 AM1/31/18
to SMART on FHIR
You should not POST to the authorization URL, you have to redirect to it

  - Michele
  CareEvolution Inc

Fhiruser

unread,
Feb 1, 2018, 6:19:38 AM2/1/18
to SMART on FHIR
I tried the below.
            Response.Redirect(url);

           // return View();
            return null;
        } 

Getting error as : 

An error has occured.

The request is invalid.

-------
My requirement is : I want to call the EPpic apis in a asp.net mvc application. 
I use redirect_uri=http://localhost/PatientMonitor/Patient/ - Is this OK?
EpicError.JPG

Michele Mottini

unread,
Feb 1, 2018, 9:22:17 AM2/1/18
to SMART on FHIR

 public ActionResult Index()
        {

            Response.Redirect(url);

           // return View();
            return null;
        } 


You are missing &aud and &scope from your URL. Also, the redirect URI should be the exact same as one used when you registered your client

Here is an example of a correct redirect URL for a different app:


    - Michele
   CareEvolution Inc

Fhiruser

unread,
Feb 2, 2018, 6:40:03 AM2/2/18
to SMART on FHIR
Thanks Michele its working now. Its redirecting to MyChart and after login  there it is coming back to my Application.

How can we avoid logging to MyChart every time where we start our web application, Its awkward to get that page always.

Fhiruser

unread,
Feb 2, 2018, 8:04:41 AM2/2/18
to SMART on FHIR
Now issue in getting Access Token, i used the auth code got in the previous request here as below:


https://open-ic.epic.com/argonaut/oauth2/token HTTP/1.1?grant_type=authorization_code&code=0Pwu2BiyAs_I3vt-
wqpfGIYxPMiHqn3BPMJ8wFklE9cAQwJVUoX2OsVgbp2JMRODeJu2IShzQTKMK9RC6tLWvgA-Pndksjn-

The remote server returned an error: (404) Not Found.

Michele Mottini

unread,
Feb 2, 2018, 9:38:38 AM2/2/18
to SMART on FHIR
Now issue in getting Access Token, i used the auth code got in the previous request here as below:


https://open-ic.epic.com/argonaut/oauth2/token HTTP/1.1?grant_type=authorization_code&code=0Pwu2BiyAs_I3vt-
wqpfGIYxPMiHqn3BPMJ8wFklE9cAQwJVUoX2OsVgbp2JMRODeJu2IShzQTKMK9RC6tLWvgA-Pndksjn-


The token request should be a POST with  

grant_type=authorization_code&code=0Pwu2BiyAs_I3vt-
wqpfGIYxPMiHqn3BPMJ8wFklE9cAQwJVUoX2OsVgbp2JMRODeJu2IShzQTKMK9RC6tLWvgA-Pndksjn-

in the body. Are you doing that?

  - Michele
  CareEvolution Inc

Fhiruser

unread,
Feb 5, 2018, 5:43:21 AM2/5/18
to SMART on FHIR
This is what i am doing, please check, is my url correct - https://open-ic.epic.com/argonaut/oauth2/token HTTP/1.1

            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            string reqBody = "{\"grant_type\":\"authorization_code\",\"code\":\""+code+"\",\"redirect_uri\":\"http://localhost/PatientMonitor/Patient\",\"client_id\":\"a8e71151-8fdf-41e6-8b22-d0799269de69\"}";

            var postStream = request.GetRequestStream();
            postStream = request.GetRequestStream();
            var bytes = Encoding.UTF8.GetBytes(reqBody);
            postStream.Write(bytes, 0, bytes.Length);
            postStream.Close();
            var response = (System.Net.HttpWebResponse)request.GetResponse();
            var reader = new System.IO.StreamReader(response.GetResponseStream());
            var jsonResponseString = reader.ReadToEnd();
            reader.Close();
            response.Close();

Michele Mottini

unread,
Feb 5, 2018, 9:36:37 AM2/5/18
to SMART on FHIR
The body is wrong - you are sending JSON, it should be urlencoded instead

By the way, Epic has its own support e-mail list: op...@epic.com

  - Michele
  CareEvolution Inc

Fhiruser

unread,
Feb 6, 2018, 6:10:26 AM2/6/18
to SMART on FHIR

I tried urlencode as below still getting 404. Could you please point out the error in the code or send a sample. Am i missing any parameters?


 public ActionResult Index(string code)
        {
            string url = "https://open-ic.epic.com/argonaut/oauth2/token HTTP/1.1";
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.ContentType = "application/x-www-form-urlencoded";
            string reqBody = "grant_type=authorization_code&code=" + code + "&redirect_uri=http://localhost/PatientMonitor/Patient&client_id=a8e71151-8fdf-41e6-8b22-d0799269de69";
            var postStream = request.GetRequestStream();
            postStream = request.GetRequestStream();
            var bytes = Encoding.UTF8.GetBytes(reqBody);
            postStream.Write(bytes, 0, bytes.Length);
            postStream.Close();           

            var response = (System.Net.HttpWebResponse)request.GetResponse();
            var reader = new System.IO.StreamReader(response.GetResponseStream());
            var jsonResponseString = reader.ReadToEnd();
            reader.Close();
            response.Close();
            return View();
        }

Michele Mottini

unread,
Feb 6, 2018, 9:37:12 AM2/6/18
to SMART on FHIR
            string url = "https://open-ic.epic.com/argonaut/oauth2/token HTTP/1.1";

Wrong URL                                                                                           ^^^^^^^^^^

            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

The response is JSON, not html or XML

 - Michele
  CareEvolution Inc

Fhiruser

unread,
Feb 7, 2018, 6:31:20 AM2/7/18
to SMART on FHIR
Awesome, it worked - i got the token. I will continue further..
Thanks lot for the help. 

Fhiruser

unread,
Feb 8, 2018, 3:42:47 AM2/8/18
to SMART on FHIR
Am able to get the patient data now.

In real world scenario, i will be trying to connect to EPIC from a WCF service, First its redirecting to MyChart and after login  there it will come back to my Application.
How can we avoid logging to MyChart every time because from a wcf service its awkward to get the login page.

Fhiruser

unread,
Feb 9, 2018, 6:17:49 AM2/9/18
to SMART on FHIR
Let me explain my workflow :

I have a WCF service which will be deployed in the Hospital where Cerner also installed. My need is to fetch the patient information for the patientid given using this Wcf service methods.
Would there be individual urls for each hospital?
This kind of  a scenario how can we implement using the sandbox authorization? single sign on ?

Alexpandiyan Chokkan

unread,
Nov 16, 2018, 9:49:21 PM11/16/18
to SMART on FHIR
Hi Michele,

How to get the token? Where we will get the token? I have tried as per your response and but didn't receive any token instead it redirects to MyChart page.


The problem is it is logging out of mychart. 

Msg - 'You have been logged out of MyChart'

Also i have configured two redirect urls, but no use.

Michele Mottini

unread,
Nov 17, 2018, 12:45:05 PM11/17/18
to SMART on FHIR
How to get the token? Where we will get the token? I have tried as per your response and but didn't receive any token instead it redirects to MyChart page.

That's the right behavior. You enter the user credentials  in the MyChart login page (fhirjason / epicepic1 for example in the Epic sandbox), then it redirects back to your app redirect URL with the authorization code in the URL - your app gets it from there and uses POST it to the token end point to get the token.


  - Michele
  CareEvolution Inc

Rajshree Agrawal

unread,
Oct 19, 2019, 5:29:37 AM10/19/19
to SMART on FHIR
Reply all
Reply to author
Forward
0 new messages