Improve auth.py order
authorHarmon <Harmon758@gmail.com>
Fri, 7 Jan 2022 11:25:05 +0000 (05:25 -0600)
committerHarmon <Harmon758@gmail.com>
Sat, 8 Jan 2022 00:19:27 +0000 (18:19 -0600)
tweepy/auth.py

index 040a7a0e678e676bf75b6e8d421a83094fc59c21..a6af3ccf599496d1542e3a08c8a0c6529d90a7d0 100644 (file)
@@ -133,49 +133,6 @@ class OAuthHandler(AuthHandler):
             raise TweepyException(e)
 
 
-class OAuth2UserHandler(OAuth2Session):
-
-    def __init__(self, *, client_id, redirect_uri, scope, client_secret=None):
-        super().__init__(client_id, redirect_uri=redirect_uri, scope=scope)
-        if client_secret is not None:
-            self.auth = HTTPBasicAuth(client_id, client_secret)
-        else:
-            self.auth = None
-
-    def get_authorization_url(self):
-        self.code_verifier = secrets.token_urlsafe(128)[:128]
-        code_challenge = urlsafe_b64encode(
-            sha256(self.code_verifier.encode("ASCII")).digest()
-        ).rstrip(b'=')
-        authorization_url, state = self.authorization_url(
-            "https://twitter.com/i/oauth2/authorize",
-            code_challenge=code_challenge, code_challenge_method="s256"
-        )
-        return authorization_url
-
-    def fetch_token(self, authorization_response):
-        return super().fetch_token(
-            "https://api.twitter.com/2/oauth2/token",
-            authorization_response=authorization_response,
-            auth=self.auth,
-            include_client_id=True,
-            code_verifier=self.code_verifier
-        )
-
-
-class OAuth2BearerHandler(AuthBase):
-
-    def __init__(self, bearer_token):
-        self.bearer_token = bearer_token
-
-    def __call__(self, request):
-        request.headers['Authorization'] = 'Bearer ' + self.bearer_token
-        return request
-
-    def apply_auth(self):
-        return self
-
-
 class OAuth2AppHandler(AuthHandler):
     """Application-only authentication handler"""
 
@@ -205,3 +162,46 @@ class OAuth2AppHandler(AuthHandler):
         return OAuth2BearerHandler(self._bearer_token)
 
 AppAuthHandler = OAuth2AppHandler
+
+
+class OAuth2BearerHandler(AuthBase):
+
+    def __init__(self, bearer_token):
+        self.bearer_token = bearer_token
+
+    def __call__(self, request):
+        request.headers['Authorization'] = 'Bearer ' + self.bearer_token
+        return request
+
+    def apply_auth(self):
+        return self
+
+
+class OAuth2UserHandler(OAuth2Session):
+
+    def __init__(self, *, client_id, redirect_uri, scope, client_secret=None):
+        super().__init__(client_id, redirect_uri=redirect_uri, scope=scope)
+        if client_secret is not None:
+            self.auth = HTTPBasicAuth(client_id, client_secret)
+        else:
+            self.auth = None
+
+    def get_authorization_url(self):
+        self.code_verifier = secrets.token_urlsafe(128)[:128]
+        code_challenge = urlsafe_b64encode(
+            sha256(self.code_verifier.encode("ASCII")).digest()
+        ).rstrip(b'=')
+        authorization_url, state = self.authorization_url(
+            "https://twitter.com/i/oauth2/authorize",
+            code_challenge=code_challenge, code_challenge_method="s256"
+        )
+        return authorization_url
+
+    def fetch_token(self, authorization_response):
+        return super().fetch_token(
+            "https://api.twitter.com/2/oauth2/token",
+            authorization_response=authorization_response,
+            auth=self.auth,
+            include_client_id=True,
+            code_verifier=self.code_verifier
+        )