Oschina Openapi PHP Call

Source: Internet
Author: User

The application is based on Yii. The current process is as follows:

    1. Generate Oschina Authorization link

    2. After the user accesses the link, they get the code

    3. 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

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.