The application is based on Yii. The current process is as follows:
-
Generate Oschina Authorization link
-
After the user accesses the link, they get the code
-
Use code in exchange for Access_token
Code:
<?php/** * @author xialei <[email protected]> */class oschinaoauth extends CComponent{ public $ak; public $sk; public $callback; private $host = ' https://www.oschina.net '; public function init () { } /** * Get Authorization links * @return string */ public Function getredirecturl () { $params = array ( ' Response_ Type ' => ' code ', ' client_id ' => $this->ak, ' Redirect_uri ' => yii::app ()->createabsoluteurl ($this->callback) ); return $this->host . '/action/oauth2/authorize? ' . http_build_query ($params); } /** * get accesstoken * @ param $code * @return string * @throws CException * @throws Exception */ public Function getaccesstoken ($code) { $params = array ( ' client_id ' => $this->ak, ' Client_secret ' => $this->sk, ' Grant_type ' => ' authorization_code ', ' code ' => $code, ' dataType ' => ' json ' ; $url = $this->host . '/action/openapi/token '; $resp = request::p ost ($url, $params); $data = json_decode ($resp, true); return $data; } public function refreshaccesstoken () { }}
<?php/** * @author xialei <[email protected]> */class Oauthcontroller extends controller{public function Actionc Allback ($code, $state) {$data = Yii::app ()->oauth->getaccesstoken ($code); Print_r ($data); Public Function Actionredirect () {$url = Yii::app ()->oauth->getredirecturl (); $this->redirect ($url); }}
The current problem is that you can get code, but use code to Oschina get Access_token when the error.
Oschina Openapi PHP Call