代表另一个推特帐户使用推特 API

问题描述 投票:0回答:1

我如何使用 twitter api 代表另一个帐户(我拥有)发布推文?我在我的主要 Twitter 帐户上有一个开发者帐户,我创建了一个应用程序,我想使用这个应用程序从不同的 Twitter 帐户而不是我的主要帐户发布推文。我一直试图理解这些文档,但我发现它们非常混乱,所以请不要回复“检查文档”之类的内容。提前致谢

我试图找到一种方法来构建带有范围和类似内容的 oauth 链接,但我遇到错误,即使我让它工作,我也不知道如何使用我从 auth 获得的代码。另外,我需要如何授权对 api 的请求才能从其他帐户发布推文?

twitter oauth-2.0 twitter-oauth twitterapi-python twitter-api-v2
1个回答
0
投票

在这里,我使用的是 Tweepy 库。

1。尝试参考此 python 代码以获取您的另一个帐户的访问令牌和访问密码。

# -*- coding: utf-8 -*-
# Copyright 2017 Twitter, Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0

# This script generates a user's access tokens which can be used to make requests on behalf of the user, also known as OAuth 1.0a (user context) authentication method
# Docs: https://developer.twitter.com/en/docs/authentication/oauth-1-0a/obtaining-user-access-tokens

import sys
import requests
from requests_oauthlib import OAuth1Session

#Prompt developer to enter App credentials
print("Enter the credentials for your developer App below.")
CONSUMER_KEY = input("Consumer key: ")
CONSUMER_SECRET = input("Consumer secret: ")
ACCESS_TOKEN = input("Access token: ")
TOKEN_SECRET = input("Access token secret: ")

# Request an OAuth Request Token. This is the first step of the 3-legged OAuth flow. This generates a token that you can use to request user authorization for access.
def request_token():

    oauth = OAuth1Session(CONSUMER_KEY, client_secret=CONSUMER_SECRET, callback_uri='oob')

    url = "https://api.twitter.com/oauth/request_token"

    try:
        response = oauth.fetch_request_token(url)
        resource_owner_oauth_token = response.get('oauth_token')
        resource_owner_oauth_token_secret = response.get('oauth_token_secret')
    except requests.exceptions.RequestException as e:
            print(e)
            sys.exit(120)
    
    return resource_owner_oauth_token, resource_owner_oauth_token_secret

# Use the OAuth Request Token received in the previous step to redirect the user to authorize your developer App for access.
def get_user_authorization(resource_owner_oauth_token):

    authorization_url = f"https://api.twitter.com/oauth/authorize?oauth_token={resource_owner_oauth_token}"
    authorization_pin = input(f" \n Send the following URL to the user you want to generate access tokens for. \n → {authorization_url} \n This URL will allow the user to authorize your application and generate a PIN. \n Paste PIN here: ")

    return(authorization_pin)

# Exchange the OAuth Request Token you obtained previously for the user’s Access Tokens.
def get_user_access_tokens(resource_owner_oauth_token, resource_owner_oauth_token_secret, authorization_pin):

    oauth = OAuth1Session(CONSUMER_KEY, 
                            client_secret=CONSUMER_SECRET, 
                            resource_owner_key=resource_owner_oauth_token, 
                            resource_owner_secret=resource_owner_oauth_token_secret, 
                            verifier=authorization_pin)
    
    url = "https://api.twitter.com/oauth/access_token"

    try: 
        response = oauth.fetch_access_token(url)
        access_token = response['oauth_token']
        access_token_secret = response['oauth_token_secret']
        user_id = response['user_id']
        screen_name = response['screen_name']
    except requests.exceptions.RequestException as e:
            print(e)
            sys.exit(120)

    return(access_token, access_token_secret, user_id, screen_name)

if __name__ == '__main__':
    resource_owner_oauth_token, resource_owner_oauth_token_secret = request_token()
    authorization_pin = get_user_authorization(resource_owner_oauth_token)
    access_token, access_token_secret, user_id, screen_name = get_user_access_tokens(resource_owner_oauth_token, resource_owner_oauth_token_secret, authorization_pin)
    print(f"\n User @handle: {screen_name}", f"\n User ID: {user_id}", f"\n User access token: {access_token}", f" \n User access token secret: {access_token_secret} \n")
    

参考:https://github.com/twitterdev/enterprise-scripts-python/blob/main/Engagement-API/generate_user_access_tokens.py

2。点击授权网址

3。将您从 auth 获得的代码粘贴到终端。

你会得到这样的最终输出。

User @handle: [YOUR_TWITTER_HANDLENAME]
User ID: [YOUR_TWITTER_USERID]
User access token: [YOUR_TWITTER_ACCESS_TOKEN]
User access token secret: [YOUR_TWITTER_ACCESS_SECRET_TOKEN]

4。然后,在您的 Python 脚本中使用该访问令牌和机密从另一个帐户发送推文。请注意,需要使用与主体相同的消费者令牌和秘密以确保其正常工作。

例如:

    import tweepy
    
    CONSUMER_KEY = "YOUR_MAIN_ACCOUNT_CONSUMER_KEY"
    CONSUMER_SECRET = "YOUR_MAIN_ACCOUNT_CONSUMER_SECRET"

    # Comment out your main access token and secret
    # ACCESS_TOKEN = "YOUR_MAIN_TWITTER_ACCESS_TOKEN"
    # ACCESS_TOKEN_SECRET = "YOUR_MAIN_TWITTER_ACCESS_TOKEN_SECRET"
    
    # Use new token obtained from the authentication.
    ACCESS_TOKEN = "YOUR_ANOTHER_TWITTER_ACCESS_TOKEN"
    ACCESS_TOKEN_SECRET = "YOUR_ANOTHER_TWITTER_ACCESS_TOKEN_SECRET"
    
    # Authenticate to Twitter
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    
    # Create API object
    api = tweepy.API(auth)
    
    # Create a tweet
    api.update_status("Hello Tweepy!")

© www.soinside.com 2019 - 2024. All rights reserved.