【问题标题】:how can I send as json with client-oauth2?如何使用 client-oauth2 作为 json 发送?
【发布时间】:2020-05-04 17:28:59
【问题描述】:

所以我有这个代码

    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"}'
curl 'https://myorg.auth0.com/oauth/token' -H 'authority: myorg.auth0.com' -H 'accept: application/json, application/x-www-form-urlencoded' -H 'origin: http://localhost:3000' -H 'authorization: Basic encoded' -H 'content-type: application/x-www-form-urlencoded'  --data 'scope=version%3Aread&grant_type=client_credentials'

我没有与client-oauth2 结婚,所以如果另一个图书馆是更好的选择,那很好。我更喜欢使用 oauth 库,所以我不必做所有的协调舞蹈。如果它在向资源服务器发出请求时会自动重新获取令牌等,则可以加分。

【问题讨论】:

    标签: typescript auth0


    【解决方案1】:

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

    安装包:

    npm install 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 客户端。例如,获取或请求。

    https://auth0.com/docs/api-auth/tutorials/client-credentials

    【讨论】:

      猜你喜欢
      • 2019-03-14
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 2012-09-19
      • 1970-01-01
      • 2018-06-27
      相关资源
      最近更新 更多