第一次問,所以我在這裏。Axios基本授權不經過Reactjs
我正在嘗試對我的團隊所做的需要授權的stormpath應用程序進行GET調用。當使用郵差測試和一些配置後,一切都出來了200
Results of API call in Postman
使用curl工作
curl --verbose --user ID:SECRET -H "Accept: application/json" https://api.stormpath.com/v1/tenants/current
...
< HTTP/1.1 302
< Cache-Control: private, no-cache, no-store, max-age=0, no-transform
< Date: Tue, 10 Jan 2017 09:27:14 GMT
< Location: https://api.stormpath.com/v1/tenants/TENANTID
< Pragma: no-cache
< Stormpath-Request-Id: f8e4dee0-d716-11e6-9795-22000aa92aa2
< Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
< X-Frame-Options: SAMEORIGIN
< Content-Length: 0
< Connection: keep-alive
<
* Connection #0 to host api.stormpath.com left intact
但是,當我試圖在React
撥打電話通過Axios
我得到一個401錯誤。
XMLHttpRequest cannot load https://api.stormpath.com/v1/tenants/current. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 401.
這是我用什麼:
axios({
method: 'get',
url: "https://api.stormpath.com/v1/tenants/current",
auth:
{
username: 'api ID',
password: 'api Secret'
}
})
我不知道爲什麼,但根據我得到的迴應它不提供用戶名和密碼。
code:401
developerMessage:"Authentication with a valid API Key is required."
message:"Authentication required."
moreInfo:"http://www.stormpath.com/docs/quickstart/connect"
requestId:"3686f590-d69e-11e6-9b8a-22000a8ce5d1"
status:401
它似乎有類似的問題之前曾被問過,但仍然沒有對他們的迴應。
Reactjs Axios/Spring boot security
Cannot Basic Auth from React App with Axios or SuperAgent
感謝的抽出時間來閱讀。
可能是無效的API密鑰/祕密,如錯誤所述。 –
使用郵遞員API密鑰/祕密工作得很好。我甚至使用API密鑰/祕密通過地址欄訪問API,它仍然有效。 –