OAuth 2.0 Authorization Flow
This module provides integration with requests-oauthlib for running the OAuth 2.0 Authorization Flow and acquiring user credentials.
Hereâs an example of using Flow
with the installed application authorization flow:
from google_auth_oauthlib.flow import Flow # Create the flow using the client secrets file from the Google API # Console. flow = Flow.from_client_secrets_file( 'path/to/client_secrets.json', scopes=['profile', 'email'], redirect_uri='urn:ietf:wg:oauth:2.0:oob') # Tell the user to go to the authorization URL. auth_url, _ = flow.authorization_url(prompt='consent') print('Please go to this URL: {}'.format(auth_url)) # The user will get an authorization code. This code is used to get the # access token. code = input('Enter the authorization code: ') flow.fetch_token(code=code) # You can use flow.credentials, or you can just get a requests session # using flow.authorized_session. session = flow.authorized_session() print(session.get('https://www.googleapis.com/userinfo/v2/me').json())
This particular flow can be handled entirely by using InstalledAppFlow
.
Flow
(oauth2session, client_type, client_config, redirect_uri=None, code_verifier=None, autogenerate_code_verifier=False)[source]¶
Bases: object
OAuth 2.0 Authorization Flow
This class uses a requests_oauthlib.OAuth2Session
instance at oauth2session
to perform all of the OAuth 2.0 logic. This class just provides convenience methods and sane defaults for doing Googleâs particular flavors of OAuth 2.0.
Typically youâll construct an instance of this flow using from_client_secrets_file()
and a client secrets file obtained from the Google API Console.
requests-oauthlib
.web
or installed
.Mapping
[ str
, Any
]) â The client configuration in the Google client secrets format.redirect_uri
.client_type
= None¶
The client type, either 'web'
or 'installed'
client_config
= None¶
The OAuth 2.0 client configuration.
oauth2session
= None¶
The OAuth 2.0 session.
from_client_config
(client_config, scopes, **kwargs)[source]¶
Creates a requests_oauthlib.OAuth2Session
from client configuration loaded from a Google-format client secrets file.
from_client_secrets_file
(client_secrets_file, scopes, **kwargs)[source]¶
Creates a Flow
instance from a Google client secrets file.
Sequence
[ str
]) â The list of scopes to request during the flow.Any
additional parameters passed to requests_oauthlib.OAuth2Session
The constructed Flow instance.
Return type:redirect_uri
¶
The OAuth 2.0 redirect URI. Pass-through to self.oauth2session.redirect_uri
.
Generates an authorization URL.
This is the first step in the OAuth 2.0 Authorization Flow. The userâs browser should be redirected to the returned URL.
This method calls requests_oauthlib.OAuth2Session.authorization_url()
and specifies the client configurationâs authorization URI (usually Googleâs authorization server) and specifies that âofflineâ access is desired. This is required in order to obtain a refresh token.
requests_oauthlib.OAuth2Session.authorization_url()
Returns:
Flow
instance to obtain the token, you will need to specify the state
when constructing the Flow
.
Tuple
[ str
, str
]
fetch_token
(**kwargs)[source]¶
Completes the Authorization Flow and obtains an access token.
This is the final step in the OAuth 2.0 Authorization Flow. This is called after the user consents.
This method calls requests_oauthlib.OAuth2Session.fetch_token()
and specifies the client configurationâs token URI (usually Googleâs token server).
credentials
¶
Returns credentials from the OAuth 2.0 session.
fetch_token()
must be called before accessing this. This method constructs a google.oauth2.credentials.Credentials
class using the sessionâs token and the client config.
authorized_session
()[source]¶
Returns a requests.Session
authorized with credentials.
fetch_token()
must be called before this method. This method constructs a google.auth.transport.requests.AuthorizedSession
class using this flowâs credentials
.
InstalledAppFlow
(oauth2session, client_type, client_config, redirect_uri=None, code_verifier=None, autogenerate_code_verifier=False)[source]¶
Bases: google_auth_oauthlib.flow.Flow
Authorization flow helper for installed applications.
This Flow
subclass makes it easier to perform the Installed Application Authorization Flow. This flow is useful for local development or applications that are installed on a desktop operating system.
This flow has two strategies: The console strategy provided by run_console()
and the local server strategy provided by run_local_server()
.
Example:
from google_auth_oauthlib.flow import InstalledAppFlow flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', scopes=['profile', 'email']) flow.run_local_server() session = flow.authorized_session() profile_info = session.get( 'https://www.googleapis.com/userinfo/v2/me').json() print(profile_info) # {'name': '...', 'email': '...', ...}
Note that these arenât the only two ways to accomplish the installed application flow, they are just the most common ways. You can use the Flow
class to perform the same flow with different methods of presenting the authorization URL to the user or obtaining the authorization response, such as using an embedded web view.
requests-oauthlib
.web
or installed
.Mapping
[ str
, Any
]) â The client configuration in the Google client secrets format.redirect_uri
.run_console
(authorization_prompt_message='Please visit this URL to authorize this application: {url}', authorization_code_message='Enter the authorization code: ', **kwargs)[source]¶
Run the flow using the console strategy.
The console strategy instructs the user to open the authorization URL in their browser. Once the authorization is complete the authorization server will give the user a code. The user then must copy & paste this code into the application. The code is then exchanged for a token.
Parameters:authorization_url()
.for the user.
run_local_server
(host='localhost', port=8080, authorization_prompt_message='Please visit this URL to authorize this application: {url}', success_message='The authentication flow has completed. You may close this window.', open_browser=True, **kwargs)[source]¶
Run the flow using the server strategy.
The server strategy instructs the user to open the authorization URL in their browser and will attempt to automatically open the URL for them. It will start a local web server to listen for the authorization response. Once authorization is complete the authorization server will redirect the userâs browser to the local web server. The web server will get the authorization code from the response and shutdown. The code is then exchanged for a token.
Parameters:authorization_url()
.for the user.
authorization_url
(**kwargs)¶
Generates an authorization URL.
This is the first step in the OAuth 2.0 Authorization Flow. The userâs browser should be redirected to the returned URL.
This method calls requests_oauthlib.OAuth2Session.authorization_url()
and specifies the client configurationâs authorization URI (usually Googleâs authorization server) and specifies that âofflineâ access is desired. This is required in order to obtain a refresh token.
requests_oauthlib.OAuth2Session.authorization_url()
Returns:
Flow
instance to obtain the token, you will need to specify the state
when constructing the Flow
.
Tuple
[ str
, str
]
authorized_session
()¶
Returns a requests.Session
authorized with credentials.
fetch_token()
must be called before this method. This method constructs a google.auth.transport.requests.AuthorizedSession
class using this flowâs credentials
.
credentials
¶
Returns credentials from the OAuth 2.0 session.
fetch_token()
must be called before accessing this. This method constructs a google.oauth2.credentials.Credentials
class using the sessionâs token and the client config.
fetch_token
(**kwargs)¶
Completes the Authorization Flow and obtains an access token.
This is the final step in the OAuth 2.0 Authorization Flow. This is called after the user consents.
This method calls requests_oauthlib.OAuth2Session.fetch_token()
and specifies the client configurationâs token URI (usually Googleâs token server).
from_client_config
(client_config, scopes, **kwargs)¶
Creates a requests_oauthlib.OAuth2Session
from client configuration loaded from a Google-format client secrets file.
from_client_secrets_file
(client_secrets_file, scopes, **kwargs)¶
Creates a Flow
instance from a Google client secrets file.
Sequence
[ str
]) â The list of scopes to request during the flow.Any
additional parameters passed to requests_oauthlib.OAuth2Session
The constructed Flow instance.
Return type:redirect_uri
¶
The OAuth 2.0 redirect URI. Pass-through to self.oauth2session.redirect_uri
.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4