2017-06-08 16:17:13 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-02-12 18:03:06 +01:00
|
|
|
# Copyright 2017-2021 Mike Fährmann
|
2017-06-08 16:17:13 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
2020-02-09 13:45:44 +01:00
|
|
|
"""Utility classes to setup OAuth and link accounts to gallery-dl"""
|
2017-06-08 16:17:13 +02:00
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2021-02-15 02:32:33 +01:00
|
|
|
from . import deviantart, flickr, mastodon, pixiv, reddit, smugmug, tumblr
|
2020-05-19 21:42:11 +02:00
|
|
|
from .. import text, oauth, util, config, exception
|
2019-01-19 14:28:59 +01:00
|
|
|
from ..cache import cache
|
2017-06-08 16:17:13 +02:00
|
|
|
import urllib.parse
|
2021-02-12 18:03:06 +01:00
|
|
|
import hashlib
|
|
|
|
import base64
|
2017-06-08 16:17:13 +02:00
|
|
|
|
2019-12-02 18:05:36 +01:00
|
|
|
REDIRECT_URI_LOCALHOST = "http://localhost:6414/"
|
|
|
|
REDIRECT_URI_HTTPS = "https://mikf.github.io/gallery-dl/oauth-redirect.html"
|
|
|
|
|
2017-06-08 16:17:13 +02:00
|
|
|
|
|
|
|
class OAuthBase(Extractor):
|
2017-06-12 09:36:14 +02:00
|
|
|
"""Base class for OAuth Helpers"""
|
2017-06-08 16:17:13 +02:00
|
|
|
category = "oauth"
|
2019-12-02 18:05:36 +01:00
|
|
|
redirect_uri = REDIRECT_URI_LOCALHOST
|
2017-06-08 16:17:13 +02:00
|
|
|
|
2017-12-18 00:12:08 +01:00
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
Extractor.__init__(self, match)
|
2017-06-08 16:17:13 +02:00
|
|
|
self.client = None
|
2020-05-25 22:19:58 +02:00
|
|
|
self.cache = config.get(("extractor", self.category), "cache", True)
|
2017-06-08 16:17:13 +02:00
|
|
|
|
2018-01-16 17:39:46 +01:00
|
|
|
def oauth_config(self, key, default=None):
|
2021-08-17 22:24:20 +02:00
|
|
|
value = config.interpolate(("extractor", self.subcategory), key)
|
|
|
|
return value if value is not None else default
|
2018-01-16 17:39:46 +01:00
|
|
|
|
2017-06-08 16:17:13 +02:00
|
|
|
def recv(self):
|
2017-06-12 09:36:14 +02:00
|
|
|
"""Open local HTTP server and recv callback parameters"""
|
|
|
|
import socket
|
2017-06-08 16:17:13 +02:00
|
|
|
print("Waiting for response. (Cancel with Ctrl+c)")
|
|
|
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2020-02-09 13:45:44 +01:00
|
|
|
server.bind(("localhost", self.config("port", 6414)))
|
2017-06-08 16:17:13 +02:00
|
|
|
server.listen(1)
|
2017-06-14 15:27:16 +02:00
|
|
|
|
|
|
|
# workaround for ctrl+c not working during server.accept on Windows
|
2020-05-19 21:42:11 +02:00
|
|
|
if util.WINDOWS:
|
2017-06-14 15:27:16 +02:00
|
|
|
server.settimeout(1.0)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
self.client = server.accept()[0]
|
|
|
|
break
|
|
|
|
except socket.timeout:
|
|
|
|
pass
|
2017-06-08 16:17:13 +02:00
|
|
|
server.close()
|
|
|
|
|
|
|
|
data = self.client.recv(1024).decode()
|
|
|
|
path = data.split(" ", 2)[1]
|
2018-04-16 19:43:27 +02:00
|
|
|
return text.parse_query(path.partition("?")[2])
|
2017-06-08 16:17:13 +02:00
|
|
|
|
|
|
|
def send(self, msg):
|
2017-06-12 09:36:14 +02:00
|
|
|
"""Send 'msg' to the socket opened in 'recv()'"""
|
2017-06-08 16:17:13 +02:00
|
|
|
print(msg)
|
|
|
|
self.client.send(b"HTTP/1.1 200 OK\r\n\r\n" + msg.encode())
|
|
|
|
self.client.close()
|
|
|
|
|
2021-02-12 18:03:06 +01:00
|
|
|
def open(self, url, params, recv=None):
|
2017-06-12 09:36:14 +02:00
|
|
|
"""Open 'url' in browser amd return response parameters"""
|
|
|
|
import webbrowser
|
|
|
|
url += "?" + urllib.parse.urlencode(params)
|
2018-04-16 19:43:27 +02:00
|
|
|
if not self.config("browser", True) or not webbrowser.open(url):
|
2017-06-12 09:36:14 +02:00
|
|
|
print("Please open this URL in your browser:")
|
|
|
|
print(url, end="\n\n", flush=True)
|
2021-02-12 18:03:06 +01:00
|
|
|
return (recv or self.recv)()
|
2017-06-12 09:36:14 +02:00
|
|
|
|
2021-06-05 14:28:26 +02:00
|
|
|
def error(self, msg):
|
|
|
|
return self.send("Remote server reported an error:\n\n" + str(msg))
|
|
|
|
|
2018-01-11 14:11:37 +01:00
|
|
|
def _oauth1_authorization_flow(
|
|
|
|
self, request_token_url, authorize_url, access_token_url):
|
|
|
|
"""Perform the OAuth 1.0a authorization flow"""
|
2018-04-16 19:43:27 +02:00
|
|
|
# get a request token
|
2018-01-11 14:11:37 +01:00
|
|
|
params = {"oauth_callback": self.redirect_uri}
|
|
|
|
data = self.session.get(request_token_url, params=params).text
|
|
|
|
|
|
|
|
data = text.parse_query(data)
|
2018-05-10 18:26:10 +02:00
|
|
|
self.session.auth.token_secret = data["oauth_token_secret"]
|
2018-01-11 14:11:37 +01:00
|
|
|
|
2018-04-16 19:43:27 +02:00
|
|
|
# get the user's authorization
|
2018-05-10 18:26:10 +02:00
|
|
|
params = {"oauth_token": data["oauth_token"], "perms": "read"}
|
2018-01-11 14:11:37 +01:00
|
|
|
data = self.open(authorize_url, params)
|
|
|
|
|
2018-04-16 19:43:27 +02:00
|
|
|
# exchange the request token for an access token
|
2018-01-11 14:11:37 +01:00
|
|
|
data = self.session.get(access_token_url, params=data).text
|
|
|
|
data = text.parse_query(data)
|
2020-05-31 15:34:05 +02:00
|
|
|
token = data["oauth_token"]
|
|
|
|
token_secret = data["oauth_token_secret"]
|
2018-01-11 14:11:37 +01:00
|
|
|
|
2020-05-25 22:19:58 +02:00
|
|
|
# write to cache
|
|
|
|
if self.cache:
|
|
|
|
key = (self.subcategory, self.session.auth.consumer_key)
|
2020-05-31 15:34:05 +02:00
|
|
|
oauth._token_cache.update(key, (token, token_secret))
|
2020-05-25 22:19:58 +02:00
|
|
|
self.log.info("Writing tokens to cache")
|
|
|
|
|
2020-05-31 15:34:05 +02:00
|
|
|
# display tokens
|
|
|
|
self.send(self._generate_message(
|
|
|
|
("access-token", "access-token-secret"),
|
|
|
|
(token, token_secret),
|
|
|
|
))
|
|
|
|
|
2017-12-18 00:12:08 +01:00
|
|
|
def _oauth2_authorization_code_grant(
|
2021-01-31 01:38:23 +01:00
|
|
|
self, client_id, client_secret, auth_url, token_url, *,
|
2019-01-19 14:28:59 +01:00
|
|
|
scope="read", key="refresh_token", auth=True,
|
2021-01-31 01:38:23 +01:00
|
|
|
cache=None, instance=None):
|
2017-12-18 00:12:08 +01:00
|
|
|
"""Perform an OAuth2 authorization code grant"""
|
2017-06-08 16:17:13 +02:00
|
|
|
|
2018-04-16 19:43:27 +02:00
|
|
|
state = "gallery-dl_{}_{}".format(
|
|
|
|
self.subcategory,
|
2018-05-10 18:26:10 +02:00
|
|
|
oauth.nonce(8),
|
2018-04-16 19:43:27 +02:00
|
|
|
)
|
2017-06-08 16:17:13 +02:00
|
|
|
|
2017-12-18 00:12:08 +01:00
|
|
|
auth_params = {
|
2021-01-31 01:38:23 +01:00
|
|
|
"client_id" : client_id,
|
2017-06-08 16:17:13 +02:00
|
|
|
"response_type": "code",
|
2021-01-31 01:38:23 +01:00
|
|
|
"state" : state,
|
|
|
|
"redirect_uri" : self.redirect_uri,
|
|
|
|
"duration" : "permanent",
|
|
|
|
"scope" : scope,
|
2017-06-08 16:17:13 +02:00
|
|
|
}
|
|
|
|
|
2018-04-16 19:43:27 +02:00
|
|
|
# receive an authorization code
|
2017-12-18 00:12:08 +01:00
|
|
|
params = self.open(auth_url, auth_params)
|
2017-06-08 16:17:13 +02:00
|
|
|
|
2018-04-16 19:43:27 +02:00
|
|
|
# check authorization response
|
2017-12-18 00:12:08 +01:00
|
|
|
if state != params.get("state"):
|
2017-06-08 16:17:13 +02:00
|
|
|
self.send("'state' mismatch: expected {}, got {}.".format(
|
2018-04-16 19:43:27 +02:00
|
|
|
state, params.get("state")
|
|
|
|
))
|
2017-06-08 16:17:13 +02:00
|
|
|
return
|
|
|
|
if "error" in params:
|
2021-06-05 14:28:26 +02:00
|
|
|
return self.error(params)
|
2017-06-08 16:17:13 +02:00
|
|
|
|
2018-04-16 19:43:27 +02:00
|
|
|
# exchange the authorization code for a token
|
2017-06-08 16:17:13 +02:00
|
|
|
data = {
|
2021-01-31 01:38:23 +01:00
|
|
|
"grant_type" : "authorization_code",
|
|
|
|
"code" : params["code"],
|
2017-06-08 16:17:13 +02:00
|
|
|
"redirect_uri": self.redirect_uri,
|
|
|
|
}
|
2018-04-16 19:43:27 +02:00
|
|
|
|
|
|
|
if auth:
|
|
|
|
auth = (client_id, client_secret)
|
|
|
|
else:
|
|
|
|
auth = None
|
|
|
|
data["client_id"] = client_id
|
|
|
|
data["client_secret"] = client_secret
|
|
|
|
|
2017-12-18 00:12:08 +01:00
|
|
|
data = self.session.post(token_url, data=data, auth=auth).json()
|
2017-06-08 16:17:13 +02:00
|
|
|
|
2017-12-18 00:12:08 +01:00
|
|
|
# check token response
|
2017-06-08 16:17:13 +02:00
|
|
|
if "error" in data:
|
2021-06-05 14:28:26 +02:00
|
|
|
return self.error(data)
|
2017-12-18 00:12:08 +01:00
|
|
|
|
2021-01-31 01:38:23 +01:00
|
|
|
token = data[key]
|
|
|
|
token_name = key.replace("_", "-")
|
|
|
|
|
2020-02-23 21:11:19 +01:00
|
|
|
# write to cache
|
2020-05-25 22:19:58 +02:00
|
|
|
if self.cache and cache:
|
2021-01-31 01:38:23 +01:00
|
|
|
cache.update(instance or ("#" + str(client_id)), token)
|
|
|
|
self.log.info("Writing '%s' to cache", token_name)
|
2020-02-23 21:11:19 +01:00
|
|
|
|
2020-05-31 15:34:05 +02:00
|
|
|
# display token
|
2021-01-31 01:38:23 +01:00
|
|
|
self.send(self._generate_message(
|
|
|
|
(token_name,), (token,),
|
|
|
|
))
|
2020-05-31 15:34:05 +02:00
|
|
|
|
|
|
|
def _generate_message(self, names, values):
|
2020-09-29 21:25:24 +02:00
|
|
|
_vh, _va, _is, _it = (
|
|
|
|
("This value has", "this value", "is", "it")
|
|
|
|
if len(names) == 1 else
|
|
|
|
("These values have", "these values", "are", "them")
|
|
|
|
)
|
2020-05-31 15:34:05 +02:00
|
|
|
|
|
|
|
msg = "\nYour {} {}\n\n{}\n\n".format(
|
|
|
|
" and ".join("'" + n + "'" for n in names),
|
|
|
|
_is,
|
|
|
|
"\n".join(values),
|
|
|
|
)
|
|
|
|
|
2020-09-29 21:25:24 +02:00
|
|
|
opt = self.oauth_config(names[0])
|
|
|
|
if self.cache and (opt is None or opt == "cache"):
|
|
|
|
msg += _vh + " been cached and will automatically be used."
|
2020-05-31 15:34:05 +02:00
|
|
|
else:
|
|
|
|
msg += "Put " + _va + " into your configuration file as \n"
|
|
|
|
msg += " and\n".join(
|
|
|
|
"'extractor." + self.subcategory + "." + n + "'"
|
|
|
|
for n in names
|
|
|
|
)
|
2020-09-29 21:25:24 +02:00
|
|
|
if self.cache:
|
|
|
|
msg += (
|
|
|
|
"\nor set\n'extractor.{}.{}' to \"cache\""
|
|
|
|
.format(self.subcategory, names[0])
|
|
|
|
)
|
|
|
|
msg += "\nto use {}.".format(_it)
|
2020-05-31 15:34:05 +02:00
|
|
|
|
|
|
|
return msg
|
|
|
|
|
2017-12-18 00:12:08 +01:00
|
|
|
|
|
|
|
class OAuthDeviantart(OAuthBase):
|
|
|
|
subcategory = "deviantart"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = "oauth:deviantart$"
|
2019-12-02 18:05:36 +01:00
|
|
|
redirect_uri = REDIRECT_URI_HTTPS
|
2017-12-18 00:12:08 +01:00
|
|
|
|
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
|
|
|
|
|
|
|
self._oauth2_authorization_code_grant(
|
2018-01-16 17:39:46 +01:00
|
|
|
self.oauth_config(
|
2020-04-04 20:28:25 +02:00
|
|
|
"client-id", deviantart.DeviantartOAuthAPI.CLIENT_ID),
|
2018-01-16 17:39:46 +01:00
|
|
|
self.oauth_config(
|
2020-04-04 20:28:25 +02:00
|
|
|
"client-secret", deviantart.DeviantartOAuthAPI.CLIENT_SECRET),
|
2017-12-18 00:12:08 +01:00
|
|
|
"https://www.deviantart.com/oauth2/authorize",
|
|
|
|
"https://www.deviantart.com/oauth2/token",
|
2021-08-29 21:06:48 +02:00
|
|
|
scope="browse user.manage",
|
2020-02-23 21:11:19 +01:00
|
|
|
cache=deviantart._refresh_token_cache,
|
2017-12-18 00:12:08 +01:00
|
|
|
)
|
2017-06-08 16:17:13 +02:00
|
|
|
|
|
|
|
|
2017-06-12 09:36:14 +02:00
|
|
|
class OAuthFlickr(OAuthBase):
|
|
|
|
subcategory = "flickr"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = "oauth:flickr$"
|
2019-12-02 18:05:36 +01:00
|
|
|
redirect_uri = REDIRECT_URI_HTTPS
|
2017-06-12 09:36:14 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2017-12-18 00:12:08 +01:00
|
|
|
OAuthBase.__init__(self, match)
|
2018-05-10 18:26:10 +02:00
|
|
|
self.session = oauth.OAuth1Session(
|
2018-01-16 17:39:46 +01:00
|
|
|
self.oauth_config("api-key", flickr.FlickrAPI.API_KEY),
|
|
|
|
self.oauth_config("api-secret", flickr.FlickrAPI.API_SECRET),
|
2017-06-14 16:11:18 +02:00
|
|
|
)
|
2017-06-12 09:36:14 +02:00
|
|
|
|
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
|
|
|
|
2018-01-11 14:11:37 +01:00
|
|
|
self._oauth1_authorization_flow(
|
|
|
|
"https://www.flickr.com/services/oauth/request_token",
|
|
|
|
"https://www.flickr.com/services/oauth/authorize",
|
|
|
|
"https://www.flickr.com/services/oauth/access_token",
|
|
|
|
)
|
2017-06-12 09:36:14 +02:00
|
|
|
|
|
|
|
|
2018-01-11 14:11:37 +01:00
|
|
|
class OAuthReddit(OAuthBase):
|
|
|
|
subcategory = "reddit"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = "oauth:reddit$"
|
2017-06-12 09:36:14 +02:00
|
|
|
|
2018-01-11 14:11:37 +01:00
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
2017-06-12 09:36:14 +02:00
|
|
|
|
2018-01-11 14:11:37 +01:00
|
|
|
self.session.headers["User-Agent"] = reddit.RedditAPI.USER_AGENT
|
|
|
|
self._oauth2_authorization_code_grant(
|
2018-01-16 17:39:46 +01:00
|
|
|
self.oauth_config("client-id", reddit.RedditAPI.CLIENT_ID),
|
2018-01-11 14:11:37 +01:00
|
|
|
"",
|
|
|
|
"https://www.reddit.com/api/v1/authorize",
|
|
|
|
"https://www.reddit.com/api/v1/access_token",
|
2019-09-27 17:38:55 +02:00
|
|
|
scope="read history",
|
2020-05-25 22:19:58 +02:00
|
|
|
cache=reddit._refresh_token_cache,
|
2018-01-11 14:11:37 +01:00
|
|
|
)
|
2017-06-12 09:36:14 +02:00
|
|
|
|
|
|
|
|
2018-05-10 18:58:05 +02:00
|
|
|
class OAuthSmugmug(OAuthBase):
|
|
|
|
subcategory = "smugmug"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = "oauth:smugmug$"
|
2018-05-10 18:58:05 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
OAuthBase.__init__(self, match)
|
|
|
|
self.session = oauth.OAuth1Session(
|
|
|
|
self.oauth_config("api-key", smugmug.SmugmugAPI.API_KEY),
|
|
|
|
self.oauth_config("api-secret", smugmug.SmugmugAPI.API_SECRET),
|
|
|
|
)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
|
|
|
|
|
|
|
self._oauth1_authorization_flow(
|
|
|
|
"https://api.smugmug.com/services/oauth/1.0a/getRequestToken",
|
|
|
|
"https://api.smugmug.com/services/oauth/1.0a/authorize",
|
|
|
|
"https://api.smugmug.com/services/oauth/1.0a/getAccessToken",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-01-11 14:11:37 +01:00
|
|
|
class OAuthTumblr(OAuthBase):
|
|
|
|
subcategory = "tumblr"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = "oauth:tumblr$"
|
2018-01-11 14:11:37 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
OAuthBase.__init__(self, match)
|
2018-05-10 18:26:10 +02:00
|
|
|
self.session = oauth.OAuth1Session(
|
2018-01-16 17:39:46 +01:00
|
|
|
self.oauth_config("api-key", tumblr.TumblrAPI.API_KEY),
|
|
|
|
self.oauth_config("api-secret", tumblr.TumblrAPI.API_SECRET),
|
2018-01-11 14:11:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
|
|
|
|
|
|
|
self._oauth1_authorization_flow(
|
|
|
|
"https://www.tumblr.com/oauth/request_token",
|
|
|
|
"https://www.tumblr.com/oauth/authorize",
|
|
|
|
"https://www.tumblr.com/oauth/access_token",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-01-19 14:28:59 +01:00
|
|
|
class OAuthMastodon(OAuthBase):
|
|
|
|
subcategory = "mastodon"
|
2020-10-22 23:12:59 +02:00
|
|
|
pattern = "oauth:mastodon:(?:https?://)?([^/?#]+)"
|
2019-01-19 14:28:59 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
OAuthBase.__init__(self, match)
|
|
|
|
self.instance = match.group(1)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
|
|
|
|
2021-01-28 02:20:12 +01:00
|
|
|
for application in mastodon.INSTANCES.values():
|
|
|
|
if self.instance == application["root"].partition("://")[2]:
|
|
|
|
break
|
|
|
|
else:
|
2019-01-19 14:28:59 +01:00
|
|
|
application = self._register(self.instance)
|
|
|
|
|
|
|
|
self._oauth2_authorization_code_grant(
|
|
|
|
application["client-id"],
|
|
|
|
application["client-secret"],
|
|
|
|
"https://{}/oauth/authorize".format(self.instance),
|
|
|
|
"https://{}/oauth/token".format(self.instance),
|
2021-01-31 01:38:23 +01:00
|
|
|
instance=self.instance,
|
2019-01-19 14:28:59 +01:00
|
|
|
key="access_token",
|
2021-01-31 01:38:23 +01:00
|
|
|
cache=mastodon._access_token_cache,
|
2019-01-19 14:28:59 +01:00
|
|
|
)
|
|
|
|
|
2019-03-14 22:21:49 +01:00
|
|
|
@cache(maxage=10*365*24*3600, keyarg=1)
|
2019-01-19 14:28:59 +01:00
|
|
|
def _register(self, instance):
|
|
|
|
self.log.info("Registering application for '%s'", instance)
|
|
|
|
|
|
|
|
url = "https://{}/api/v1/apps".format(instance)
|
|
|
|
data = {
|
|
|
|
"client_name": "gdl:" + oauth.nonce(8),
|
|
|
|
"redirect_uris": self.redirect_uri,
|
|
|
|
"scopes": "read",
|
|
|
|
}
|
|
|
|
data = self.session.post(url, data=data).json()
|
|
|
|
|
|
|
|
if "client_id" not in data or "client_secret" not in data:
|
2019-10-28 16:06:36 +01:00
|
|
|
raise exception.StopExtraction(
|
|
|
|
"Failed to register new application: '%s'", data)
|
2019-01-19 14:28:59 +01:00
|
|
|
|
|
|
|
data["client-id"] = data.pop("client_id")
|
|
|
|
data["client-secret"] = data.pop("client_secret")
|
|
|
|
|
|
|
|
self.log.info("client-id:\n%s", data["client-id"])
|
|
|
|
self.log.info("client-secret:\n%s", data["client-secret"])
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2021-02-12 18:03:06 +01:00
|
|
|
class OAuthPixiv(OAuthBase):
|
|
|
|
subcategory = "pixiv"
|
|
|
|
pattern = "oauth:pixiv$"
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
|
|
|
|
|
|
|
code_verifier = util.generate_token(32)
|
|
|
|
digest = hashlib.sha256(code_verifier.encode("ascii")).digest()
|
|
|
|
code_challenge = base64.urlsafe_b64encode(
|
|
|
|
digest).rstrip(b"=").decode("ascii")
|
|
|
|
|
|
|
|
url = "https://app-api.pixiv.net/web/v1/login"
|
|
|
|
params = {
|
|
|
|
"code_challenge": code_challenge,
|
|
|
|
"code_challenge_method": "S256",
|
|
|
|
"client": "pixiv-android",
|
|
|
|
}
|
|
|
|
code = self.open(url, params, self._input)
|
|
|
|
|
|
|
|
url = "https://oauth.secure.pixiv.net/auth/token"
|
|
|
|
headers = {
|
|
|
|
"User-Agent": "PixivAndroidApp/5.0.234 (Android 11; Pixel 5)",
|
|
|
|
}
|
|
|
|
data = {
|
|
|
|
"client_id" : self.oauth_config(
|
|
|
|
"client-id" , pixiv.PixivAppAPI.CLIENT_ID),
|
|
|
|
"client_secret" : self.oauth_config(
|
|
|
|
"client-secret", pixiv.PixivAppAPI.CLIENT_SECRET),
|
|
|
|
"code" : code,
|
|
|
|
"code_verifier" : code_verifier,
|
|
|
|
"grant_type" : "authorization_code",
|
|
|
|
"include_policy": "true",
|
|
|
|
"redirect_uri" : "https://app-api.pixiv.net"
|
|
|
|
"/web/v1/users/auth/pixiv/callback",
|
|
|
|
}
|
|
|
|
data = self.session.post(url, headers=headers, data=data).json()
|
|
|
|
|
|
|
|
if "error" in data:
|
|
|
|
print(data)
|
|
|
|
if data["error"] == "invalid_request":
|
|
|
|
print("'code' expired, try again")
|
|
|
|
return
|
|
|
|
|
|
|
|
token = data["refresh_token"]
|
|
|
|
if self.cache:
|
|
|
|
username = self.oauth_config("username")
|
|
|
|
pixiv._refresh_token_cache.update(username, token)
|
|
|
|
self.log.info("Writing 'refresh-token' to cache")
|
|
|
|
|
|
|
|
print(self._generate_message(("refresh-token",), (token,)))
|
|
|
|
|
|
|
|
def _input(self):
|
|
|
|
print("""
|
|
|
|
1) Open your browser's Developer Tools (F12) and switch to the Network tab
|
|
|
|
2) Login
|
2021-09-08 20:14:39 +02:00
|
|
|
3) Select the last network monitor entry ('callback?state=...')
|
2021-02-12 18:03:06 +01:00
|
|
|
4) Copy its 'code' query parameter, paste it below, and press Enter
|
|
|
|
""")
|
|
|
|
code = input("code: ")
|
|
|
|
return code.rpartition("=")[2].strip()
|