簡體   English   中英

如何使用 SSO oauth 2.0 登錄,然后存儲令牌以從郵遞員自動化腳本運行所有其他 API?

[英]How to login using SSO oauth 2.0 and then storing token to run all other API from postman automation script?

我無法為此過程自動化郵遞員腳本:

  1. 打開郵遞員然后在授權選項卡中選擇類型-> OAuth 2.0,添加授權數據-> 請求頭和訪問令牌-> 獲取訪問令牌填充字段-令牌名稱、授予類型-> 授權代碼、回調 URL、Auth Url、Acess Token Url , client Id, Scope, State, Client Authentication->Send as basic Auth header 然后當請求令牌時會打開一個彈出窗口供 SSO在此處輸入圖像描述

  2. 然后會彈出管理訪問令牌,然后選擇底部的使用令牌按鈕,然后運行 ​​api url,它現在在 Header 選項卡->temperory headers 中包含令牌。 如何通過將令牌存儲在環境變量中來自動執行此過程,然后使用它運行其余的 API。

我嘗試使用 Express 和 puppeteer 從 oauth 2.0 登錄訪問 access_token。

var express = require('express');
const puppeteer = require('puppeteer');
var app = express();
app.get('/', function(req, res) {
    run().then(() => console.log('Done')).catch(error => console.log(error));
    async function run(){
        const browser = await puppeteer.launch({headless: false});
        const page = await browser.newPage();
        await page.goto('https://abcd.com/authorize? 
audience=https://abcd.com&scope=openid%20email%20profile&client_id=abcd&response_type=token&redirect_uri=https://abcd.com/callback');
await new Promise(resolve => setTimeout(resolve, 5000));
    await page.focus('#email');
    await page.keyboard.type('abcd@gmail.com');
    await page.focus('#password');
    await page.keyboard.type('efghi');
    const waitForLoad = new Promise(resolve => page.on('load', () => resolve()));
    await page.evaluate(() => {
        document.querySelector('span[class="label"]').click();
    });
    await waitForLoad;
    console.log('Waiting to be redirected to the client.');
    const clientUrl = await page.evaluate(() => window.location.href);
        //1st the split is from when it encounters = in the url
    var split1 = clientUrl.split('=');
    //2nd split is when it encounters & in the 2nd object of the array
    var split2 = split1[1].split('&');
    //taking array in an object and conversing it to json
    var obj = {
      access_token: split2[0]
    }
    await browser.close();
    res.send(obj);
  };
});
app.listen(8000, function(){
    console.log('Heard on 8000');
});

這可以在郵遞員上運行以使用收到的訪問令牌運行其他 api。

使用刷新令牌 這也可以在更短的時間內通過 API 獲取刷新令牌 https://{{auth0_domain}}/oauth/token BODY-

grant_type:password      
client_id:abcdefghijklmn
audience:https://abcd.com
username:abcd
password:efgh
scope:openid profile email offline_access

在 Response 中會生成 Refresh Token,此令牌可用於獲取 accesss_token ,而無需再次生成刷新令牌。 這只是一個一次性過程,不會在一天、一周或一個月內過期。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM