|
7 | 7 |
|
8 | 8 | from ..errors import APIError, AuthError, SessionError |
9 | 9 | from .session import Session |
10 | | -from ..endpoints import GOOGLE_BASE_URL, PASSWORD_VERIFY |
| 10 | +from ..endpoints import GOOGLE_BASE_URL, PASSWORD_VERIFY, GOOGLE_TOKEN_BASE_URL, TOKEN |
11 | 11 | from .functions import HEADERS |
12 | 12 | from .api_wrapper import APIWrapper |
13 | 13 |
|
@@ -61,7 +61,9 @@ async def async_update_access_token(self) -> bool: |
61 | 61 |
|
62 | 62 | try: |
63 | 63 | _LOGGER.debug('Updating access token') |
64 | | - access_token_updated: bool = await self.__update_access_token() |
| 64 | + access_token_updated: bool = await self.__update_access_token( |
| 65 | + refresh=self.access_token_expired() |
| 66 | + ) |
65 | 67 |
|
66 | 68 | _LOGGER.debug( |
67 | 69 | "Updated access token. New expiration: %s", |
@@ -90,23 +92,43 @@ async def async_update_access_token(self) -> bool: |
90 | 92 |
|
91 | 93 | async def __update_access_token(self, refresh: bool = False) -> bool: |
92 | 94 | return_value = False |
| 95 | + id_token_response = 'idToken' |
| 96 | + refresh_token_response = 'refreshToken' |
| 97 | + expires_in_response = 'expiresIn' |
93 | 98 |
|
94 | 99 | try: |
95 | 100 | wrapper = APIWrapper(session=self._session) |
96 | | - response = await wrapper.post( |
97 | | - url=f"{GOOGLE_BASE_URL}{PASSWORD_VERIFY}", |
98 | | - body={"email": self.email, "returnSecureToken": True, "password": self.password}, |
99 | | - headers=HEADERS, |
100 | | - exception_class=AuthError) |
| 101 | + |
| 102 | + if refresh: |
| 103 | + _LOGGER.debug('Refreshing access token') |
| 104 | + headers = HEADERS.copy() |
| 105 | + headers["Content-type"] = 'application/x-www-form-urlencoded' |
| 106 | + |
| 107 | + id_token_response = 'id_token' |
| 108 | + refresh_token_response = 'refresh_token' |
| 109 | + expires_in_response = 'expires_in' |
| 110 | + |
| 111 | + response = await wrapper.post_form_data( |
| 112 | + url=f"{GOOGLE_TOKEN_BASE_URL}{TOKEN}", |
| 113 | + body=f"grant_type=refresh_token&refresh_token={self.refresh_token}", |
| 114 | + headers=headers, |
| 115 | + exception_class=AuthError) |
| 116 | + else: |
| 117 | + _LOGGER.debug('Getting a new access token') |
| 118 | + response = await wrapper.post( |
| 119 | + url=f"{GOOGLE_BASE_URL}{PASSWORD_VERIFY}", |
| 120 | + body={"email": self.email, "returnSecureToken": True, "password": self.password}, |
| 121 | + headers=HEADERS, |
| 122 | + exception_class=AuthError) |
101 | 123 |
|
102 | 124 | if response.status != 200: |
103 | 125 | await self.__handle_response_error(response, AuthError) |
104 | 126 |
|
105 | 127 | json = await response.json() |
106 | | - self.access_token = json["idToken"] |
107 | | - self.refresh_token = json["refreshToken"] |
| 128 | + self.access_token = json[id_token_response] |
| 129 | + self.refresh_token = json[refresh_token_response] |
108 | 130 | self.access_token_expiry = datetime.now() + timedelta( |
109 | | - seconds=int(json["expiresIn"]) - 10 |
| 131 | + seconds=int(json[expires_in_response]) - 10 |
110 | 132 | ) |
111 | 133 | return_value = True |
112 | 134 |
|
@@ -134,4 +156,7 @@ async def __handle_response_error(self, response, error_class): |
134 | 156 | status = response.status |
135 | 157 | response = await response.text() |
136 | 158 |
|
| 159 | + if self._http_debug: |
| 160 | + _LOGGER.debug(response) |
| 161 | + |
137 | 162 | raise error_class(status, response) |
0 commit comments