66from googleapiclient import discovery
77from google_auth_oauthlib .flow import InstalledAppFlow
88from google .auth .transport .requests import Request
9- from google .oauth2 .credentials import Credentials
9+ from google .auth .credentials import Credentials
1010
1111
1212class AuthenticatedService :
@@ -24,7 +24,8 @@ def __init__(
2424 save_token : bool = True ,
2525 read_only : bool = False ,
2626 authentication_flow_host : str = 'localhost' ,
27- authentication_flow_port : int = 8080
27+ authentication_flow_port : int = 8080 ,
28+ authentication_flow_bind_addr : str = None
2829 ):
2930 """
3031 Specify ``credentials`` to use in requests or ``credentials_path`` and ``token_path`` to get credentials from.
@@ -49,6 +50,9 @@ def __init__(
4950 Host to receive response during authentication flow
5051 :param authentication_flow_port:
5152 Port to receive response during authentication flow
53+ :param authentication_flow_bind_addr:
54+ Optional IP address for the redirect server to listen on when it is not the same as host
55+ (e.g. in a container)
5256 """
5357
5458 if credentials :
@@ -66,7 +70,8 @@ def __init__(
6670 scopes ,
6771 save_token ,
6872 authentication_flow_host ,
69- authentication_flow_port
73+ authentication_flow_port ,
74+ authentication_flow_bind_addr
7075 )
7176
7277 self .service = discovery .build ('calendar' , 'v3' , credentials = self .credentials )
@@ -75,7 +80,7 @@ def __init__(
7580 def _ensure_refreshed (
7681 credentials : Credentials
7782 ) -> Credentials :
78- if not credentials .valid and credentials .expired and credentials . refresh_token :
83+ if not credentials .valid and credentials .expired :
7984 credentials .refresh (Request ())
8085 return credentials
8186
@@ -87,7 +92,8 @@ def _get_credentials(
8792 scopes : List [str ],
8893 save_token : bool ,
8994 host : str ,
90- port : int
95+ port : int ,
96+ bind_addr : str
9197 ) -> Credentials :
9298 credentials = None
9399
@@ -101,7 +107,7 @@ def _get_credentials(
101107 else :
102108 credentials_path = os .path .join (credentials_dir , credentials_file )
103109 flow = InstalledAppFlow .from_client_secrets_file (credentials_path , scopes )
104- credentials = flow .run_local_server (host = host , port = port )
110+ credentials = flow .run_local_server (host = host , port = port , bind_addr = bind_addr )
105111
106112 if save_token :
107113 with open (token_path , 'wb' ) as token_file :
0 commit comments