Handling 3rd Party API access

Summary

I can’t seem to navigate around redirects from strava’s 3rd party api. Ideally I want the user to connect their data to the app

def app(df):
st.set_page_config(page_title=‘Home’)

if st.button('Authorize Strava Access'):
    # Construct the authorization url
    auth_url = f"https://www.strava.com/oauth/authorize?client_id={''}&response_type=code&redirect_uri={'http://localhost/'}&scope={'activity:read_all'}&approval_prompt=force"
    # Redirect the user to the authorization url
    webbrowser.open_new_tab(auth_url)

authorization_code = st.text_input('Enter the authorization code:')

Are there any cleaner solutions with Streamlit to connect this API? without the manual redirect?

Hi @George_Gomes

It does appear that Strava requires the user to give consent to the application as outlined at the following:

https://developers.strava.com/docs/authentication/

Hi Chanin tyvm for the reply,

So you are correct in saying that Strava handles the authentication for a 3rd party app.

The issue that I am trying to navigate is the manual process that the user on streamlit would have to proceed with to ensure this authentication takes place.

Below

def app(df):
    st.set_page_config(page_title='Home')
    client_id = '' # your client_id goes here
    client_secret = '' # your client_secret goes here

    if st.button('Authorize Strava Access'):
        # Construct the authorization url
        auth_url = f"https://www.strava.com/oauth/authorize?client_id={client_id}&response_type=code&redirect_uri={'http://localhost/'}&scope={'activity:read_all'}&approval_prompt=force"
        # Redirect the user to the authorization url
        webbrowser.open_new_tab(auth_url)
    
    authorization_code = st.text_input('Enter the authorization code:')

    if authorization_code:
        # Request access token
        response = request_token(client_id, client_secret, authorization_code)

        print(f"Response from request_token: {response}") # Debug print statement

        # Check response status
        access_token = response['access_token']

        # Get the user's activity data
        activity_data = get_all_activity_data(access_token)

        # Convert the activity data to a dataframe
        df = pd.DataFrame(activity_data)

The workflow at the moment is as follows -

  1. Click st.button to redirect to auth_url
  2. User logs in on strava url
  3. User click authorise.
    4.New url loaded with auth code in. User copies this auth code into st.text_input
  4. This is inputted into another function (request_token)
  5. Access token is returned and data is generated.

I want to take out the manual step4, is that possible with streamlit?

Hi!
i deployed my app on streamlit cloud; i want to give access to only user with specific ip addresses but my code is working in local environment but not worked in deployed environment. I dont what is the issue.
Thanks in advance!!
here is my code:
ALLOWED_IP_ADDRESSES = [“124.109.39.000”, “319.32.24.988”]
def get_user_ip():
try:
response = requests.get(“https://api64.ipify.org?format=json”)
if response.status_code == 200 and “ip” in response.json():
return response.json()[“ip”]
except requests.RequestException as e:
st.error(f"Error occurred while getting IP: {e}")

return "127.0.0.1"

def main():
st.title(“IP Address Detection”)

user_ip = get_user_ip()
st.write("Your IP address:", user_ip)

if user_ip not in ALLOWED_IP_ADDRESSES:
    st.error("Access denied. Your IP address is not allowed.")
    return

st.success("Access granted. You are allowed to access the app.")

if name == “main”:
main()