简体   繁体   English

如何在节点环境中模拟浏览器按钮单击?

[英]How do I simulate a browser button click in a node environment?

I am building an android app which is supposed to connect to a server of mine, such that the server performs OAuth2 authentication at a third-party website.我正在构建一个 android 应用程序,它应该连接到我的服务器,以便服务器在第三方网站上执行 OAuth2 身份验证。 In this website there is a button which submits a form via a POST request when clicked.在这个网站上有一个按钮,点击后通过 POST 请求提交表单。 Clicking the button is part of the authentication process.单击按钮是身份验证过程的一部分。

The problem is that the third-party website's server enforces the same-origin request policy, so a simple AJAX request with application/x-www-form-urlencoded won't do the job - it will get denied due to CORS.问题是第三方网站的服务器强制执行同源请求策略,因此带有 application/x-www-form-urlencoded 的简单 AJAX 请求将无法完成这项工作 - 由于 CORS 将被拒绝。 As it stands, the only workaround is to somehow click on that button.就目前而言,唯一的解决方法是以某种方式单击该按钮。

How do I do this in nodejs, where there is no window, DOM, etc...?如何在没有 window、DOM 等的 nodejs 中执行此操作?

The CORS mechanism is specifically intended to regulate interactions between a browser and a server. CORS 机制专门用于规范浏览器和服务器之间的交互。 The server sets the rules for how other origins are allowed to interact with it, but ultimately it relies on the browser to enforce these rules - and the browser actually provides the garantee that it will.服务器设置允许其他来源与其交互的规则,但最终它依赖于浏览器来强制执行这些规则 - 并且浏览器实际上提供了它会的保证。

Server to server interactions on the other hand aren't subject to CORS, so you can freely interact with other origins directly from your server regardless.另一方面,服务器到服务器的交互不受 CORS 的约束,因此您可以直接从您的服务器自由地与其他来源进行交互。

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

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

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