首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么在尝试使用reddit API时会出现405错误?

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

Stack Overflow用户
提问于 2021-10-31 21:11:32
回答 1查看 144关注 0票数 0

我目前正在制作一个移动应用程序,从reddit API中获取数据,并将其显示在Reddit界面的重拍上。

应用程序由多个页面组成,第一个页面是登录页,它使用OAuth2.0检索连接令牌,然后导航到另一个页面,在那里我尝试获取关于reddit配置文件的数据并显示它。

登录页面以缩进的方式工作,我可以连接到reddit API并导航到下一个页面,但是当我尝试从reddit API获取任何数据时,我得到了405个错误代码,有时还会出现500个错误。

下面是第二页后端的代码:

代码语言:javascript
运行
复制
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();
    }, []);

在我的终端中显示以下错误:

代码语言:javascript
运行
复制
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

任何帮助都将是宝贵的,谢谢你提前!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-31 21:28:09

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69790642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档