Package: oryks-google-oauth
google-oauthOverviewA python library for authenticating requests for various google services includinggmail,youtube,driveandcalendar.RequirementsPython 3.10+Works on Linux, Windows, macOS, BSDGetting startedGetting an access keyTo get started with this library, you need a google account in order to obtain access credentials. First, pick a service that you want to use, which could be:
-gmail-youtube-drive-calendarInstalling the libraryThe next step is to instal the library from pypi:pipinstalloryks-google-oauthAuthorizing a requestIn this case we will useyoutubeas the service. We will authorize a request to searchyoutubeforpython programming videos:fromoryks_google_oauthimportGoogleOAuth,YouTubeScopesclient_secret_file:str='client_secret.json'api_service_name:str='youtube'api_version:str='v3'credentials_dir:str='.youtube'scopes:list[str]=[YouTubeScopes.youtube.value]oauth:GoogleOAuth=GoogleOAuth(secrets_file=secret_file,scopes=scopes,api_service_name=api_service_name,api_version=api_version,credentials_dir=credentials_dir)youtube_client=oauth.authenticate_google_server()youtube_find_request=youtube_client.search().list(q='python programming videos',part='id, snippet')print(youtube_find_request.execute())Using the Flask FrameworkCurrently, this library does not intergrate with web frameworks like Flask and fastAPI. To use flask, we use the underlying library:fromflaskimportFlask,url_for,redirect,request,sessionfromgoogle_auth_oauthlib.flowimportFlowfromgoogle.oauth2.credentialsimportCredentialsimportosCLIENT_SECRETS_FILE="client_secret.json"SCOPES=['https://www.googleapis.com/auth/youtube.force-ssl','https://www.googleapis.com/auth/youtube']API_SERVICE_NAME='youtube'API_VERSION='v3'app:Flask=Flask(__name__)app.secret_key='secret-key'@app.route('/')defhome():ifnotsession['credentials']:redirect(url_for('authorize'))return'Home'@app.route('/authorize')defauthorize():flow=Flow.from_client_secrets_file(CLIENT_SECRETS_FILE,scopes=SCOPES)flow.redirect_uri=url_for('oauth2callback',_external=True)authorization_url,state=flow.authorization_url(access_type='offline',include_granted_scopes='true')session['state']=statereturnredirect(authorization_url)defcredentials_to_dict(credentials:Credentials)->dict:return{'token':credentials.token,'refresh_token':credentials.refresh_token,'token_uri':credentials.token_uri,'client_id':credentials.client_id,'client_secret':credentials.client_secret,'scopes':credentials.scopes}@app.route('/oauth2callback')defoauth2callback():state=session['state']flow=Flow.from_client_secrets_file(CLIENT_SECRETS_FILE,scopes=SCOPES,state=state)flow.redirect_uri=url_for('oauth2callback',_external=True)authorization_response=request.urlflow.fetch_token(authorization_response=authorization_response)credentials:Credentials=flow.credentialssession['credentials']=credentials_to_dict(credentials)returnredirect('/')if__name__=='__main__':os.environ['OAUTHLIB_INSECURE_TRANSPORT']='1'app.run('localhost',5000,debug=True)How to ContributeTo contribute, chack out thecontribution guideline.LicenseThe API uses anMIT LicenseDeveloperLyle Okoth –@lyleokothon twitterlyle okothon mediumMy email islyceokoth@gmail.comHere is myGitHub ProfileYou can also find me onLinkedin