该错误是由于Twitter API v2的认证问题所导致的。解决此问题的方法是使用OAuth1.0a验证而不是OAuth2.0验证,因为Twitter API v2目前仅支持OAuth1.0a验证。
以下是一个包含代码示例的解决方案:
library(academictwitteR)
library(httr)
# 设置OAuth参数
consumer_key <- "YOUR_CONSUMER_KEY_HERE"
consumer_secret <- "YOUR_CONSUMER_SECRET_HERE"
access_token <- "YOUR_ACCESS_TOKEN_HERE"
access_token_secret <- "YOUR_ACCESS_TOKEN_SECRET_HERE"
# 创建OAuth1认证对象
oauth1.0a_token <- create_token(
endpoint = oauth_endpoint(
request = "https://api.twitter.com/oauth/request_token",
authorize = "https://api.twitter.com/oauth/authorize",
access = "https://api.twitter.com/oauth/access_token"
),
consumer_key = consumer_key,
consumer_secret = consumer_secret,
access_token = access_token,
access_secret = access_token_secret
)
# 使用OAuth1认证获取用户时间线
user_timeline <- get_user_timeline(
user = "USERNAME",
n = 10,
token = oauth1.0a_token
)
# 打印用户时间线
print(user_timeline)