public function upgrade_token($code) {
        $callbackurl = self::callback_url();
        $params = array('code' => $code,
            'grant_type' => 'authorization_code',
            'redirect_uri' => $callbackurl->out(false),
        );

        if ($this->basicauth) {
            $idsecret = urlencode($this->clientid) . ':' . urlencode($this->clientsecret);
            $this->setHeader('Authorization: Basic ' . base64_encode($idsecret));
        } else {
            $params['client_id'] = $this->clientid;
            $params['client_secret'] = $this->clientsecret;
        }

        // Requests can either use http GET or POST.
        if ($this->use_http_get()) {
            $response = $this->get($this->token_url(), $params);
        } else {
            $response = $this->post($this->token_url(), $this->build_post_data($params));
        }

        if ($this->info['http_code'] !== 200) {
            throw new moodle_exception('Could not upgrade oauth token');
        }

        $r = json_decode($response);

        if (is_null($r)) {
            throw new moodle_exception("Could not decode JSON token response");
        }

        if (!empty($r->error)) {
            throw new moodle_exception($r->error . ' ' . $r->error_description);
        }

        if (!isset($r->access_token)) {
            return false;
        }

        if (isset($r->refresh_token)) {
            $this->refreshtoken = $r->refresh_token;
        }

        // Store the token an expiry time.
        $accesstoken = new stdClass;
        $accesstoken->token = $r->access_token;
        if (isset($r->expires_in)) {
            // Expires 10 seconds before actual expiry.
            $accesstoken->expires = (time() + ($r->expires_in - 10));
        }
        $accesstoken->scope = $this->scope;
        // Also add the scopes.
        self::$upgradedcodes[] = $code;
        $this->store_token($accesstoken);

        return true;
    }
public function upgrade_token($code) {
        $callbackurl = self::callback_url();
        $params = array('code' => $code,
            'grant_type' => 'authorization_code',
            'redirect_uri' => $callbackurl->out(false),
        );

        if ($this->basicauth) {
            $idsecret = urlencode($this->clientid) . ':' . urlencode($this->clientsecret);
            $this->setHeader('Authorization: Basic ' . base64_encode($idsecret));
        } else {
            $params['client_id'] = $this->clientid;
            $params['client_secret'] = $this->clientsecret;
        }

        // Requests can either use http GET or POST.
        if ($this->use_http_get()) {
            $response = $this->get($this->token_url(), $params);
        } else {
            $response = $this->post($this->token_url(), $this->build_post_data($params));
        }

        if ($this->info['http_code'] !== 200) {
            throw new moodle_exception('Could not upgrade oauth token');
        }

        $r = json_decode($response);

        if (is_null($r)) {
            throw new moodle_exception("Could not decode JSON token response");
        }

        if (!empty($r->error)) {
            throw new moodle_exception($r->error . ' ' . $r->error_description);
        }

        if (!isset($r->access_token)) {
            return false;
        }

        if (isset($r->refresh_token)) {
            $this->refreshtoken = $r->refresh_token;
        }

        // Store the token an expiry time.
        $accesstoken = new stdClass;
        $accesstoken->token = $r->access_token;
        if (isset($r->expires_in)) {
            // Expires 10 seconds before actual expiry.
            $accesstoken->expires = (time() + ($r->expires_in - 10));
        }
        $accesstoken->scope = $this->scope;
        // Also add the scopes.
        self::$upgradedcodes[] = $code;
        $this->store_token($accesstoken);

        return true;
    }
public function upgrade_token ( $ code ) { $ callbackurl = self :: callback_url ( ) ; $ params = array ( 'code' => $ code , 'grant_type' => 'authorization_code' , 'redirect_uri' => $ callbackurl -> out ( false ) , ) ; if ( $ this -> basicauth ) { $ idsecret = urlencode ( $ this -> clientid ) . ':' . urlencode ( $ this -> clientsecret ) ; $ this -> setHeader ( 'Authorization: Basic ' . base64_encode ( $ idsecret ) ) ; } else { $ params [ 'client_id' ] = $ this -> clientid ; $ params [ 'client_secret' ] = $ this -> clientsecret ; } // Requests can either use http GET or POST. if ( $ this -> use_http_get ( ) ) { $ response = $ this -> get ( $ this -> token_url ( ) , $ params ) ; } else { $ response = $ this -> post ( $ this -> token_url ( ) , $ this -> build_post_data ( $ params ) ) ; } if ( $ this -> info [ 'http_code' ] !== 200 ) { throw new moodle_exception ( 'Could not upgrade oauth token' ) ; } $ r = json_decode ( $ response ) ; if ( is_null ( $ r ) ) { throw new moodle_exception ( "Could not decode JSON token response" ) ; } if ( ! empty ( $ r -> error ) ) { throw new moodle_exception ( $ r -> error . ' ' . $ r -> error_description ) ; } if ( ! isset ( $ r -> access_token ) ) { return false ; } if ( isset ( $ r -> refresh_token ) ) { $ this -> refreshtoken = $ r -> refresh_token ; } // Store the token an expiry time. $ accesstoken = new stdClass ; $ accesstoken -> token = $ r -> access_token ; if ( isset ( $ r -> expires_in ) ) { // Expires 10 seconds before actual expiry. $ accesstoken -> expires = ( time ( ) + ( $ r -> expires_in - 10 ) ) ; } $ accesstoken -> scope = $ this -> scope ; // Also add the scopes. self :: $ upgradedcodes [ ] = $ code ; $ this -> store_token ( $ accesstoken ) ; return true ; }
Upgrade a authorization token from oauth 2.0 to an access token

@param string $code the code returned from the oauth authenticaiton
@return boolean true if token is upgraded succesfully
Upgrade a authorization token from oauth 2 . 0 to an access token