donglu4633 2017-09-24 16:13
浏览 175
已采纳

如何从auth0客户端的api获取有效令牌?

I am using emberjs with auth0 to log in. Normally I enter username and password and I get a valid token which I send to api etc.

Now, I want to write some tests on api and I always need a valid token. My token expires in a day. So to run tests I always have to manually change the token sample.

So I want to get a token somehow from api? is possible and how?

  • 写回答

1条回答 默认 最新

  • duanhao7786 2017-09-25 02:43
    关注

    Very good question. An easy way to do this for tests, is to use a Client Credentials Grant Flow. Since you want an access token from within a test, you can treat this as a machine to machine flow.

    Here is an example where I am doing this from a test to receive an Auth0 Management API v2 access token. And the corresponding test too. And here is where I use that access token lookup as part of a separate test. However, the same principle applies whether it the Auth0 specific Management API or an API you have defined yourself in the Auth0 Dashboard (under APIs) using a Non-Interactive Client and your own audience.

    The "shape" of the call is pretty much as follows:

    { 
      method: 'POST',
      url: 'https://{TENANT}.auth0.com/oauth/token',
      headers: { 'content-type': 'application/json' },
      body: '{
             "client_id":"{CLIENT ID}",
             "client_secret":"{CLIENT SECRET}, 
             "audience":"https://{tenant}.auth0.com/api/v2/",
             "grant_type":"client_credentials"
      }' 
    };
    

    You call the oauth/token endpoint, and pass clientId, clientSecret, audience (for that api), and grant-type (client_credentials). If doing this with emberjs / php - it is very similar - If you create a new Client in Auth0 Dashboard and choose Non Interactive client type, then hit Quick Start - it will guide you through setup (if needed). See screenshot below, and feel free to leave comments if you have any probs.

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大