[pixiv] use refresh_token based authentication

The first login will still use username and password, but everything
afterwards will use the refresh_token obtained from that.

This will prevent pixiv from sending a "New login to pixiv" email every
time a new access_token is requested.
pull/133/head
Mike Fährmann 6 years ago
parent 2221cf97ff
commit 8faf03ed84
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -432,23 +432,31 @@ class PixivAppAPI():
@cache(maxage=3590, keyarg=1) @cache(maxage=3590, keyarg=1)
def _login_impl(self, username, password): def _login_impl(self, username, password):
self.log.info("Logging in as %s", username)
url = "https://oauth.secure.pixiv.net/auth/token" url = "https://oauth.secure.pixiv.net/auth/token"
data = { data = {
"client_id": self.client_id, "client_id": self.client_id,
"client_secret": self.client_secret, "client_secret": self.client_secret,
"grant_type": "password",
"username": username,
"password": password,
"get_secure_url": 1, "get_secure_url": 1,
} }
refresh_token = _refresh_token_cache(username)
if refresh_token:
self.log.info("Refreshing access token")
data["grant_type"] = "refresh_token"
data["refresh_token"] = refresh_token
else:
self.log.info("Logging in as %s", username)
data["grant_type"] = "password"
data["username"] = username
data["password"] = password
response = self.session.post(url, data=data) response = self.session.post(url, data=data)
if response.status_code >= 400: if response.status_code >= 400:
raise exception.AuthenticationError() raise exception.AuthenticationError()
data = response.json()["response"] data = response.json()["response"]
if not refresh_token:
_refresh_token_cache.update(username, data["refresh_token"])
return data["user"], "Bearer " + data["access_token"] return data["user"], "Bearer " + data["access_token"]
def illust_detail(self, illust_id): def illust_detail(self, illust_id):
@ -506,3 +514,8 @@ class PixivAppAPI():
return return
query = data["next_url"].rpartition("?")[2] query = data["next_url"].rpartition("?")[2]
params = text.parse_query(query) params = text.parse_query(query)
@cache(maxage=365*24*60*60, keyarg=0)
def _refresh_token_cache(username):
return None

Loading…
Cancel
Save