我必须通过向指定地址发送请求来获取 token 。我用过
$ curl $ curl -XPOST -H "Content-Type: application/json" -d '{ "client_id": "xxx", "client_secret": "11111111", "grant_type": "client_credentials", "scope": "public" }' "https://example.com/auth/token"
我得到了 token
abcdefg12345
现在我必须使用该 token 向此 url 端点发出请求(包括参数,如下所示):
$ curl -XGET -H "Authorization: Bearer {access_token}" "https://example.com/api/vacations/search?country=us&locale=en-US&price=affortable"
我不太熟悉 api 请求,但试图对其进行研究,所以我试图做的是:
http.get("https://example.com/api/vacations/search?country=us&locale=en-US&price=affortable"+"&token=abcdefg12345")
不幸的是,这不起作用,我得到这个:“访问 token 未授权” 有人能帮忙吗?我也不太确定 Bearer token 是否与“常规” token 有任何不同,并且请求必须不同?也不确定将参数留在 url 端点中是否正确,或者我应该这样指定它们:
http.get("https://example.com/api/vacations/search?"+params+"&token=abcdefg12345", {params:country='us', price:affordable})
非常感谢!
最佳答案
你会用
$ curl -H "Authorization: Bearer abcdefg12345" "https://example.com/api/vacations/search?country=us&locale=en-US&price=affortable"
你试过吗?结果如何?
关于javascript - 如何使用授权 token 向 api 发出 http 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45381600/