How to extract access token from redirect URL without full libraries?

32 views Asked by At
import urequests as requests

# Google OAuth 2.0 authorization endpoint
AUTH_URI = "https://accounts.google.com/o/oauth2/auth"

# Your client ID and scope
CLIENT_ID = "220352837780-bf38n8g0vp9p13gr6j6j0vgq2q5u0996.apps.googleusercontent.com"
SCOPE = "https://www.googleapis.com/auth/calendar.readonly"

# Perform OAuth 2.0 authorization flow
def authorize():
    redirect_uri = "https://sites.google.com/view/redirect-url-project3"  
    auth_url = "{}?client_id={}&response_type=token&scope={}&redirect_uri={}".format(AUTH_URI, CLIENT_ID, SCOPE, redirect_uri)
    print("Open this URL in your browser and grant permission:", auth_url)
    print(redirect_uri)
    return auth_url

def extract_access_token(auth_url):
    # Find the start and end index of the access token in the auth_url
    start_index = auth_url.find("access_token=")
    if start_index != -1:
        start_index += len("access_token=")
        end_index = auth_url.find("&", start_index)
        if end_index == -1:
            end_index = len(auth_url)
        access_token = auth_url[start_index:end_index]
        print("Access Token:", access_token)
        return access_token
    else:
        print("Access token not found in redirect URI.")
        return None

# Main function
def main():
    auth_url = authorize()
    access_token = extract_access_token(auth_url)

    # Use the access token to make authenticated requests to the Google Calendar API
    if access_token:
        headers = {"Authorization": "Bearer " + access_token}
        response = requests.get("YOUR_CALENDAR_API_ENDPOINT", headers=headers)
        if response.status_code == 200:
            print("Calendar API response:", response.json())
        else:
            print("Failed to fetch calendar events:", response.text)

if __name__ == "__main__":
    main()


I have the redirect url set up and i am getting the access token in the redirect url, but some how in my code i am not able to get the same thing

since I am using raspberry pico W I do not have access to many libraries in micropython, I need solution to this problem

0

There are 0 answers