SaeTOAuthV2::getAccessToken PHP Method

getAccessToken() public method

对应API:{@link http://open.weibo.com/wiki/OAuth2/access_token OAuth2/access_token}
public getAccessToken ( string $type = 'code', array $keys ) : array
$type string 请求的类型,可以为:code, password, token
$keys array 其他参数: - 当$type为code时: array('code'=>..., 'redirect_uri'=>...) - 当$type为password时: array('username'=>..., 'password'=>...) - 当$type为token时: array('refresh_token'=>...)
return array
    function getAccessToken($type = 'code', $keys)
    {
        $params = array();
        $params['client_id'] = $this->client_id;
        $params['client_secret'] = $this->client_secret;
        if ($type === 'token') {
            $params['grant_type'] = 'refresh_token';
            $params['refresh_token'] = $keys['refresh_token'];
        } elseif ($type === 'code') {
            $params['grant_type'] = 'authorization_code';
            $params['code'] = $keys['code'];
            $params['redirect_uri'] = $keys['redirect_uri'];
        } elseif ($type === 'password') {
            $params['grant_type'] = 'password';
            $params['username'] = $keys['username'];
            $params['password'] = $keys['password'];
        } else {
            throw new OAuthException("wrong auth type");
        }
        $response = $this->oAuthRequest($this->accessTokenURL(), 'POST', $params);
        $token = json_decode($response, true);
        if (is_array($token) && !isset($token['error'])) {
            $this->access_token = $token['access_token'];
            //$this->refresh_token = $token['refresh_token'];
        } else {
            throw new OAuthException("get access token failed." . $token['error']);
        }
        return $token;
    }

Usage Example

 /**
  * sina绑定
  */
 function bindSina()
 {
     $code = $this->trimmed('code');
     if (empty($code)) {
         $this->clientError('cannot find sina code, oauth failed', $code);
         exit;
     }
     $keys = array();
     $keys['code'] = $code;
     $keys['redirect_uri'] = WB_CALLBACK_URL;
     try {
         $sinaOauth = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
         $token = $sinaOauth->getAccessToken('code', $keys);
     } catch (OAuthException $e) {
         $this->clientError("oauth failed {$e}", 400);
         exit;
     }
     $url = 'https://api.weibo.com/2/users/show.json?' . http_build_query(array('access_token' => $token['access_token'], 'uid' => $token['uid']));
     $user = json_decode(file_get_contents($url));
     if (array_key_exists("error", $user)) {
         $this->clientError($user, 400);
     }
     $userOption = array('via' => 'weibo', 'uid' => $user->id, 'screen_name' => $user->screen_name, 'name' => $user->name, 'location' => $user->location, 'description' => $user->description, 'image' => $user->profile_image_url, 'access_token' => $token->access_token, 'expire_at' => $token->expires, 'refresh_token' => $token->refresh_token);
     $this->bind_common($user->id, User::PLATFORM_TYPE_SINA, $userOption);
 }
All Usage Examples Of SaeTOAuthV2::getAccessToken