为啥我在尝试使用 reddit API 时会收到 405 错误?
Posted
技术标签:
【中文标题】为啥我在尝试使用 reddit API 时会收到 405 错误?【英文标题】:Why do I get 405 error when trying to use reddit API?为什么我在尝试使用 reddit API 时会收到 405 错误? 【发布时间】:2021-12-15 19:31:22 【问题描述】:我目前正在制作一个从 reddit API 获取数据并将其显示在 Reddit 界面重制版上的移动应用程序。
该应用程序由多个页面组成,第一个页面是登录页面,它使用 oauth 2.0 检索连接令牌,然后导航到另一个页面,在那里我尝试获取有关我的 reddit 个人资料的数据并显示它。
登录页面是缩进的,我可以连接到 reddit API 并导航到下一页,但是当我尝试从 reddit API 获取任何数据时,我得到了一个 405 错误代码,有时会跟随500 错误。
这是第二页后端的代码:
const LoginScreen = (navigation) =>
let token = route.params.token;
const [username, setUsername] = useState('');
const [sub_count, setSubCount] = useState('');
const [profile_img_url, setImgUrl] = useState('');
const [date, setDate] = useState('');
async function fetchRedditAPI()
var config =
method: 'set',
url: 'https://oauth.reddit.com/api/v1/me',
headers:
'Authorization': 'Bearer ' + token
;
axios(config)
.then(function (response)
console.log("----API DATA-----")
console.log(response["data"])
console.log("-----------------")
let profile_img_url = response["data"]["icon_img"]
setImgUrl(profile_img_url);
let date = response["data"]["created_utc"]
setDate(date);
let username = response["data"]["display_name_prefixed"]
setUsername(username);
let sub_count = response["data"]["subscribers"]
setSubCount(sub_count);
)
.catch(function (error)
console.log("GOT THE FOLLOWING ERROR :")
console.log(error);
console.log("------------------------")
);
React.useEffect(() =>
console.log("fetching reddit API...");
fetchRedditAPI();
, []);
我的终端显示以下错误:
Request failed with status code 405
at node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:605:6 in setReadyState
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in __didCompleteResponse
at node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
Request failed with status code 500
at node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:605:6 in setReadyState
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in __didCompleteResponse
at node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
任何帮助都将是宝贵的,在此先感谢您!
【问题讨论】:
【参考方案1】:当您尝试使用不允许的请求方法访问端点时,会出现405 错误。我认为这是因为您的请求方法是set
而不是get
?
【讨论】:
谢谢,这确实是问题所在,我用method: 'get'
更改了method: 'set'
并且成功了!以上是关于为啥我在尝试使用 reddit API 时会收到 405 错误?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我在尝试使用 HttpListener 时会收到“AccessDenied”? [复制]
为啥我在尝试将它与目标 c 一起使用时会收到错误消息,即我的 json 末尾有垃圾?
为啥我在尝试编辑离开语音频道的用户时会收到 DiscordAPIError?