Looker API with Python

low_priority
done
reply
api
(Tian Cheng) #1

Hi,
I am trying to download some dashboards through looker with Looker API. I tried multiple ways to log in looker followed by the instruction on the website, but none of them success. Can anyone provide me a basic way to first pass the authorization step of Looker API.

Example code tried:
import requests

def log_me_in(id, secret, base_url):
payload = {‘client_id’: id, ‘client_secret’: secret}
url = base_url + ‘/login’
r = requests.post(url, data=payload)
# check for successful auth
if r.status_code == 200:
token = r.json()[‘access_token’]
return token
else:
print(‘Authentication failed’)

def get_look(token, look_id, base_url):
# hard coded for json format response
url = base_url +’/looks/’ + look_id + ‘/run/json?access_token=’ + token
r = requests.get(url)
if r.status_code == 200:
for elem in r.json():
print(str(elem))

0 Likes

(Izzy) #2

What error/status code are you getting when trying to run this? It might be helpful to print the r.status_code instead of just ‘Authentication Failed’, so we know what exactly is going wrong.

If you’re comfortable with not using requests, we have a semi-official Looker SDK for python, with a LOT of documentation here: https://github.com/llooker/python_sdk and it’s installable via pip install lookerapi

0 Likes

(Luke Brutvan) #3

Our team found Eric’s how-to pretty useful if you want to continue going the requests route: article

0 Likes