Typescript 如何使用client-oauth2以json的形式发送?

Typescript 如何使用client-oauth2以json的形式发送?,typescript,auth0,Typescript,Auth0,所以我有这个密码 const auth0 = new ClientOAuth2({ clientId: 'my id', clientSecret: 'my secret', accessTokenUri: 'https://myorg.auth0.com/oauth/token', scopes: ['version:read'] }); auth0.credentials.getToken().then

所以我有这个密码

    const auth0 = new ClientOAuth2({
        clientId: 'my id',
        clientSecret: 'my secret',
        accessTokenUri: 'https://myorg.auth0.com/oauth/token',
        scopes: ['version:read']
    });

    auth0.credentials.getToken().then(tok => {
        console.log(tok);
    }).catch(e => console.log(e));
但我要回403

这是auth0建议我发送的内容

 calebcushing$ curl --request POST   --url https://myorg.auth0.com/oauth/token   --header 'content-type: application/json'   --data '{"client_id":"my id","client_secret":"my secret","audience":"http://localhost:4000","grant_type":"client_credentials"}'

我没有嫁给
client-oauth2
,所以如果另一个库是更好的选择,那就好了。不过我更喜欢使用oauth库,这样我就不必做所有的协调工作了。如果它在向资源服务器发出请求时只会自动重新提取令牌等,则会获得额外的积分。

如果我理解正确,我认为您正在尝试执行客户端凭据授予类型并获取Auth0管理API令牌。您可以使用auth0节点sdk()

安装软件包:

npm安装auth0


var AuthenticationClient = require('auth0').AuthenticationClient;

var auth0 = new AuthenticationClient({
  domain: '{YOUR_ACCOUNT}.auth0.com',
  clientId: '{CLIENT_ID}',
  clientSecret: '{CLIENT_SECRET}'
});

auth0.clientCredentialsGrant(
  {
    audience: 'https://{YOUR_ACCOUNT}.auth0.com/api/v2/',
    scope: '{MANAGEMENT_API_SCOPES}'
  },
  function(err, response) {
    if (err) {
      // Handle error.
    }
    console.log(response.access_token);
  }
);
您还可以使用任何HTTP客户端。例如,fetch或request


var AuthenticationClient = require('auth0').AuthenticationClient;

var auth0 = new AuthenticationClient({
  domain: '{YOUR_ACCOUNT}.auth0.com',
  clientId: '{CLIENT_ID}',
  clientSecret: '{CLIENT_SECRET}'
});

auth0.clientCredentialsGrant(
  {
    audience: 'https://{YOUR_ACCOUNT}.auth0.com/api/v2/',
    scope: '{MANAGEMENT_API_SCOPES}'
  },
  function(err, response) {
    if (err) {
      // Handle error.
    }
    console.log(response.access_token);
  }
);