简体   繁体   English

Autodesk Forge:使用POST / XMLHttpRequest和Javascript获取访问令牌吗?

[英]Autodesk forge: Get access token using POST/XMLHttpRequest and Javascript?

I am new to the Autodesk API's, and trying to create the Viewer using javascript on my website, which is hosted by a 3rd party (think Weebly, Squarespace, etc.) that supports JavaScript. 我是Autodesk API的新手,正在尝试使用我网站上的javascript创建查看器,该网站由支持JavaScript的第三方(如Weebly,Squarespace等)托管。 I can successfully get an access token if I use Postman to make the POST call to https://developer.api.autodesk.com/authentication/v1/authenticate , but that doesn't help me from a general use perspective. 如果我使用Postman对POST进行https://developer.api.autodesk.com/authentication/v1/authenticate的POST调用,则可以成功获取访问令牌,但是从一般使用的角度来看,这对我没有帮助。 When I try to use XMLHttpRequest and make the same POST call from my javascript, I get an error related to CORS ("No 'Access-Control-Allow-Origin' header is present on the requested resource."). 当我尝试使用XMLHttpRequest并从我的JavaScript进行相同的POST调用时,出现与CORS相关的错误(“所请求的资源上没有'Access-Control-Allow-Origin'标头。”)。 I can't find anywhere where it seems possible to use javascript to call out to Autodesk's API's and create an Autodesk viewer on my own website. 我找不到在哪里可以使用JavaScript调用Autodesk API并在自己的网站上创建Autodesk查看器的任何地方。 Is this possible using javascript alone? 是否可以仅使用JavaScript? Any info would be great. 任何信息都很好。

I am working from the step-by-step API tutorial at https://developer.autodesk.com/en/docs/viewer/v2/tutorials/basic-viewer/ , which is great, but doesn't seem to indicate how an actual POST call is worked into your application, instead of getting the token via Postman or some other testing tool. 我正在通过https://developer.autodesk.com/zh_CN/docs/viewer/v2/tutorials/basic-viewer/的分步API教程进行工作,虽然很好,但似乎并未指出如何实际的POST调用已应用到您的应用程序中,而不是通过Postman或其他测试工具获取令牌。

JavaScript: JavaScript:

function getToken() { 
    var xhttp = new XMLHttpRequest();
    var url = "https://developer.api.autodesk.com/authentication/v1/authenticate"; 
    var params = "client_id=MY_CLIENT_ID&client_secret=MY_CLIENT_SECRET&grant_type=client_credentials&scope=data:read";
    xhttp.open("POST", url, true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    xhttp.send(params);
}

Thanks. 谢谢。

You cannot, this is not supported. 您不能,这不受支持。 Autodesk Forge only supports OAuth from the server side, which is more secure. Autodesk Forge仅从服务器端支持OAuth ,这更加安全。 Explaining, your approach requires the Client ID & Secret to be exposed on the client, so the API blocks it via CORS header. 解释一下,您的方法要求在客户端上公开“客户端ID和机密”,因此API通过CORS标头将其阻止。

If you are using one of the following programming languages: JavaScript (node.js), .Net, Java, Ruby, simply use the existing SDK. 如果您使用以下编程语言之一:JavaScript(node.js)、. Net,Java,Ruby,只需使用现有的SDK。 It will make things much easier. 这将使事情变得容易得多。

See here for more details. 有关更多详细信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM