简体   繁体   English

在 Javascript 中对 API 进行身份验证

[英]Authenticate to API within Javascript

I have an API that I am able to authenticate against using the Postman client.我有一个可以使用 Postman 客户端进行身份验证的 API。 Using Postman I am able to enter in my username and password into the header and receive back an access token.使用 Postman,我可以在标题中输入我的用户名和密码并收到一个访问令牌。

I would like to accomplish the same authentication with a simple HTML page using Javascript.我想通过使用 Javascript 的简单 HTML 页面完成相同的身份验证。 However, I am unsure how to craft the Javascript request and pass in my username and password as I did with Postman.但是,我不确定如何像使用 Postman 一样制作 Javascript 请求并传入我的用户名和密码。

A password is normally considered private, if you include it in your javascript anyone can read it and fire requests off to the API as your user.密码通常被认为是私有的,如果您将它包含在您的 javascript 中,任何人都可以读取它并以您的用户身份向 API 发出请求。 Additionally, the browsers same-origin policy - unless configured otherwise will stop you firing ajax requests to a domain other than the one the webpage was loaded from.此外,浏览器的同源策略 - 除非以其他方式配置,否则将阻止您向加载网页的域以外的域发出 ajax 请求。

Instead you should create a proxy script in the server-side language of your choice hosted on your domain and fire your ajax requests off to this.相反,您应该使用托管在您的域上的您选择的服务器端语言创建一个代理脚本,并将您的 ajax 请求发送到此。 This script would do the relevant actions with the API keeping your credentials a secret and return the response.此脚本将使用 API 执行相关操作,将您的凭据保密并返回响应。

Under the address bar of Postman there's a link that says "Generate Code" on previous versions it was a button with a </> symbol.在 Postman 的地址栏下有一个链接,上面写着“生成代码”,在以前的版本中它是一个带有</>符号的按钮。 Clicking that link opens up a popup with a dropdownlist where Javascript is one option, this will generate the code to do the request.单击该链接会打开一个带有下拉列表的弹出窗口,其中 Javascript 是一个选项,这将生成执行请求的代码。

Let me also add to the other answers that with jQuery you can do a get request with $.get() , and a post request with $.post() .让我也补充一下其他答案,使用 jQuery 您可以使用$.get()执行 get 请求,并使用$.post()执行 post 请求。 But I would do what Vector suggests and generate the JavaScript with Postman.但我会按照 Vector 的建议去做,并用 Postman 生成 JavaScript。

You could do that with ajax call within a javascript file and you can aslo do with the xhttprequest .您可以使用 javascript 文件中的 ajax 调用来做到这一点,也可以使用 xhttprequest 来做到这一点。 example of ajax call: $.ajax({ url : url, //URL type : 'POST', // The HTTP Method data : array, contentType : 'application/json', cache : false, success : function (data) { ajax调用示例: $.ajax({ url : url, //URL type : 'POST', // HTTP Method data : array, contentType : 'application/json', cache : false, success : function (data) {

                }, 
                error       : function (err) {

            });

In this you can also add the Headers where u can your api access token在此您还可以添加标题,您可以在其中添加 api 访问令牌

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

相关问题 在 JavaScript 上验证 GitHub API - Authenticate GitHub API on JavaScript 使用 Google javascript API 进行身份验证 - Authenticate using Google javascript API Propublica API使用JavaScript验证密钥 - Propublica API authenticate key with javascript 使用JavaScript问题通过LinkedIn API进行身份验证 - Authenticate with LinkedIn api using javascript issues 验证Gcloud AutoML Vision API的Javascript程序 - Authenticate Javascript Program for Gcloud AutoML Vision API 如何自动验证Google javascript API? REST API - how do I auto authenticate google javascript api ? REST api 如何使用Facebook API对Facebook应用进行身份验证以访问公共页面供稿? - How to authenticate Facebook app to access public pages feed with javascript API? 如何对javascript使用的REST API进行身份验证(因为代码是公开的) - How to authenticate a REST API used by javascript (since the code is public) 来自javascript的office 365 graph api:如何正确验证 - office 365 graph api from javascript: how to properly authenticate 使用本机 JavaScript 向 Google Cloud 文本转语音 API 进行身份验证 - Authenticate to Google Cloud text-to-speech API using native JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM