POST 在 bluemix 对话框中调用 Unauthorized
POST call Unauthorized in bluemix dialog
我在单击按钮时收到此错误响应:
"POST https://watson-api-explorer.mybluemix.net/dialog/api/v1/dialogs/c8e08780-b08b-4cdb-8b8c-ea118863fcd1/conversation 401 (Unauthorized)"
这是我的代码:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<button onclick="makePostCall()">Click me</button>
<script>
makePostCall = function() {
var username = "c0ae64ef-410a-4f74-b875-ef52dee90686";
var password = "9K2q4byngzo7";
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://watson-api-explorer.mybluemix.net/dialog/api/v1/dialogs/c8e08780-b08b-4cdb-8b8c-ea118863fcd1/conversation', true);
//xhr.withCredentials = true;
xhr.setRequestHeader('Access-Control-Allow-Origin', 'http://localhost:80');
xhr.setRequestHeader('Access-Control-Allow-Credentials', '*');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
xhr.setRequestHeader('Access-Control-Allow-Headers', 'Content-Type', 'application/json', 'Authorization');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + " " + password));
xhr.send('{"query":{"match_all":{}}}');
}
</script>
</body>
</html>
首先,您真的不应该 post 在代码中使用您的对话服务凭据。现在您已经拥有了,我建议您删除该服务并为自己获取一组新的凭据。
第二点,由于浏览器域跨站点限制,您的客户端 javascript 应该在您自己的服务器中调用代理 API。 https://watson-api-explorer.mybluemix.net is the entry point into the swagger documentation, which can't be your application. You can use the swagger APIs either from the swagger client or from an application like curl, not from the browser based client of your own web application. Actually that is how the swagger application works, the application is on https://watson-api-explorer.mybluemix.net and it has proxy end points on https://watson-api-explorer.mybluemix.net
您将需要自己的应用程序入口点作为对话服务 url 的代理,该服务 API 的入口点位于 https://gateway.watsonplatform.net/dialog/api
您正在将对话 ID 传递给您的代理,因此您一定是通过某种方式获得的。有两种方法可以做到这一点。通过创建它 ala -
curl -u "{username}":"{password}" -X POST \
--form file=@template.xml \
--form name=templateName \
"https://gateway.watsonplatform.net/dialog/api/v1/dialogs"
或通过询问对话列表 ala -
curl -u "{username}":"{password}" "https://gateway.watsonplatform.net/dialog/api/v1/dialogs"
这两种方式都需要您提供服务凭证。所以他们应该没问题。
一旦你对架构进行排序,对话 API 就是
curl -u "{username}":"{password}" \
-X POST
--form conversation_id={conversation_id}
--form client_id={client_id}
--form input="Hi Hello"
"https://gateway.watsonplatform.net/dialog/api/v1/dialogs/{dialog_id}/conversation"
文档中提供了服务的所有示例 curls 调用 - https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/dialog/api/v1/
我在单击按钮时收到此错误响应:
"POST https://watson-api-explorer.mybluemix.net/dialog/api/v1/dialogs/c8e08780-b08b-4cdb-8b8c-ea118863fcd1/conversation 401 (Unauthorized)"
这是我的代码:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<button onclick="makePostCall()">Click me</button>
<script>
makePostCall = function() {
var username = "c0ae64ef-410a-4f74-b875-ef52dee90686";
var password = "9K2q4byngzo7";
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://watson-api-explorer.mybluemix.net/dialog/api/v1/dialogs/c8e08780-b08b-4cdb-8b8c-ea118863fcd1/conversation', true);
//xhr.withCredentials = true;
xhr.setRequestHeader('Access-Control-Allow-Origin', 'http://localhost:80');
xhr.setRequestHeader('Access-Control-Allow-Credentials', '*');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
xhr.setRequestHeader('Access-Control-Allow-Headers', 'Content-Type', 'application/json', 'Authorization');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + " " + password));
xhr.send('{"query":{"match_all":{}}}');
}
</script>
</body>
</html>
首先,您真的不应该 post 在代码中使用您的对话服务凭据。现在您已经拥有了,我建议您删除该服务并为自己获取一组新的凭据。
第二点,由于浏览器域跨站点限制,您的客户端 javascript 应该在您自己的服务器中调用代理 API。 https://watson-api-explorer.mybluemix.net is the entry point into the swagger documentation, which can't be your application. You can use the swagger APIs either from the swagger client or from an application like curl, not from the browser based client of your own web application. Actually that is how the swagger application works, the application is on https://watson-api-explorer.mybluemix.net and it has proxy end points on https://watson-api-explorer.mybluemix.net
您将需要自己的应用程序入口点作为对话服务 url 的代理,该服务 API 的入口点位于 https://gateway.watsonplatform.net/dialog/api
您正在将对话 ID 传递给您的代理,因此您一定是通过某种方式获得的。有两种方法可以做到这一点。通过创建它 ala -
curl -u "{username}":"{password}" -X POST \
--form file=@template.xml \
--form name=templateName \
"https://gateway.watsonplatform.net/dialog/api/v1/dialogs"
或通过询问对话列表 ala -
curl -u "{username}":"{password}" "https://gateway.watsonplatform.net/dialog/api/v1/dialogs"
这两种方式都需要您提供服务凭证。所以他们应该没问题。
一旦你对架构进行排序,对话 API 就是
curl -u "{username}":"{password}" \
-X POST
--form conversation_id={conversation_id}
--form client_id={client_id}
--form input="Hi Hello"
"https://gateway.watsonplatform.net/dialog/api/v1/dialogs/{dialog_id}/conversation"
文档中提供了服务的所有示例 curls 调用 - https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/dialog/api/v1/