我们如何发送的OAuth2.0与爱可信在JS作出反应

问题描述 投票:0回答:1

我的一个身份验证问题的工作,其中我要实现我的应用程序做出反应的OAuth2.0身份验证。有没有我可以使用基于爱可信承诺库,验证任何方式???

reactjs oauth-2.0 axios
1个回答
2
投票

你必须通过你的令牌在头中。

见下文;

const instance = axios.create({
  baseURL: 'http://localhost/api/',
  headers: {'Authorization': 'basic '+ token}
});

instance.get('/path')
.then(response => {
    return response.data;
})

要么

一旦你得到你的令牌在浏览器中设置的授权cookie。浏览器总是会发送授权cookie中向服务器发出的每个请求。你将不必通过爱可信GET / POST传递给它。

更新:

为了获得从服务器的OAuth访问令牌,通过CLIENT_ID,client_secret,范围和grant_type如下:

var axios = require("axios");

axios.request({
  url: "/oauth/token",
  method: "post",
  baseURL: "http://sample.oauth.server.com/",
  auth: {
    username: "myUsername", // This is the client_id
    password: "myPassword" // This is the client_secret
  },
  data: {
    "grant_type": "client_credentials",
    "scope": "public"    
  }
}).then(respose => {
  console.log(respose);  
}); 

我假设你正在使用grant_type =“client_credentials”,如果没有,那么根据您使用grant_type,你也必须相应地改变请求参数。

© www.soinside.com 2019 - 2024. All rights reserved.