使用 Tauri 框架内置的 fetch 方法调用 GitHub API 时,出现 Request forbidden by administrative rules 错误。
问题原因
GitHub API 要求所有的请求都必须包含一个有效的 User-Agent 头部。User-Agent 头部用于标识发起请求的用户或应用程序。
github 官网说明:
All API requests must include a valid User-Agent header. The User-Agent header identifies the user or application that is making the request.
By default, GitHub CLI sends a valid User-Agent header. However, GitHub recommends using your GitHub username, or the name of your application, for the User-Agent header value. This allows GitHub to contact you if there are problems.
解决方案
- 在请求的 headers 中添加
'User-Agent': 'Tauri-fetch'即可解决此问题,Tauri-fetch可替换为真实的,也可以自定义其他值。 - http 请求封装:
1 | |
欢迎访问:天问博客
本文作者: Tiven
发布时间: 2024-03-12
最后更新: 2024-03-27
本文标题: github API : Request forbidden by administrative rules
本文链接: https://www.tiven.cn/p/588a8def/
版权声明: 本作品采用 CC BY-NC-SA 4.0 许可协议进行许可。转载请注明出处!
发布时间: 2024-03-12
最后更新: 2024-03-27
本文标题: github API : Request forbidden by administrative rules
本文链接: https://www.tiven.cn/p/588a8def/
版权声明: 本作品采用 CC BY-NC-SA 4.0 许可协议进行许可。转载请注明出处!

