oTree Forum >

<Response [403]> HTTP Request Header otree-rest-key is missing

#1 by Raphael

Hello everyone,
I would like to activate browser bots on my Heroku server using my command-line browser. I have already created a value called OTREE_REST_KEY on my Heroku server under 'Settings' -> 'Config Vars' -> 'Reveal Config Vars'.

With the help of this Python file, I am able to create a room on my Heroku server:
__________________________________________________________________________________
import requests
from pprint import pprint

GET = requests.get
POST = requests.post

SERVER_URL = 'https://exampleserver.herokuapp.com'
REST_KEY = 'ExampleKey'

def call_api(method, *path_parts, **params) -> dict:
path_parts = '/'.join(path_parts)
url = f'{SERVER_URL}/api/{path_parts}/'
resp = method(url, json=params, headers={'otree-rest-key': REST_KEY})
if not resp.ok:
msg = (
f'Request to "{url}" failed '
f'with status code {resp.status_code}: {resp.text}'
)
raise Exception(msg)
return resp.json()

data = call_api(
POST,
'sessions',
session_config_name='ExampleName',
room_name='ExampleRoom',
num_participants=4,
modified_session_config_fields=dict(use_browser_bots=True),
)
pprint(data)
__________________________________________________________________________________

I have created an environment variable within my PyCharm project that matches the Heroku OTREE_REST_KEY variable.

When I try to test the browser bots with this command:
otree browser_bots ExampleName --server-url=https://exampleserver.herokuapp.com

I receive this error message:
AssertionError: Request to close the existing browser bots session failed. Response: <Response [403]> HTTP Request Header otree-rest-key is missing.

Unfortunately, I'm not sure what I'm overlooking. Can you please help me with this? Thank you very much!
(Of course, the example values have been replaced by me to ensure that I do not disclose my actual data publicly.)

#2 by woodsd42

I'm going to bump this thread as I'm having the same issue.  The documentation is lacking detail on how to actually run browser bots with Heroku.

Automated testing is one of the good features of otree, and using it with Heroku is a good way to assess what level of servers need to be provisioned.  I think even the otree studio users would benefit from this, given the text editor approach is no longer 'recommended' in the documentation.

#3 by Chris_oTree

You need to set the OTREE_REST_KEY env var both on your PC and on Heroku. This key is like a password that gives you permission to create browser bot sessions. Without it, anyone could launch browser bot sessions on your public Heroku server. Anyway thanks for the feedback, I'll look at the documentation if something needs to be expanded.

#4 by woodsd42

Sure, but I think what we actually need to know is the command line code to run the browser bots.  We need to either pass the key in the URL, or pass the key as a custom header? That's the step that isn't clear.

#5 by Chris_oTree

The command is here: https://otree.readthedocs.io/en/latest/misc/bots_advanced.html#command-line-browser-bots-on-a-remote-server-e-g-heroku

For OTREE_REST_KEY, you set it as an env var on your PC, i.e. through your control panel. It's not part of the browser_bots command. Or a more temporary way is to set it on your command line before running the browser_bots command, although the syntax for that depends what shell you are using (PowerShell, bash, etc.).

#6 by woodsd42

That did it, the last hint I needed was needing to set the key on my physical computer, rather than in settings.py or whatever.  I think the average user would struggle to achieve that without step by step instructions though.

Write a reply

Set forum username