Skip to content

Commit

Permalink
[pixiv] use refresh_token based authentication
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mikf committed Oct 12, 2018
1 parent 2221cf9 commit 8faf03e
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions gallery_dl/extractor/pixiv.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,23 +432,31 @@ def login(self):

@cache(maxage=3590, keyarg=1)
def _login_impl(self, username, password):
self.log.info("Logging in as %s", username)

url = "https://oauth.secure.pixiv.net/auth/token"
data = {
"client_id": self.client_id,
"client_secret": self.client_secret,
"grant_type": "password",
"username": username,
"password": password,
"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)
if response.status_code >= 400:
raise exception.AuthenticationError()

data = response.json()["response"]
if not refresh_token:
_refresh_token_cache.update(username, data["refresh_token"])
return data["user"], "Bearer " + data["access_token"]

def illust_detail(self, illust_id):
Expand Down Expand Up @@ -506,3 +514,8 @@ def _pagination(self, endpoint, params):
return
query = data["next_url"].rpartition("?")[2]
params = text.parse_query(query)


@cache(maxage=365*24*60*60, keyarg=0)
def _refresh_token_cache(username):
return None

0 comments on commit 8faf03e

Please sign in to comment.