当我使用OAuth常量在邮递员中请求@TwitterApi时,它的工作正常,但是当我尝试执行axios.get()时,它给出了错误。如果有人仔细检查代码并帮助我解决这些问题,我将非常感激。
const searchUrl = "https://api.twitter.com/1.1/search/tweets.json";
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get("/tweets", async (req, res) => {
await axios({
method: "get",
url: searchUrl,
params: { q: "narendramodi" },
headers: {
accept: "application/json",
Authorization:
'OAuth oauth_consumer_key="' +
CONSUMER_KEY +
'",oauth_token="' +
ACCESS_TOKEN +
'",oauth_consumer_secret="' +
CONSUMER_SECRET +
'",oauth_token_secret="' +
ACCESS_TOKEN_SECRET +
'"'
}
})
.then(response => {
console.log(headerString);
console.log(response);
res.status(200).send(response);
})
.catch(err => {
console.log(headerString);
res.status(404).send(err);
});
});
回复
{
"message": "Request failed with status code 400",
"name": "Error",
"stack": "Error: Request failed with status code 400\n at createError (C:\\Projects\\Test\\node_modules\\axios\\lib\\core\\createError.js:16:15)\n at settle (C:\\Projects\\Test\\node_modules\\axios\\lib\\core\\settle.js:17:12)\n at IncomingMessage.handleStreamEnd (C:\\Projects\\Test\\node_modules\\axios\\lib\\adapters\\http.js:237:11)\n at IncomingMessage.emit (events.js:194:15)\n at endReadableNT (_stream_readable.js:1125:12)\n at process._tickCallback (internal/process/next_tick.js:63:19)",
"config": {
"url": "https://api.twitter.com/1.1/search/tweets.json",
"method": "get",
"params": {
"q": "narendramodi"
},
"headers": {
"Accept": "application/json",
"Authorization": "OAuth oauth_consumer_key=\"0XG5299e6oSESyHvLGIMGmwW3\",oauth_token=\"3097151617-91Ayf0gu7O81oe6ae3quLPX5cxYkf7pZlkNZ09h\",oauth_consumer_secret=\"kh08Sydpo5hYYr0DCY8i7oJRAbxNkI1NKNpdStVi08ICIwBUOW\",oauth_token_secret=\"TPnK7IgPW0TB0m9NemXiyKAlZC6rBRpqi56w7sDhVxEgl\"",
"User-Agent": "axios/0.19.0"
},
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1
}
}
但是当我用邮递员发帖时,响应就会像这样:
{
"statuses": [
{
"created_at": "Fri Jul 12 01:02:35 +0000 2019",
"id": 1149484164317495296,
"id_str": "1149484164317495296",
"text": "RT @poonam_mahajan: \"The Story of a New India\"\n\nThe Youth of the #NewIndia is transforming into one voice with the help of @BJYM to join an…",
"truncated": false,
"entities": {
"hashtags": [
{
"text": "NewIndia",
"indices": [
65,
74
]
}
],
"symbols": [],
"user_mentions": [
{
"screen_name": "poonam_mahajan",
"name": "Poonam Mahajan",
"id": 230856076,
"id_str": "230856076",
"indices": [
3,
18
...