d60 / twikit

Twitter API Scraper | Without an API key | Twitter Internal API | Free | Twitter scraper | Twitter Bot
https://twikit.readthedocs.io/en/latest/twikit.html
MIT License
1.19k stars 133 forks source link

Hit error 'This request looks like it might be automated. ' #170

Open KazuyaFCB opened 1 month ago

KazuyaFCB commented 1 month ago

It hits error in the line: await client.create_tweet(comment_content['text'], reply_to=new_tweet.id, media_ids=comment_media_ids)

Error: twikit.errors.CouldNotTweet: {'message': "Authorization: This request looks like it might be automated. To protect our users from spam and other malicious activity, we can't complete this action right now. Please try again later. (226)", 'locations': [{'line': 18, 'column': 3}], 'path': ['create_tweet'], 'extensions': {'name': 'AuthorizationError', 'source': 'Client', 'code': 226, 'kind': 'Permissions', 'tracing': {'trace_id': '282baea6b4e205d1'}}, 'code': 226, 'kind': 'Permissions', 'name': 'AuthorizationError', 'source': 'Client', 'tracing': {'trace_id': '282baea6b4e205d1'}}

My source code:

# Function to upload media and check file type
async def upload_and_check(file_path : str):
    with open(file_path, 'rb') as f:
        binary = f.read()
    kind = filetype.guess(binary)
    if kind is None:
        raise Exception(f"Cannot guess file type for {file_path}")
    return await client.upload_media(file_path, wait_for_completion=True)

async def random_delay():
    print("Wait 10-20 seconds...\n")
    delay = random.uniform(10, 20)
    await asyncio.sleep(delay)

"""
Sample for post_content:
{
    "text": "Some post content",
    "images": [
        "media\\20240803231338\\test.jpg"
    ],
    "videos": []
}

Sample for comment_contents:
{
    "text": "Some comment content",
    "images": [],
    "videos": ["media\\20240803231338\\test.mp4"]
}
"""
async def create_post_and_comment(post_content: dict[str, any], comment_contents: list):
    post_media_ids: List[str] = []
    for image in post_content['images']:
        post_media_ids.append(await upload_and_check(image))

    for video in post_content['videos']:
        post_media_ids.append(await upload_and_check(video))

    new_tweet = await client.create_tweet(post_content['text'], media_ids=post_media_ids)

    print(f"Created new post: {post_content['text']}\n")

    await random_delay()

    # Create comments under the new tweet
    for comment_content in comment_contents:
        comment_media_ids: List[str] = []
        for image in comment_content['images']:
            comment_media_ids.append(await upload_and_check(image))

        for video in comment_content['videos']:
            try:
                comment_media_ids.append(await upload_and_check(video))
            except Exception as e:
                print(f"Error uploading video: {e}")
                continue  # Skip this video and continue

        await client.create_tweet(comment_content['text'], reply_to=new_tweet.id, media_ids=comment_media_ids)

        print(f"Created new comment: {comment_content['text']}\n")

        await random_delay()
    # Log out
    await client.logout()

Is there any solution?

justin-dx commented 1 month ago

I just hit this too. However, searching and other interactions are fine

david-lev commented 4 weeks ago

Same for me. any solution?

twikit.errors.CouldNotTweet: {
    "message": "Authorization: This request looks like it might be automated. To protect our users from spam and other malicious activity, we can't complete this action right now. Plea se try again later. (226)",
    "locations": [{"line": 18, "column": 3}],
    "path": ["notetweet_create"],
    "extensions": {
        "name": "AuthorizationError",
        "source": "Client",
        "code": 226,
        "kind": "Permissions",
        "tracing": {"trace_id": "205xxxxxxxfca"},
    },
    "code": 226,
    "kind": "Permissions",
    "name": "AuthorizationError",
    "source": "Client",
    "tracing": {"trace_id": "2053xxxxxdfbfca"},
}
AritzUMA commented 3 weeks ago

After update is already fixed for me