1
头图

背景介绍

在ONES开放体系下,OpenAPI是ONES与外部系统对接的重要连接方式。
开发者可以参看https://developer.ones.cn/zh-CN/docs/abilities/openapi/ 来了解其基本的使用方式。

Postman 是一款广泛应用于 API开发、测试和管理的协作工具,支持发送HTTP请求(如GET、POST)、调试接口、自动化测试及生成文档。简单来说,它让开发者无需编写代码即可快速验证接口功能,并支持团队协作与持续集成。

目标

使用Postman工具,在ONES中的凭据管理页面创建凭据, 并使用postman和凭据进行ONES OpenAPI调用。

(这种类型凭据的授权流程是标准的OAuth2.0 RFC6749 - Authorization Code流程)。

1. 创建Postman Mock Server

创建指引文档见官方:https://learning.postman.com/docs/design-apis/mock-apis/set-u...
注意:需要在workspace中打开这个功能 image.png
  1. 创建一个mock server: ones_test_oauth_app
  2. server中提供一个GET方法,路径是oauth_redirect, 配置默认返回结果"ok",创建成功后如下图所示。

    image.png

    image.png

  3. 此时可以通过curl进行测试

    curl --location 'https://e41fe905-554c-45cb-**.mock.pstmn.io/oauth_redirect' \
    --data ''
  4. 测试成功示例
    image.png

2. 在ONES中创建凭据

  1. 新建时Redirect URI使用的地址是'https://e41fe905-554c-45cb-**.mock.pstmn.io/oauth_redirect'
    image.png
  2. 创建成功获得client_idclient_secret
    image.png

3. OAuth授权获取access_token

  1. 浏览器输入地址'https://{your_ones_domain}/oauth2/authorize?client_id={client_id}&response_type=code'跳转到授权页面
    image.png
  2. 点击同意后,查看postman mock server日志,获取到code。浏览器也可以看到
    image.png
  3. 创建一个POST接口,如下图。注意使用x-www-form-urlencoded的body类型。
    image.png
  4. 调用返回获取access_token
    image.png

4. 使用access_token调用open_api

将access_token按照如下方式,调用接口,成功返回数据!
image.png


马尔文999
12 声望0 粉丝