From 12822795fac8eceea6a0c1260a38b304a34ffd13 Mon Sep 17 00:00:00 2001 From: Harmon Date: Wed, 9 Feb 2022 15:51:52 -0600 Subject: [PATCH] Update usage of OAuthHandler to OAuth1UserHandler --- docs/getting_started.rst | 5 +++-- examples/API_v1/authentication.py | 5 +++-- examples/API_v1/follow_followers.py | 5 +++-- examples/API_v1/pin-based_authorization.py | 2 +- examples/API_v1/rate_limit_handling.py | 5 +++-- examples/API_v1/update_status.py | 5 +++-- tests/config.py | 7 ++++--- tests/test_auth.py | 6 +++--- tweepy/client.py | 8 +++++--- 9 files changed, 28 insertions(+), 20 deletions(-) diff --git a/docs/getting_started.rst b/docs/getting_started.rst index 39e26e8..449e444 100644 --- a/docs/getting_started.rst +++ b/docs/getting_started.rst @@ -19,8 +19,9 @@ Hello Tweepy import tweepy - auth = tweepy.OAuthHandler(consumer_key, consumer_secret) - auth.set_access_token(access_token, access_token_secret) + auth = tweepy.OAuth1UserHandler( + consumer_key, consumer_secret, access_token, access_token_secret + ) api = tweepy.API(auth) diff --git a/examples/API_v1/authentication.py b/examples/API_v1/authentication.py index c737d5b..b436aa1 100644 --- a/examples/API_v1/authentication.py +++ b/examples/API_v1/authentication.py @@ -15,8 +15,9 @@ consumer_secret = "" access_token = "" access_token_secret = "" -auth = tweepy.OAuthHandler(consumer_key, consumer_secret) -auth.set_access_token(access_token, access_token_secret) +auth = tweepy.OAuth1UserHandler( + consumer_key, consumer_secret, access_token, access_token_secret +) api = tweepy.API(auth) diff --git a/examples/API_v1/follow_followers.py b/examples/API_v1/follow_followers.py index 0c49a34..00095ee 100644 --- a/examples/API_v1/follow_followers.py +++ b/examples/API_v1/follow_followers.py @@ -6,8 +6,9 @@ consumer_secret = "" access_token = "" access_token_secret = "" -auth = tweepy.OAuthHandler(consumer_key, consumer_secret) -auth.set_access_token(access_token, access_token_secret) +auth = tweepy.OAuth1UserHandler( + consumer_key, consumer_secret, access_token, access_token_secret +) api = tweepy.API(auth) diff --git a/examples/API_v1/pin-based_authorization.py b/examples/API_v1/pin-based_authorization.py index 9c3eea6..471aa88 100644 --- a/examples/API_v1/pin-based_authorization.py +++ b/examples/API_v1/pin-based_authorization.py @@ -10,7 +10,7 @@ import tweepy consumer_key = "" consumer_secret = "" -auth = tweepy.OAuthHandler(consumer_key, consumer_secret) +auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret) # This prints a URL that can be used to authorize your app # After granting access to the app, a PIN to complete the authorization process diff --git a/examples/API_v1/rate_limit_handling.py b/examples/API_v1/rate_limit_handling.py index 3845a58..cea1807 100644 --- a/examples/API_v1/rate_limit_handling.py +++ b/examples/API_v1/rate_limit_handling.py @@ -6,8 +6,9 @@ consumer_secret = "" access_token = "" access_token_secret = "" -auth = tweepy.OAuthHandler(consumer_key, consumer_secret) -auth.set_access_token(access_token, access_token_secret) +auth = tweepy.OAuth1UserHandler( + consumer_key, consumer_secret, access_token, access_token_secret +) # Setting wait_on_rate_limit to True when initializing API will initialize an # instance, called api here, that will automatically wait, using time.sleep, diff --git a/examples/API_v1/update_status.py b/examples/API_v1/update_status.py index 7c1fa69..aad24ed 100644 --- a/examples/API_v1/update_status.py +++ b/examples/API_v1/update_status.py @@ -6,8 +6,9 @@ consumer_secret = "" access_token = "" access_token_secret = "" -auth = tweepy.OAuthHandler(consumer_key, consumer_secret) -auth.set_access_token(access_token, access_token_secret) +auth = tweepy.OAuth1UserHandler( + consumer_key, consumer_secret, access_token, access_token_secret +) api = tweepy.API(auth) diff --git a/tests/config.py b/tests/config.py index 6f65c40..db6abf0 100644 --- a/tests/config.py +++ b/tests/config.py @@ -4,7 +4,7 @@ import unittest import vcr from tweepy.api import API -from tweepy.auth import OAuthHandler +from tweepy.auth import OAuth1UserHandler user_id = os.environ.get('TWITTER_USER_ID', '1072250532645998596') @@ -35,6 +35,7 @@ class TweepyTestCase(unittest.TestCase): def create_auth(): - auth = OAuthHandler(consumer_key, consumer_secret) - auth.set_access_token(access_token, access_token_secret) + auth = OAuth1UserHandler( + consumer_key, consumer_secret, access_token, access_token_secret + ) return auth diff --git a/tests/test_auth.py b/tests/test_auth.py index 29ea4ab..015d05c 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -2,7 +2,7 @@ import random import unittest from config import * -from tweepy import API, OAuthHandler +from tweepy import API, OAuth1UserHandler class TweepyAuthTests(unittest.TestCase): @@ -11,7 +11,7 @@ class TweepyAuthTests(unittest.TestCase): if not consumer_key or not consumer_secret: self.skipTest("Missing consumer key and/or secret") - auth = OAuthHandler(consumer_key, consumer_secret) + auth = OAuth1UserHandler(consumer_key, consumer_secret) # test getting access token auth_url = auth.get_authorization_url() @@ -30,7 +30,7 @@ class TweepyAuthTests(unittest.TestCase): if not consumer_key or not consumer_secret: self.skipTest("Missing consumer key and/or secret") - auth = OAuthHandler(consumer_key, consumer_secret) + auth = OAuth1UserHandler(consumer_key, consumer_secret) auth_url = auth.get_authorization_url(access_type='read') print('Please open: ' + auth_url) answer = input('Did Twitter only request read permissions? (y/n) ') diff --git a/tweepy/client.py b/tweepy/client.py index 8848afb..4a20e1d 100644 --- a/tweepy/client.py +++ b/tweepy/client.py @@ -12,7 +12,7 @@ import warnings import requests import tweepy -from tweepy.auth import OAuthHandler +from tweepy.auth import OAuth1UserHandler from tweepy.errors import ( BadRequest, Forbidden, HTTPException, TooManyRequests, TwitterServerError, Unauthorized @@ -92,8 +92,10 @@ class Client: headers = {"User-Agent": self.user_agent} auth = None if user_auth: - auth = OAuthHandler(self.consumer_key, self.consumer_secret) - auth.set_access_token(self.access_token, self.access_token_secret) + auth = OAuth1UserHandler( + self.consumer_key, self.consumer_secret, + self.access_token, self.access_token_secret + ) auth = auth.apply_auth() else: headers["Authorization"] = f"Bearer {self.bearer_token}" -- 2.25.1