/** * Exchange a provided OAuth code for an OAuth access token, 'expires in' * value and refresh token. * * @param $client_id int The Client ID of your registered OAuth application. * @param $client_secret string The Client Secret of your registered OAuth application. * @param $redirect_uri string The Redirect URI of your registered OAuth application. * @param $code string The unique OAuth code to be exchanged for an access token. * @return OP_CS_REST_Wrapper_Result A successful response will be an object of the form * { * 'access_token' => The access token to use for API calls * 'expires_in' => The number of seconds until this access token expires * 'refresh_token' => The refresh token to refresh the access token once it expires * } * @access public **/ public static function exchange_token($client_id, $client_secret, $redirect_uri, $code) { $body = "grant_type=authorization_code"; $body .= "&client_id=" . urlencode($client_id); $body .= "&client_secret=" . urlencode($client_secret); $body .= "&redirect_uri=" . urlencode($redirect_uri); $body .= "&code=" . urlencode($code); $options = array('contentType' => 'application/x-www-form-urlencoded'); $wrap = new OP_CS_REST_Wrapper_Base(NULL, 'https', CS_REST_LOG_NONE, CS_HOST, NULL, new OP_CS_REST_DoNothingSerialiser(), NULL); return $wrap->post_request(CS_OAUTH_TOKEN_URI, $body, $options); }
/** * Refresh the current OAuth token using the current refresh token. * @access public */ function refresh_token() { if (!isset($this->_default_call_options['authdetails']) || !isset($this->_default_call_options['authdetails']['refresh_token'])) { trigger_error('Error refreshing token. There is no refresh token set on this object.', E_USER_ERROR); return array(NULL, NULL, NULL); } $body = "grant_type=refresh_token&refresh_token=" . urlencode($this->_default_call_options['authdetails']['refresh_token']); $options = array('contentType' => 'application/x-www-form-urlencoded'); $wrap = new OP_CS_REST_Wrapper_Base(NULL, 'https', CS_REST_LOG_NONE, CS_HOST, NULL, new OP_CS_REST_DoNothingSerialiser(), NULL); $result = $wrap->post_request(CS_OAUTH_TOKEN_URI, $body, $options); if ($result->was_successful()) { $access_token = $result->response->access_token; $expires_in = $result->response->expires_in; $refresh_token = $result->response->refresh_token; $this->_default_call_options['authdetails'] = array('access_token' => $access_token, 'refresh_token' => $refresh_token); return array($access_token, $expires_in, $refresh_token); } else { trigger_error('Error refreshing token. ' . $result->response->error . ': ' . $result->response->error_description, E_USER_ERROR); return array(NULL, NULL, NULL); } }