/** * 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 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 CS_REST_Wrapper_Base(NULL, 'https', CS_REST_LOG_NONE, CS_HOST, NULL, new CS_REST_DoNothingSerialiser(), NULL); return $wrap->post_request(CS_OAUTH_TOKEN_URI, $body, $options); }
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 CS_REST_Wrapper_Base(NULL, 'https', CS_REST_LOG_NONE, CS_HOST, NULL, new 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); } }
/** * Constructor. * @param $client_id string The client id that the people belong to * @param $auth_details array Authentication details to use for API calls. * This array must take one of the following forms: * If using OAuth to authenticate: * array( * 'access_token' => 'your access token', * 'refresh_token' => 'your refresh token') * * Or if using an API key: * array('api_key' => 'your api key') * @param $protocol string The protocol to use for requests (http|https) * @param $debug_level int The level of debugging required CS_REST_LOG_NONE | CS_REST_LOG_ERROR | CS_REST_LOG_WARNING | CS_REST_LOG_VERBOSE * @param $host string The host to send API requests to. There is no need to change this * @param $log CS_REST_Log The logger to use. Used for dependency injection * @param $serialiser The serialiser to use. Used for dependency injection * @param $transport The transport to use. Used for dependency injection * @access public */ function __construct($client_id, $auth_details, $protocol = 'https', $debug_level = CS_REST_LOG_NONE, $host = 'api.createsend.com', $log = NULL, $serialiser = NULL, $transport = NULL) { parent::__construct($auth_details, $protocol, $debug_level, $host, $log, $serialiser, $transport); $this->set_client_id($client_id); }
/** * Constructor. * @param $auth_details array Authentication details to use for API calls. * This array must take one of the following forms: * If using OAuth to authenticate: * array( * 'access_token' => 'your access token', * 'refresh_token' => 'your refresh token') * * Or if using an API key: * array('api_key' => 'your api key') * @param $protocol string The protocol to use for requests (http|https) * @param $debug_level int The level of debugging required CS_REST_LOG_NONE | CS_REST_LOG_ERROR | CS_REST_LOG_WARNING | CS_REST_LOG_VERBOSE * @param $host string The host to send API requests to. There is no need to change this * @param $log CS_REST_Log The logger to use. Used for dependency injection * @param $serialiser The serialiser to use. Used for dependency injection * @param $transport The transport to use. Used for dependency injection * @access public */ function __construct($auth_details, $protocol = 'https', $debug_level = CS_REST_LOG_NONE, $host = 'api.createsend.com', $log = NULL, $serialiser = NULL, $transport = NULL) { parent::__construct($auth_details, $protocol, $debug_level, $host, $log, $serialiser, $transport); $this->_admins_base_route = $this->_base_route . 'admins'; }