为什么在尝试使用 reddit API 时出现 405 错误?

Why do I get 405 error when trying to use reddit API?

我目前正在制作一个从 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

任何帮助都将是宝贵的,在此先感谢您!

当您尝试使用不允许的请求方法访问端点时,会发生 405 错误。我认为这是因为您的请求方法是 set 而不是 get?