BookmarkSubscribeRSS Feed

Getting Python Talking with The SAS Viya REST API [PART 2]

Started ‎10-10-2019 by
Modified ‎10-10-2019 by
Views 8,129

If you have been following along with this two-part series, then you are getting very close to making your very first SAS Viya API call. In Part 1 of this guide series, we discussed how to Register a Client, which provides us with several important pieces of information that we need prior to making an API call. Now, this guide will walk you through making your first  API call to the SAS Viya REST API from with Python.

 

Pre-requisites:

  • Python compiler installed on your local machine
  • Basic understanding of python: functions and variables
  • Client Registration information obtained from Part 1 of this guide series: Application Name, Secret (password)
  • The only required python library needed for this code is the requests library

    import requests

     

Assumption:This guide assumes the python developer has appropriate credentials to access to the SAS Viya platform i.e. username and password. These credentials are the ones that are used to access, for example SAS Visual Analytics, and are typically managed with some combination of Active Directory and the SAS Environment Manager.

 

Obtaining Access Token and Making Calls to the SAS VIYA REST API

 

 

Step 1 : Obtain the Access Token 

 

To get going, we are going to need a few bits of information from the prep-work we did in Part 1 of this guide, specifically the Application Name and the Secret (password). The username and password needed for this API call is one that the developer would use to login to the SAS Viya platform, for example SAS Visual Analytics.

 

After running the code below, we are going to get an access token, also called a Bearer Token. This token is valid for 12 hours assuming the validity was set as such in Part 1 during the registration. After 12 hours, this token will expire and a new one will be required. This is NOT to say that you have re-register again every 12 hours. In essence, you can treat the function below as a log in that has an expiration. The token itself is what assures the API that we have logged in and we are authorized to make an API call. 

 

 

hostname = “example.com”
def obtainAccessTokenPythonUser(appName, secret, username, password):
    url = "https://"+ str(hostname) +"/SASLogon/oauth/token"

    headers = {"Content-Type":"application/x-www-form-urlencoded"}

    data = "grant_type=password&username=" + str(username)+ "&password=" + str(password)

    auth = (str(appName), str(secret)) 

    r = requests.post(url,headers=headers, data=data, timeout=30, auth = auth, verify=False)
    print(r)
    print(r.json())
    if r.status_code == 200:
      return r.json()['access_token']

 

Sample Output:

token2.JPG

 

Step 2: Sample API Call

Now lets make our first API call ! As part of the this call, the access token (Bearer Token) obtained in the previous step needs to be passed in to this request as part of the headers of the request.

The output of many of the API calls is going to be a JSON string containing the response of your request.

 

def samplePythonReuqest(pythonAccessToken):
    headers = {'Accept': 'application/vnd.sas.collection+json',
               'Authorization': 'Bearer ' +str(pythonAccessToken),
               'Accept-Item': 'application/vnd.sas.summary+json',
               'Accept-Language': 'string'}

    r = requests.get('https://' +str(hostname)+ '/reports/reports#reports', 
                      params={}, headers=headers, verify=False)

    print(r.json())
    return r.json()

 

For the list of all available API calls, please reference the full API guide available at https://developer.sas.com/apis/rest/

 

Sample Output (JSON):

json.png

 

How to make this example work for you

This example was created in a Viya 3.4. All code in this article is intended to be submitted in Python 3.6. Client registration, as show in Part 1 has to be successfully completed before attempting to make an API call as demonstrated in this guide. 

 

The attachments for this post are:

  • viya_api_client_call.py 

Run the file on the head Viya Server by sending the following command:

 

python viya_api_client_call.py

 

REFERENCES 

Contributors
Version history
Last update:
‎10-10-2019 10:04 AM
Updated by:

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Tags