授权认证

鉴权方式

知晓云开放 API 授权通过 Access Token 作为接口调用的凭证,在对开放 API 发起请求时,均需要在 HTTP Header 加入以下授权参数:

  1. Authorization: Bearer <Access Token>

授权流程

  1. +--------+ ID/Secrct +--------+
  2. | | +-----------------> | |
  3. | | | |
  4. | | Code | |
  5. | | <-----------------+ | |
  6. | Client | | 知晓云 |
  7. | | Code | |
  8. | | +-----------------> | |
  9. | | | |
  10. | | Access Token | |
  11. | | <-----------------+ | |
  12. +--------+ +--------+

info
ID/Secert 为知晓云应用的 ClientIDClientSecret,可通过知晓云管理后台进行获取。

获取 Access Token

获取 Access Token 需要经过以下两个步骤

获取 code

接口

POST https://cloud.minapp.com/api/oauth2/hydrogen/openapi/authorize/

参数说明

Content-Type: application/json

参数 类型 必填 说明
client_id String Y 知晓云应用的 ClientID
client_secret String Y 知晓云应用的 ClientSecret

返回参数

参数 类型 说明
code String 授权码
expires_in Number code 的过期时间

info
获取 Code 会经过两次的 HTTP 302 Found 跳转,开发者在实现时需要允许客户端跟随跳转。

不跟随跳转示例:

  1. $ curl ifanr.com
  2. <html>
  3. <head><title>302 Found</title></head>
  4. <body bgcolor="white">
  5. <center><h1>302 Found</h1></center>
  6. <hr><center>nginx/1.11.5</center>
  7. </body>
  8. </html>

跟随跳转示例:

  1. $ curl -L ifanr.com
  2. <!DOCTYPE html>
  3. <html lang="zh-CN">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta property="og:site_name" content="爱范儿" />
  7. <meta property="og:type" content="article" />
  8. <meta property="og:url" content="http://www.ifanr.com" />
  9. <meta name="MSSmartTagsPreventParsing" content="true"/>
  10. <meta http-equiv="imagetoolbar" content="no"/>
  11. <meta name="robots" content="all"/>
  12. <title> 爱范儿 · 报道未来,服务新生活引领者</title>
  13. ....

使用 code 获取 Access Token

接口地址

POST https://cloud.minapp.com/api/oauth2/access_token/

参数说明

Content-Type: multipart/form-data

参数 类型 必填 说明
client_id String Y 知晓云应用的 ClientID
client_secret String Y 知晓云应用的 ClientSecret
code String Y 授权码,通过上一步获取到的
grant_type String Y 授权类型,这里需指定为 “authorization_code”

返回参数

参数 类型 说明
access_token String 用户授权的唯一票据
token_type String token 类型
expires_in Number access_token 过期时间
refresh_token String 用于刷新授权有效期
scope String 权限范围

代码示例

{% tabs first=”Node”,second=”PHP” %}

{% content “first” %}

  1. var request = require('request');
  2. // 获取 code
  3. var opt = {
  4. uri: 'https://cloud.minapp.com/api/oauth2/hydrogen/openapi/authorize/',
  5. method: 'POST',
  6. json: {
  7. client_id: 'a4d2d62965ddb57fa4xx',
  8. client_secret: 'e5802b40135baab9b4e84e35bed058a264c37dxx'
  9. },
  10. jar: true, // 允许记住 cookie
  11. followAllRedirects: true, // 允许重定向
  12. }
  13. request(opt, function(err, res, body) {
  14. getToken(body.code) // 回调调用 getToken 函数
  15. })
  16. // 获取 token
  17. function getToken(code) {
  18. var opt = {
  19. uri: 'https://cloud.minapp.com/api/oauth2/access_token/',
  20. method: 'POST',
  21. formData: { // 指定 data 以 "Content-Type": "multipart/form-data" 传送
  22. client_id: 'a4d2d62965ddb57fa4xx',
  23. client_secret: 'e5802b40135baab9b4e84e35bed058a264c37dxx',
  24. grant_type: 'authorization_code',
  25. code,
  26. }
  27. }
  28. request(opt, function(err, res, body) {
  29. let token = JSON.parse(body).access_token
  30. })
  31. }

{% content “second” %}

  1. <?php
  2. $param = array(
  3. 'client_id' => "{$client_id}",
  4. 'client_secret' => "{$client_secret}"
  5. );
  6. // 获取 code
  7. $code_url = 'https://cloud.minapp.com/api/oauth2/hydrogen/openapi/authorize/';
  8. $code_data = postData($code_url, json_encode($param));
  9. $code_data = json_decode($code_data, true);// 使用 code 获取 Access Token
  10. $param['code'] = $code_data['code'];
  11. $param['grant_type'] = 'authorization_code';
  12. $access_token_url = 'https://cloud.minapp.com/api/oauth2/access_token/';
  13. $access_token = postData($access_token_url, $param, 'multipart/form-data'); // 获取到的 Access Token
  14. // 封装请求函数
  15. function postData($url, $param, $content_type = 'application/json') {
  16. $ch = curl_init();
  17. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  18. curl_setopt($ch, CURLOPT_URL, $url);
  19. curl_setopt($ch, CURLOPT_POST, true);
  20. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  21. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 设置允许重定向
  22. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  23. curl_setopt($ch, CURLOPT_COOKIEFILE, '');
  24. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  25. curl_setopt($ch, CURLOPT_COOKIESESSION, true);
  26. curl_setopt($ch, CURLINFO_CONTENT_TYPE, $content_type); // 设置 Content-Type,默认 application/json
  27. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  28. $response = curl_exec($ch);
  29. curl_close($ch);
  30. return $response;
  31. }

{% endtabs %}

danger
开发者需要保证 Access Token 的安全性