PHP OAuth2.0 Server 搭建

oauth2 server php

http://oauth.net/2/

Step-By-Step Walkthrough

Ref: http://bshaffer.github.io/oauth2-server-php-docs/cookbook/


Q1:
curl -u testclient:testpass http://localhost/token.php -d 'grant_type=client_credentials'
在windows下执行这步的时候返回:

C:\Users\Frank>curl -u testclient:testpass http://oauth2.dev/token.php -d 'grant_type=client_credentials'
{"error":"invalid_request","error_description":"The grant type was not specified in the request"}

将单引号去掉即可。

C:\Users\Frank>curl -u testclient:testpass http://oauth2.dev/token.php -d grant_type=client_credentials
{"access_token":"594732584f808259555411aba1f5fdcc45b99fb1","expires_in":3600,"token_type":"Bearer","scope":null}

参考:https://github.com/bshaffer/oauth2-server-php/issues/160

Q2:

http://localhost/authorize.php?response_type=code&client_id=testclient&state=xyz
在浏览器执行这个的时候,返回错误:

{"error":"invalid_client","error_description":"No client id supplied"}
修改数据库表oauth_clients 字段grant_type 的值为authorization_code即可。

Q3:

curl -u testclient:testpass http://localhost/token.php -d grant_type=authorization_code&code=YOUR_CODE
返回错误如下:

C:\Users\Frank>curl -u testclient:testpass http://oauth2.dev/token.php -d grant_type=authorization_code&code=10ad1afa9569c7796eea48dab6014b9ed2a01b50
{"error":"unsupported_grant_type","error_description":"Grant type \"authorization_code\" not supported"}'code' is not recognized as an internal or external comm
and,operable program or batch file.

原来是把server.php 里的$server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage)); 这句注释了,去掉注释依然返回错误:

C:\Users\Frank>curl -u testclient:testpass http://oauth2.dev/token.php -d granttype=authorization_code & code=10ad1afa9569c7796eea48dab6014b9ed2a01b50
{"error":"invalid_request","error_description":"Missing parameter: \"code\" isequired"}'code' is not recognized as an internal or external command,operable program or batch file.

修改参数-d为--data ,并把参数加双引号,

参考:http://www.ruanyifeng.com/blog/2011/09/curl.html

执行如下:

C:\Users\Frank>curl -u testclient:testpass http://oauth2.dev/token.php --data "grant_type=authorization_code&code=10ad1afa9569c7796eea48dab6014b9ed2a01b50"
{"error":"invalid_grant","error_description":"The authorization code has expired"}

code过期了,重新在浏览器获取一次后,执行如下:

C:\Users\Frank>curl -u testclient:testpass http://oauth2.dev/token.php --data "grant_type=authorization_code&code=45daf60218e025028ffa55564c2901d8195a4418"
{"access_token":"81e35b27e604f95676ff9cb3b2a42ac12bbc8d22","expires_in":3600,"token_type":"Bearer","scope":null,"refresh_token":"a7b20df01ce1980d0fd80ec87fc68c2313995de7"}

成功!


使用 OAuth2-Server-php 在 Yii 框架上搭建 OAuth2 Server

Ref:http://www.cnblogs.com/rereadyou/p/3448381.html


下一步:oauth2.0 client ,redis替换mysql

http://bshaffer.github.io/oauth2-server-php-docs/storage/redis/

https://github.com/nrk/predis/

http://www.cnblogs.com/weafer/archive/2011/09/21/2184059.html

https://github.com/jasonlewis/oauth2-server-redis/

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值