コード例 #1
0
def test_oauth_uid():
    responses.add(
        responses.POST,
        url="https://api.twitter.com/oauth/request_token",
        json={
            "oauth_token": "uid-token",
            "oauth_token_secret": "oauth token secret",
            "oauth_callback_confirmed": True,
        },
    )
    api = Api(
        consumer_key="consumer key", consumer_secret="consumer secret", oauth_flow=True
    )
    assert api.get_authorize_url()
    # do authorize
    resp_url = "https://localhost/?oauth_token=oauth_token&oauth_token_secret=oauth_token_secret&oauth_verifier=oauth_verifier"

    responses.add(
        responses.POST,
        url="https://api.twitter.com/oauth/access_token",
        json={
            "oauth_token": "uid-token",
            "oauth_token_secret": "oauth token secret",
        },
    )

    token = api.generate_access_token(response=resp_url)

    assert token["oauth_token"] == "uid-token"
    assert api.auth_user_id == "uid"
コード例 #2
0
"""

from pytwitter import Api

consumer_key = "your app consumer key"
consumer_secret = "your app consumer secret"

api = Api(
    consumer_key=consumer_key,
    consumer_secret=consumer_secret,
    oauth_flow=True,
)

authorize_url = api.get_authorize_url()
print(f"Click authorize url to do authorize: {authorize_url}")

resp = input("Response here: ")

token = api.generate_access_token(response=resp)
print(f"Get token: {token}")
# Get token: {'oauth_token': 'token', 'oauth_token_secret': 'token_secret', 'user_id': 'user_id', 'screen_name': 'name'}

tweet_id = input("Tweet id you want to like:")

resp = api.like_tweet(
    user_id=api.auth_user_id,
    tweet_id=tweet_id,
)
print(f"Resp: {resp}")
# Resp: {'data': {'liked': True}}