登录
首页 >  Golang >  Go问答

使用 go 向 mojang API 发出请求会返回 403

来源:stackoverflow

时间:2024-04-21 23:09:38 191浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《使用 go 向 mojang API 发出请求会返回 403》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容

我对 golang 很陌生,但我正在尝试向 mojang api 发出 http get 请求以获取玩家姓名的 uuid。这是我的代码。

func gethttp(url string) (response string) {

    client := &http.client{}

    req, err := http.newrequest("get", url, nil)
    req.header.add("content-type", "application/json")
    if err != nil {
        return string(err.error())
    }

    resp, err := client.do(req)
    if err != nil {
        return string(err.error())
    }

    defer resp.body.close()

    body, e := ioutil.readall(resp.body)
    if err != nil {
        return string(e.error())
    }

    return string(body)
}

func main() {
  fmt.printf(gethttp("https://api.mojang.com/users/profiles/minecraft/paultje52"))
}

我得到的是一个带有 403 错误(请求被阻止)的 html 文档,就像您在此处看到的那样。



error: the request could not be satisfied

403 error

the request could not be satisfied.


request blocked. we can't connect to the server for this app or website at this time. there might be too much traffic or a configuration error. try again later, or contact the app or website owner.
if you provide content to customers through cloudfront, you can find steps to troubleshoot and help prevent this error by reviewing the cloudfront documentation.

generated by cloudfront (cloudfront)
request id: jxluo2auflong500lqbzeswb0yksv4ae8dreo3g-0arxxmjsfs5c8g==

但是当我使用 google chrome 或 postman(具有相同 ip 的同一台机器)访问完全相同的 url 时,我会从 mojang api 得到正常响应。

{
    "name": "Paultje52",
    "id": "895933a3f98b4bd697515673a04d632e"
}

我做错了什么或者这是 go 的问题吗?或者阻止 go 发出的 cloudfront 请求?


解决方案


该网站似乎阻止了默认用户代理。 使用以下行更改它,一切都会正常工作

req.Header.Set("User-Agent", "test")

理论要掌握,实操不能落!以上关于《使用 go 向 mojang API 发出请求会返回 403》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>