PHP OAuth2.0 Server 搭建 在 Yii 框架上

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/

顶0

你可能感兴趣的:(PHP OAuth2.0 Server 搭建 在 Yii 框架上)