使用Postman生成访问令牌

时间:2018-05-17 19:18:42

标签: postman django-oauth

我使用Django REST FramewordDjango oAuth Toolkit编写了API进行oauth2身份验证,并使用Postman来测试我的API授权流程。

我必须发送以下卷曲请求

curl -X POST -d "grant_type=password&username=<user>&password=<password>" -u "<client_id>:<client_secret" http://127.0.0.1:3333/auth/token/

我只需使用Postman access_token窗口生成Get Access Token

enter image description here

但我想通过发送请求并使用请求表单传递数据来做到这一点,这样我就可以测试API并生成auth的文档。

现在,我可以在form-data中传递用户数据(用户名,密码)但是如何传递client_idclient_secret

enter image description here

2 个答案:

答案 0 :(得分:0)

curl加密-u参数的值,我们可以使用-v(详细)选项查看。

因此,要收集标头的授权值,请使用curl命令使用-v一次。它将打印原始请求,如下所示: -

$ curl -X POST -d "grant_type=password&username=<user>&password=<password>" -u "client_id:client_secret" http://127.0.0.1:3000 -v
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: http://127.0.0.1:3000/
*   Trying 127.0.0.1...
* TCP_NODELAY set 
* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
* Server auth using Basic with user 'client_id'
> POST / HTTP/1.1
> Host: 127.0.0.1:3000
> Authorization: Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 55
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 55 out of 55 bytes
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Date: Sat, 19 May 2018 07:09:35 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked
< 

在上面的详细日志中,我们可以看到键值对为

> Authorization: Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=

收集这些密钥后,作为&#34;授权&#34;和值为&#34;基本Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ =&#34;,您可以通过邮递员在请求的标题中使用它们。 &#34;基本Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ =&#34;是使用-u&#34; client_id生成的加密值:client_secret&#34; curl选项。

希望这能解决使用邮递员请求的身份验证问题。

答案 1 :(得分:0)

要获得完整的Postman答案,可以通过请求前脚本来实现。客户端ID和客户端机密仅通过base64编码方案进行编码。只需这样做:

enter image description here

请注意, client_id_client_secret是环境变量。如果您不想这样做,请放下第一行,并将您的客户端ID和密码硬编码到CryptoJS.enc.Utf8.parse('my-trusted-client:mysecret'),其中“ my-trusted-客户端”是客户端ID,“ mysecret”是客户端密码。

这是复制/粘贴喜悦的代码。

let keys = pm.environment.get('client_id_client_secret');
let encoded = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(keys));
pm.environment.set("base64_client_id_client_secret", encoded);

现在,创建一个标题并包含您创建的变量:

enter image description here

该图片的价值部分:

Basic {{base64_client_id_client_secret}}

现在...只是邮递员的幸福。

相关问题