Oschina Openapi PHP Call

Source: Internet
Author: User
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:

  */class Oschinaoauth extends ccomponent{public $ak, public $sk, public $callback, private $host = ' HTTPS://WWW.OSCHINA.N ET '; Public Function init () {}/** * get authorization link * @return String */Public Function Getredirecturl () {$params = Array (' re Sponse_type ' + ' code ', ' client_id ' = $this->ak, ' Redirect_uri ' and Yii::app ()->createabsoluteurl ($th  Is->callback)); Return $this->host. '/action/oauth2/authorize? '. Http_build_query ($params); }/** * Get Accesstoken * @param $code * @return String * @throws CException * @throws Exception */Public function ge Taccesstoken ($code) {$params = array (' client_id ' = = $this->ak, ' client_secret ' = = $this->sk, ' Gran  T_type ' + ' authorization_code ', ' code ' = $code, ' dataType ' and ' json '); $url = $this->host.  '/action/openapi/token ';  $resp = Request::p ost ($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); }}

The current problem is that you can get code, but use code to Oschina get Access_token when the error.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.