1. 程式人生 > >PHP OAuth2.0 Server 搭建,問題解決持續更新中...

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:
<code class="language-text" style="border: 0px; margin: 0px; padding: 0px;">curl -u testclient:testpass http://localhost/token.php -d 'grant_type=client_credentials'</code>
在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:

<code class="language-text" style="border: 0px; margin: 0px; padding: 0px;">http://localhost/authorize.php?response_type=code&client_id=testclient&state=xyz</code>
在瀏覽器執行這個的時候,返回錯誤:
{"error":"invalid_client","error_description":"No client id supplied"}
修改資料庫表oauth_clients 欄位grant_type 的值為authorization_code即可。

Q3:

<code class="language-text" style="border: 0px; margin: 0px; padding: 0px;">curl -u testclient:testpass http://localhost/token.php -d grant_type=authorization_code&code=YOUR_CODE</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"}

成功!

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/