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.
Returns:
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:
creds.refresh(Request())
else:
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:
token.write(creds.to_json())
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.
Thanks @mathcatsand and @asehmi for your response. This was my first question in the community and I was very happy 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.
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.
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.