📜  Python – Tweepy 中的 API.update_profile_image()

📅  最后修改于: 2022-05-13 01:54:57.829000             🧑  作者: Mango

Python – Tweepy 中的 API.update_profile_image()

Twitter是一个流行的社交网络,用户在其中分享称为推文的消息。 Twitter 允许我们使用 Twitter API 或Tweepy挖掘任何用户的数据。数据将是从用户那里提取的推文。首先要做的是从 twitter 开发人员那里轻松获得每个用户可用的消费者密钥、消费者密钥、访问密钥和访问密钥。这些密钥将帮助 API 进行身份验证。

API.update_profile_image()

Tweepy 模块中API类的update_profile_image()方法用于更新认证用户的头像。

示例 1:使用以下图片更新个人资料图片:

# import the module
import tweepy
  
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
  
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
# set access to user's access key and access secret 
auth.set_access_token(access_token, access_token_secret)
  
# calling the api 
api = tweepy.API(auth)
  
# the file path
filename = "sunflower.jpg"
  
# updating the profile picture
api.update_profile_image(filename)

输出 :

示例 2:尝试上传除 GIF、JPG 或 PNG 以外的文件将产生异常。

# the file path
filename = "sunflower.mp3"
  
# updating the profile picture
api.update_profile_image(filename)

输出 :

raise TweepError('Invalid file type for image: %s' % file_type)
tweepy.error.TweepError: Invalid file type for image: None