areed1192 / interactive-broker-python-api

A python packaged used to interact with the Interactive Brokers REST API.
MIT License
383 stars 124 forks source link

macOS Terminal opening but not running the authentication #19

Closed huiosx closed 3 years ago

huiosx commented 3 years ago

When I run ib_client.create_session(), I get the expected message with steps 1-3 telling me how to authenticate. Terminal is opened. However, nothing is run in Terminal so there's nothing running at https://localhost:5000. However, if I cd to the correct folder and do bin/run.sh root/conf.yaml then localhost shows the login screen. So it seems the problem is that the command isn't being passed through to Terminal.

client.py contains def _start_server(self) with this relevant section:

# mac will use the terminal.
        elif self._operating_system == 'darwin':
            IB_WEB_API_PROC = ["open", "-F", "-a", "Terminal", r"bin/run.sh", r"root/conf.yaml"]
            self.server_process = subprocess.Popen(
                args=IB_WEB_API_PROC,
                cwd=self.client_portal_folder
            ).pid

screenshot

My suspicion is that something is up with subprocess. Not only because of this, but also because after Terminal does nothing and the command prompt in VS Code is asking me Would you like to make an authenticated request (Yes/No)?, when I type 'No', I get a few errors including

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ibw/client.py", line 483, in close_session
    return_code = subprocess.call("TASKKILL /F /PID {} /T".format(self.server_process), creationflags=subprocess.DETACHED_PROCESS)
AttributeError: module 'subprocess' has no attribute 'DETACHED_PROCESS'

sc2

After some Googling, someone pointed out that if you have your own subprocess.py then Python will look through that and not find the expected DETACHED_PROCESS, but I don't have my own subprocess.py.

Have I got a corrupt subprocess module, even though it comes as standard with Python? Is something else wrong? Thanks.

huiosx commented 3 years ago

I tried subprocess.Popen(["open", "-F", "-a", "Terminal", r"bin/run.sh", r"root/conf.yaml"]) in its own file, and ran it from /resources/clientportal.beta.gw so that it would pick up bin/rus.sh and root/conf.yaml. Here's what happened: sc3

The error is: “run.sh” can’t be opened because Sandbox is not allowed to open documents in Terminal. It's followed by the same error for conf.yaml.

I came across this error a few times before too while trying out a few ideas. I think this could be key now.

It seems this person had the exact same issue. He doesn't mention interactive-broker-python-api by name, but he quotes the same code I'm talking about verbatim.

huiosx commented 3 years ago

Here's how I got around this issue.

In /resources I created start.command:

#!/bin/bash
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )

cd "$parent_path"/clientportal.beta.gw
bin/run.sh root/conf.yaml

Then in client.py I modified it so that the method _start_server, specifically line 522. It is now: IB_WEB_API_PROC = ["open", "-F", "-a", "Terminal", "start.command"]

You may have to edit your cwd in line 525 to point it to /resources, I'm not sure. For now I've just given the literal path to my /resources folder. Hopefully this helps someone.

incarobot commented 3 years ago

Hey bud ... I am having this same issue, not able to get your work around functioning.

would you be able to post the code as you have it if you don't mind ?? Im wondering if I have done the "start.command" correctly by simply pasting that text and naming my file exactly that .

it does show that I have a server runnon on process id (***) but just can't connect. Cheers.

huiosx commented 3 years ago

Here's what I have: Hope it helps:

1 2

I should note I've now moved to ib-insync as I've found that to work better.