我现在被困了一段时间,希望得到任何帮助。我了解 Oauth2.
我能够:
oauth2_user_handler = tweepy.OAuth2UserHandler(
client_id=client_id,
redirect_uri="https://example.com",
scope=["tweet.write","users.read", "tweet.read", "offline.access", "bookmark.read"],
client_secret="client_secret"
)
print(oauth2_user_handler.get_authorization_url())
full_url = access_token = input("Paste in the full URL after you authorized your App: ")
access_token = oauth2_user_handler.fetch_token(full_url)
access = access_token['access_token']
refresh_token = access_token['refresh_token']
client = tweepy.Client(access)
me = client.get_me()
print(me)
我无法:
这是类的文档Oauth2UserHandler
这里有一些我正在研究的代码:
auth_url = auth.get_authorization_url()
print(f"Please authorize the app by visiting:\n{auth_url}")
auth_url_full = input("Paste in the full URL after you authorized your App: ")
# Fetch the access and refresh tokens
tokens = auth.fetch_token(auth_url_full)
# Extract the access and refresh tokens from the response
access_token = tokens['access_token']
refresh_token = tokens['refresh_token']
if 'expires_in' in tokens:
token_expiration = tokens['expires_in']
else:
print("Error: 'expires_in' key not found in tokens dictionary")
print(f"Keys in tokens dictionary: {tokens.keys()}")
# Handle the case where the key is missing or has a different name
print(f"Access token: {access_token}")
print(f"Refresh token: {refresh_token}")
print(f"Keys in tokens dictionary: {vars(tokens).keys()}")
print(tokens)
print(" ")
print(" ")
print(dir(auth))
这是目录输出
Keys in tokens dictionary: dict_keys(['_new_scope', '_old_scope'])
['__attrs__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_client', '_state', 'access_token', 'adapters', 'auth', 'authorization_url', 'authorized', 'auto_refresh_kwargs', 'auto_refresh_url', 'cert', 'client_id', 'close', 'compliance_hook', 'cookies', 'delete', 'fetch_token', 'get', 'get_adapter', 'get_authorization_url', 'get_redirect_target', 'head', 'headers', 'hooks', 'max_redirects', 'merge_environment_settings', 'mount', 'new_state', 'options', 'params', 'patch', 'post', 'prepare_request', 'proxies', 'put', 'rebuild_auth', 'rebuild_method', 'rebuild_proxies', 'redirect_uri', 'refresh_token', 'register_compliance_hook', 'request', 'resolve_redirects', 'scope', 'send', 'should_strip_auth', 'state', 'stream', 'token', 'token_from_fragment', 'token_updater', 'trust_env', 'verify']
我尝试将 refresh_token 作为 access_token 传递,并尝试将其传递给 feth_token() 方法,但它不起作用