Когда я запрашиваю @TwitterApi в почтальоне с константами OAuth, он работает нормально, но когда я пытаюсь сделать 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
...