Issue with connection to local ports in Streamlit Cloud

am trying to run a local server using this command: creds = flow.run_local_server(port=0) but I am not able to as the ports are not either free or I don’t have permission to connect to those ports in a streamlit server or it’s taking forever (app just keeps on running) to trying to connect to a port. I need to run the above to open a new window to authenticate users to select and provide access to their google drive account so that their private data can be used with GPT for question answering (a growing application these days).

Please find the full code below and advise me on what could be done to circumvent this issue (if any redirect_uri can be used or any different port or local ip address can be leveraged). Eager to hear soon from you. If this works, we are planning to offer premium streamlit services as well as it’s an excellent platform for developing prototypes easily.

Full code:

def get_credentials():
“”"Gets valid user credentials from storage.

If nothing has been stored, or if the stored credentials are invalid,
the OAuth 2.0 flow is completed to obtain the new credentials.

    Credentials, the obtained credential.
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.json"):
    creds = Credentials.from_authorized_user_file("token.json", SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next run
    with open("token.json", "w") as token:
return creds

You are going to be limited on Streamlit Cloud on what you can do with ports and local servers; I don’t think it’s transparently geared toward that kind of use. Maybe @asehmi might be able to advise if there is any hope here…

Try port 5000. That seems to work in my one app that used it, but it only good for only intra-app API calls, not inter-app or from external client requests, e.g. an external IdP. Alternatively, use st cloud’s built in Google OAuth.

1 Like

Thanks @mathcatsand and @asehmi for your response. This was my first question in the community and I was very happy :blush: to be helped.

@asehmi I tried port 5000 as you recommended. I am not getting any permission error but the streamlit app is just running (as it says in the top-right corner) continuously and is not done with processing (and the code halts/enters an infinite loop exactly at the “creds = flow.run_local_server(port=0)” command line.

Also based on the link you shared, the inbuilt OAuth doesn’t doesn’t provide me with config.json / credentials.json / token.json (does it?) which I can then later use to read google doc contents stored in the client’s drive, which is my main intention for authenticating user.

Please let me know if there is any other alternative solution to this issue as in this langchain+privateGPT world, everyone might be trying to do something like this with streamlit.

I don’t see it in your code snippet, but also try instead of localhost.

Instead of run_local_server? I think it’s the same. I tried using redirect_uri in the InstalledAppFlow.from_client_secrets_file as an argument but that also didn’t help.

I have same issue, the code halt on the run_local_server on Streamlit Cloud but work on my local environment. Do you have solve this issue?

Yes, on local environment all ports are available for use. But in streamlit cloud servers, they aren’t . Haven’t solved this yet. Any inputs from streamlit themselves on what port we can use for this purpose would be helpful.