oschina openapi php 調用

來源:互聯網
上載者:User
應用時基於yii的。目前流程如下:

產生oschina授權連結

使用者訪問該連結之後得到 code

利用code換取access_token

代碼:

 */class OschinaOauth extends CComponent{ public $ak; public $sk; public $callback; private $host = 'https://www.oschina.net'; public function init() { } /**  * 擷取授權連結  * @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); } /**  * 擷取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::post($url, $params);  $data = json_decode($resp,true);  return $data; } public function refreshAccessToken() { }}
 */class OauthController extends Controller{ public function actionCallback($code, $state) {  $data = Yii::app()->oauth->getAccessToken($code);  print_r($data); } public function actionRedirect() {  $url = Yii::app()->oauth->getRedirectUrl();  $this->redirect($url); }}

目前的問題是可以得到 code,但是利用code去oschina擷取access_token的時候報錯了。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.