Simple use of Python looker_sdk Auth Problem (401) running scheduled_plan_run_once_by_id ,some samples ?

Hello,
I am new using looker_sdk and do not find a running sample.
That can run a schedule plan once.
Use looker_sdk, api 3.1, genereated API key for test user.

I simple tried this ( indenting maybe not correct cause of copy);

from looker_sdk import client, models, error
import requests
import requests.auth

if __name__ == "__main__":
 print("Start")
 scheduled_plan_id = 132
 sdk = client.setup("looker.ini")
 looker_api_user = sdk.me()

 sdk.login_user(looker_api_user.id)
try:
    # result = sdk.scheduled_plan_run_once_by_id(scheduled_plan_id)
    print(sdk.auth.user_token.token_type)
    header = {
        "access_token": sdk.auth.user_token.access_token,
        "token_type": sdk.auth.user_token.token_type,
        "expires_in": str(sdk.auth.user_token.expires_in)
    }
    print(header)
    looker_request = "https://springmedia.de.looker.com:19999/api/3.1/scheduled_plans/" + str(scheduled_plan_id) \
                     + "/run_once"
    print(looker_request)
    response = requests.post(looker_request, headers=header)
    print(response)
    sdk.logout()
except Exception as e:
    sdk.logout()
    print(str(e))
sdk.logout()

Result is always:
{‘access_token’: ‘xxxxxxxxxxxxxxxxxxxxxxx’, ‘token_type’: ‘Bearer’, ‘expires_in’: ‘3600’}
https://xxxxxxx.de.looker.com:19999/api/3.1/scheduled_plans/16/run_once
<Response [401]>

Would be simple use at one point
the method sdk.scheduled_plan_run_once_by_id(scheduled_plan_id)
but also do not function.
I would be appreciate for help 🙂
Sample code yould be nice, or what is wrong

0 2 481
2 REPLIES 2

Hello @MichaelD,

I didn’t have time to dig into the LookerSDK to determine the correct method to use for running a schedule once, however, below is a solution which builds off of your code. I’ve tested it works.

import looker_sdk
import requests
import requests.auth

##############
# Parameters
##############
INSTANCE_URL        = 'https://springmedia.de.looker.com/api/3.1/' 
LOOKER_INI_PATH     = '/path/to/looker.ini'
SCHEDULE_ID         = 132

################
# Run Sch. Once
################
print("Logging in.")
sdk = looker_sdk.init31(LOOKER_INI_PATH) 
looker_api_user = sdk.me()
sdk.login_user(looker_api_user.id)

# Basic authentication header.
header = {
    "Authorization": "token " + sdk.auth.token.access_token,
    "Content-Type": "application/json",
}

print("Making request.")
looker_request = f"{INSTANCE_URL}scheduled_plans/{SCHEDULE_ID}/run_once"
response = requests.post(looker_request, headers=header)

print(f"Returned: {response.status_code}")

# Make sure we logout.
sdk.logout()

I think the problem with your request was probably the headers, as they don’t align with what I see in the Looker API authentication documentation. I hope it helps.

Note, the above example uses the latest version of the SDK and Python 3.7.

Kind regards,
–Thomas

@MichaelD - Did you find a solution to this? I’m having a very similar issue, but trying to run a run_look() call instead. Despite Admin permissions and using sudo’ed credentials, I’m still getting a 401 Error on making the API call.