Alteryx Server Discussions

Find answers, ask questions, and share expertise about Alteryx Server.
SOLVED

Alteryx API: connect to the new oauth2 API with python

RobertZiegl
8 - Asteroid

The Alteryx Server API service enables quite interesting features from a technical perspective. (e.g. to get users, subscription, workflows, starts jobs, ...)

 

With the new release of the newest Alteryx Server 2022.1, many improvements and change were introduced, and this time, also the authentication method changed quite significantly as the API changed from oauth1 to oauth2 and the legacy oauth1 were removed. (https://help.alteryx.com/release-notes/server/server-20221-release-notes)

 

In the past, we were utilizing the Alteryx API package from github (https://github.com/Theamazingdp/AlteryxGalleryAPI) which included the authentication method, signing method to directly connect to the API and also simply allowed us to connect to the Alteryx Server and trigger first API calls.
(especially interesting for us was the starting point with the Gallery class: https://github.com/Theamazingdp/AlteryxGalleryAPI/blob/master/AlteryxGalleryAPI.py)

 

The new method is not covered yet in this package. (hopefully, it does soon) And I didn’t find a solution online & also had my struggles with the Alteryx support on this one.

 

Getting from oauth1 to oauth2 for the Alteryx Gallery

So, let’s summarize the steps to get from the existing solution to the adjusted one.

 

The oauth2 method is now build in two steps: First get an access token (time constraint) and secondly use the access token to use the API.

 

Using python, firstly define a function to get the access token

def get_access_token():
        json.loads(requests.post(self.gallery_url+'/webapi/oauth2/token',data={'client_id':self.api_key, 'client_secret':self.api_secret, 'grant_type':'client_credentials'}).text)['access_token']

 

Utilizing the gallery Url (e.g. https://www.mygallery.com), API key, API secret key from the Alteryx Gallery.

 

With this token, instead of using the signing method, the API call requires "only" the access token:

def get_subscriptions():

        method = 'GET'
        url = self.api_location + '/webapi/admin/v1/subscriptions/'
        headers = {'Authorization':'Bearer ' + self._access_token}
        output = requests.get(url, headers=headers)
        output, output_content = output, json.loads(output.content.decode("utf8"))

 

 

Looking back, the generation of access token was indeed the trickiest part of the change.

 

 

5 REPLIES 5
MatthewO
Alteryx
Alteryx

@RobertZiegl I have two suggestions for you depending on if you are calling the Server API from a third party application or from an Alteryx workflow.

 

  • New help documentation has been published on the V3 API including the steps to authenticate. Additionally, there is an article on using Postman to test your requests. Postman allows you to export your requests as Python code. This may be a good starting point for development if the request is being made by a third-party application.

  • If you are making the request from an Alteryx workflow, I recommend reading Introducing the Alteryx Server v3 API. The article includes a macro that will execute the authentication step. With the introduction of Oauth2.0, authorization can be accomplished using Alteryx tools rather than a script.
RobertZiegl
8 - Asteroid

Hi,

 

Thanks! The postman article is indeed quite interesting :) And I also like to option to use the build-in nodes from Alteryx directly, this makes it really easy to start with the API.

 

My article was more about sharing how to connect to the API via python as I didn't find any relevant info for this (and the alteryx support didn't help either.)

MatthewO
Alteryx
Alteryx

@RobertZiegl there are many python libraries out there for interacting with APIs. Your best option is to refer to the documentation for your library of choice. Below is an example using the requests library. The script will retrieve an access token from the Server API, then use the token to get a list of the Collections on the Server. I hope this helps!

 

import requests
import json

##############################################
############### GET AUTH TOKEN ###############
##############################################

# Auth variables
apiKey = '{{YOUR API KEY FROM THE SERVER UI}}'
apiSecret = '{{YOUR API SECRET FROM THE SERVER UI}}'
authUrl = 'http(s)://{{YOUR SERVER URL}}/webapi/oauth2/token'

# Request auth token
response = requests.post(
    authUrl,
    data = {
        'grant_type':'client_credentials',
        'client_id' : apiKey,
        'client_secret' : apiSecret
    }
)

# Parse the access token
response = json.loads(response.text)
token = response['access_token']

##############################################
###### MAKE A REQUEST TO THE SERVER API ######
##############################################

requestUrl = 'http(s)://{{YOUR SERVER URL}}/webapi/v3/collections'
headers = {'Authorization' : 'Bearer ' + token}

collections = requests.request('GET',
                              requestUrl,
                              headers=headers)

collections = json.loads(collections.text)

 

RobertZiegl
8 - Asteroid

Hi @MatthewO,

 

Thanks!

Heer
7 - Meteor

Hi. I tried this, but its not getting past

response = requests.post(
    authUrl,
    data = {
        'grant_type':'client_credentials',
        'client_id' : apiKey,
        'client_secret' : apiSecret
    }
)

it throws requests.exceptions.ConnectionError: ('Connection aborted.', error(110, 'Connection timed out')) error. what am i missing?