From 668b6a4495e3da87f9166edcfdc4e23060ee54f7 Mon Sep 17 00:00:00 2001 From: Tom Dryer Date: Sun, 6 Sep 2020 18:38:19 -0700 Subject: [PATCH] Fix random 401 errors Google appear to be randomly routing requests to a new backend that does not support quoted cookie values. Until aiohttp gets an option to disable quoting cookie values, we need to serialize the cookie header ourselves as a workaround. Fixes #498. --- hangups/http_utils.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/hangups/http_utils.py b/hangups/http_utils.py index dd196e88..2f2c5ca3 100644 --- a/hangups/http_utils.py +++ b/hangups/http_utils.py @@ -32,8 +32,8 @@ class Session(object): def __init__(self, cookies, proxy=None): self._proxy = proxy timeout = aiohttp.ClientTimeout(connect=CONNECT_TIMEOUT) - self._session = aiohttp.ClientSession(cookies=cookies, - timeout=timeout) + self._session = aiohttp.ClientSession(timeout=timeout) + self._cookies = cookies sapisid = cookies['SAPISID'] self._authorization_headers = _get_authorization_headers(sapisid) @@ -116,6 +116,13 @@ def fetch_raw(self, method, url, params=None, headers=None, data=None): headers = headers or {} headers.update(self._authorization_headers) + # Workaround for #498: Serialize the cookie header manually to prevent + # aiohttp from quoting cookie values, which the server does not + # support. + headers['Cookie'] = '; '.join( + '{}={}'.format(name, value) + for name, value in sorted(self._cookies.items()) + ) return self._session.request( method, url, params=params, headers=headers, data=data, proxy=self._proxy