Samantha is an Avid Commentator
2 published comments for 2024
User bio
404 bio not found
Member since Feb 28, 2018
Replies:

Yes, we built GetAuthorizationToken method ourselves so you can compose the request however you need.

Basically, in order to get your token, you are just making another REST request to their authorization endpoint.

Example:

Method GetAuthorizationToken() As %Status
{
//Authenticate with System
    set tHttpResponse=##class(%Net.HttpResponse).%New()
    set tHttpRequest = ##class(%Net.HttpRequest).%New()
    set tHttpRequest.ContentType="application/x-www-form-urlencoded"
    
    //Get username and pwd out of Credential store
    set objCred = ##class(Ens.Config.Credentials).%OpenId(..Adapter.Credentials)
//Insert the username and pwd (in your case may be "email" instead of "username")
    do tHttpRequest.InsertFormData("username",objCred.Username)
    do tHttpRequest.InsertFormData("password",objCred.Password)
    //add any other things the request may need
    
    //send auth request
    set tURL=..AuthURL
    set tSC = ..Adapter.SendFormDataArray(.tHttpResponse,"POST",tHttpRequest,"","",tURL)
    
    //Get response, were our credentials accepted?
    set tStatusCode = tHttpResponse.StatusCode
    $$$TRACE("Authorization Status Code [" _ tStatusCode _ "]")
    if tStatusCode = 200 {
        set ..tToken=tHttpResponse.access_token
        Return $$$OK
    } else {
    Quit $$$ERROR("5001", "tStatusCode - can't auth with System")
        }
    }
}

What we have typically built into our code is that the OnInit() will just call a separate ..GetAuthorizationToken method where the authorization code is stored.

Then in our GET methods (or whatever API endpoint you are consuming), if we receive a '401 Unauthorized' HTTP Status Response, at that point we will call out to the ..GetAuthorizationToken method. We will get a fresh token, and then continue down the GET method.

There is no need to get a fresh token every time. If we are getting 200 OK responses back, the token must be valid. It should be that when you get a 401 status response, you can capture that into an "if" statement and just call out to your GetAuthorizationToken method at that point.

I found this is just an issue with the UI - I may try extending the XML Adaptor on my message class to see if that will help display the field. But I am able to access my field and was able to test it was available with the following in studio console:

set msg=##class(AH.AHLIB.Custom.Athena.Message.GetEventsSubscribedResponse).%OpenId(<messageIDfromtester>)

set test=""

set tsC=msg.%JSONExportToString(.test)

w test

Certifications & Credly badges:
Samantha has no Certifications & Credly badges yet.
Global Masters badges:
Samantha has no Global Masters badges yet.
Followers:
Following:
Samantha has not followed anybody yet.