后台管理授权认证
鉴权方式
即速云开放 API 授权通过 Access Token 作为接口调用的凭证,在对开放 API 发起请求时,均需要在 HTTP Header 加入以下授权参数:
Authorization: Bearer <Access Token>
或者在请求接口url参数中带上?access_token={Access Token}
授权流程
+--------+ 用户访问后台管理 +--------+
| | +-----------------> | |
| | | |
| | Code | |
| | <-----------------+ | |
| Client | | 即速云 |
| | Code/ID/Secert | |
| | +-----------------> | |
| | | |
| | Access Token | |
| | <-----------------+ | |
+--------+ +--------+
ID/Secert 为即速云应用的 ClientID、ClientSecret,可通过即速云管理后台进行获取。
获取 Access Token
获取 Access Token 需要经过以下两个步骤
获取 code (用户静默授权)
假设插件设置的后台访问链接为:http://www.xxx.com
用户访问插件设置的后台url链接会自动带上code参数, 如:
http://www.xxx.com?code={code}
使用 code 获取 Access Token
接口地址
POST https://open.jisuapp.cn/OAuth2/token/
参数说明
Content-Type: application/x-www-form-urlencoded
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| client_id | String | Y | 即速云应用的 ClientID |
| client_secret | String | Y | 即速云应用的 ClientSecret |
| code | String | Y | 授权码,通过上一步获取到的 |
| grant_type | String | Y | 授权类型,这里需指定为 "authorization_code" |
| scope | String | Y | 授权范围,这里需指定为 "basic" |
返回参数
| 参数 | 类型 | 说明 |
|---|---|---|
| access_token | String | 用户授权的唯一票据 |
| token_type | String | token 类型 |
| expires_in | Number | access_token 过期时间 |
| refresh_token | String | 用于刷新授权有效期 |
| scope | String | 权限范围 |
使用 refresh_token 刷新 Access Token
接口地址
POST https://open.jisuapp.cn/OAuth2/token/
参数说明
Content-Type: application/x-www-form-urlencoded
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| client_id | String | Y | 即速云应用的 ClientID |
| client_secret | String | Y | 即速云应用的 ClientSecret |
| refresh_token | String | Y | 刷新码,通过上一步获取到的 |
| grant_type | String | Y | 授权类型,这里需指定为 "refresh_token" |
| scope | String | Y | 授权范围,这里需指定为 "basic" |
返回参数
| 参数 | 类型 | 说明 |
|---|---|---|
| access_token | String | 用户授权的唯一票据 |
| token_type | String | token 类型 |
| expires_in | Number | access_token 过期时间 |
| scope | String | 权限范围 |
代码示例
var request = require('request');
var http = require('http');
var url = require("url");
var querystring = require("querystring");
http.createServer(function(req,res){
//获取返回的url对象的query属性值
var arg = url.parse(req.url).query;
//将arg参数字符串反序列化为一个对象
var params = querystring.parse(arg);
// 获取 code
var code = params.code;
}).listen(8080,'127.0.0.1');
getToken(code); // 回调调用 getToken 函数
// 获取 token
function getToken(code) {
var opt = {
uri: 'https://open.jisuapp.cn/OAuth2/token/',
method: 'POST',
formData: { // 指定 data 以 "Content-Type": "application/x-www-form-urlencoded" 传送
client_id: 'a4d2d62965ddb57fa4xx',
client_secret: 'e5802b40135baab9b4e84e35bed058a264c37dxx',
grant_type: 'authorization_code',
scope: 'basic',
code,
}
}
request(opt, function(err, res, body) {
let token = JSON.parse(body).access_token
})
}
<?php
// 获取 code
$code = $_GET['code'];
// 使用 code 获取 Access Token
$param = array(
'client_id' => "{$client_id}",
'client_secret' => "{$client_secret}",
'code' => "{$code}",
'grant_type' => 'authorization_code'
);
$access_token_url = 'https://open.jisuapp.cn/OAuth2/token/';
$access_token = postData($access_token_url, $param, 'application/x-www-form-urlencoded'); // 获取到的 Access Token
// 封装请求函数
function postData($url, $param, $content_type = 'application/json') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 设置允许重定向
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLINFO_CONTENT_TYPE, $content_type); // 设置 Content-Type,默认 application/json
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
开发者需要保证 Access Token 的安全性