创建API
curl -i -X POST \
--url http://localhost:8001/apis/ \
--data 'name=example-api' \
--data 'uris=/user' \
--data 'upstream_url=http://test.my'
返回值
{
"created_at":1513756862000,
"strip_uri":true,
"id":"47b03eba-2394-448e-99cd-5b2b9e43ead7",
"name":"example-api",
"http_if_terminated":false,
"preserve_host":false,
"upstream_url":"http://test.my",
"uris":[
"/user"
],
"upstream_connect_timeout":60000,
"upstream_send_timeout":60000,
"upstream_read_timeout":60000,
"retries":5,
"https_only":false
}
关联oauth插件
curl -X POST \http://localhost:8001/apis/example-api/plugins \
--data "name=oauth2" \
--data "config.enable_authorization_code=true" \
--data "config.scopes=email,phone,address" \
--data "config.mandatory_scope=true"
返回值
{
"created_at":1513757015000,
"config":{
"token_expiration":7200,
"mandatory_scope":true,
"hide_credentials":false,
"enable_authorization_code":true,
"enable_implicit_grant":false,
"global_credentials":false,
"scopes":[
"email",
"phone",
"address"
],
"enable_password_grant":false,
"accept_http_if_already_terminated":false,
"anonymous":"",
"enable_client_credentials":false,
"provision_key":"qKu4d1T3XfJUI1bFz5I36zTelX3Bg2xs"
},
"id":"f34b7df6-a43f-4872-8958-61838392fc25",
"name":"oauth2",
"api_id":"47b03eba-2394-448e-99cd-5b2b9e43ead7",
"enabled":true
}
创建消费者
curl -X POST http://localhost:8001/consumers/ \
--data "username=user123"
返回值
{
"created_at":1513756931000,
"username":"user123",
"id":"96a3dddd-3e04-45f2-aaf0-ab4fd63a322e"
}
创建应用
curl -X POST http://localhost:8001/consumers/96a3dddd-3e04-45f2-aaf0-ab4fd63a322e/oauth2 \
--data "name=test-app" \
--data "redirect_uri=http://test.my"
返回值
{
"client_id":"7IzBLyXHFTptd3kuHNpGU4z3hHql7NMD",
"created_at":1513756974000,
"id":"ac02108b-2ff0-4866-b9a5-2b89360c3898",
"redirect_uri":[
"http://test.my"
],
"name":"test-app",
"client_secret":"NLx8FJtSFXvu9I3WtMjwCNmzaNWn0D4m",
"consumer_id":"96a3dddd-3e04-45f2-aaf0-ab4fd63a322e"
}
查看应用信息
curl localhost:8001/oauth2?client_id=7IzBLyXHFTptd3kuHNpGU4z3hHql7NMD
返回值
{
"total":1,
"data":[
{
"created_at":1513756974000,
"client_id":"7IzBLyXHFTptd3kuHNpGU4z3hHql7NMD",
"id":"ac02108b-2ff0-4866-b9a5-2b89360c3898",
"redirect_uri":[
"http://test.my"
],
"name":"test-app",
"client_secret":"NLx8FJtSFXvu9I3WtMjwCNmzaNWn0D4m",
"consumer_id":"96a3dddd-3e04-45f2-aaf0-ab4fd63a322e"
}
]
}
获取Token
获取code
curl -X POST https://localhost:8443/user/oauth2/authorize \
--data "client_id=7IzBLyXHFTptd3kuHNpGU4z3hHql7NMD" \
--data "response_type=code" \
--data "provision_key=qKu4d1T3XfJUI1bFz5I36zTelX3Bg2xs" \
--data "authenticated_userid=0" \
--data "scope=email" \
--insecure
返回值
{
"redirect_uri":"http://test.my?code=PiY5UtiNY3FnFi1XRGPFKoPOodxtuPx1"
}
获取Token
curl -X POST https://localhost:8443/user/oauth2/token \
--data client_id=7IzBLyXHFTptd3kuHNpGU4z3hHql7NMD \
--data client_secret=NLx8FJtSFXvu9I3WtMjwCNmzaNWn0D4m \
--data provision_key=qKu4d1T3XfJUI1bFz5I36zTelX3Bg2xs \
--data code=PiY5UtiNY3FnFi1XRGPFKoPOodxtuPx1 \
--data grant_type=authorization_code \
--insecure
返回值
{
"refresh_token":"ipbs2amg6kLpTMwWbafORFSDhBPTVogG",
"token_type":"bearer",
"access_token":"K0BcshombAFvRi04sS2mdm6RlgGHw4uG",
"expires_in":7200
}
验证Token
curl -X GET https://localhost:8443/user?access_token=K0BcshombAFvRi04sS2mdm6RlgGHw4uG \
--insecure
返回值
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
忽略返回值哈;
删除Token
curl -X DELETE http://localhost:8001/oauth2_tokens/K0BcshombAFvRi04sS2mdm6RlgGHw4uG \
--insecure
没有返回值
再次请求api
curl -X GET https://localhost:8443/user?access_token=K0BcshombAFvRi04sS2mdm6RlgGHw4uG \
--insecure
返回值
{
"error_description":"The access token is invalid or has expired",
"error":"invalid_token"
}