2017-07-12 85 views
2

我正在嘗試獲取curl並從API獲取JSON。包含憑據的URL的請求

curl -XPOST -d "grant_type=password" -d "[email protected]" \ 
    -d "password=admin" "[email protected]:8081/oauth/token" 

當我使用終端的一切捲曲工作正常,但與試圖成爲一個取我得到的底部所提及的錯誤消息。

fetch("http://[email protected]:8081/oauth/token", { 
     credentials: 'include', 
     body: "grant_type=password&[email protected]&password=admin", 
     headers: { 
      "Content-Type": "application/x-www-form-urlencoded", 
     }, 
     method: "POST" 
    } 

這是錯誤我得到:

TypeError: http://[email protected]:8081/oauth/token is an url with embedded credentials.

是取指我做錯了?

回答

2

您不能使用https://user:[email protected]形式,您需要設置授權HTTP標題:

var headers = new Headers(); 

headers.append('Authorization', 'Basic ' + btoa(username + ':' + password)); 
fetch('https://host.com', {headers: headers}) 

btoa編碼「用戶名:密碼」到base64編碼字符串。您可以在MDN上找到btoa功能可用性(簡而言之:它適用於IE 10及更高版本)。

相關問題