我正试图通过REST API在Tableau服务器上执行一些操作,我需要Auth令牌。我试着使用Postman的signin API,它的效果非常好。然而,当我尝试从我的网页上的javacript代码调用时,同样的代码给出了"Bad Request“错误。
var dataVal = {
"credentials": {
"name": "admin",
"password": "admin",
"site": {
"contentUrl": "MySite"
}
}
};
$.ajax({
contentType: 'application/json',
data: JSON.stringify(dataVal),
dataType: 'jsonp',
success: function (data) {
console.log("call succeeded");
},
error: function (request, textStatus, errorThrown) {
console.log(request.getAllResponseHeaders());
},
processData: false,
type: 'POST',
url: 'http://mytableauserver/api/2.6/auth/signin'
});
以上代码从postman返回token、siteid、userid,但从javascript代码返回""BAD REQUEST“”错误。任何指针将不胜感激。
发布于 2021-03-23 01:07:32
对于其他正在寻找的人来说,Tableau现在支持CORS,但目前只支持Tableau Server
。
Enabling CORS on Tableau Server for the REST API
For security, most web browsers restrict HTTP requests to the same origin. That is, to
access a resource on a server through an API, the request must come from the same
origin (server), or a proxy must be set up to handle the request.
Tableau Server now supports Cross-Origin Resource Sharing (CORS), so you can do away
with your proxy and call the REST API from the browser. The CORS mechanism is
currently only enabled for Tableau Server, and can be turned on by server
administrators in a couple of steps using the TSM command-line tool. To learn more,
see the description of the vizportal.rest_api.cors.allow_origin option in tsm
configuration set Options. As a security measure, you should make API calls to Tableau
Server using the HTTPS protocol (SSL or TLS). See Using HTTPS (SSL/TLS) for API Calls.
Add the origins that need access to the Tableau Server.
Determine the origins (servers) you want to allow access to the REST API, and use the
tsm configuration set command with the vizportal.rest_api.cors.allow_origin option.
For example, to grant access to one two origins, https://mysite and https://yoursite,
you would stop the server (tsm stop) and then use the following command:
tsm configuration set -k vizportal.rest_api.cors.allow_origin -v
https://mysite,https://yoursite
You can enter multiple origins. Use a comma to separate the entries.
Note: You could also use an asterisk (*) as a wild card to match all sites. This is
not recommended as it allows access from any origin that has access to the server and
could present a security risk. Do not use an asterisk (*) unless you fully understand
the implications and risks for your site.
Enable CORS on Tableau Server.
Use the tsm configuration set command with the vizportal.rest_api.cors.enabled option.
The default setting is false, so set this to true as follows:
tsm configuration set -k vizportal.rest_api.cors.enabled -v true
Update your Tableau Server configuration (tsm pending-changes apply) to restart
Tableau Server and make the changes take effect. Only the origins you specify will
have access.
https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_concepts_fundamentals.htm
https://stackoverflow.com/questions/47996257
复制