Created
January 8, 2013 04:29
-
-
Save anonymous/4481160 to your computer and use it in GitHub Desktop.
OAUTH2 for Bufferapp API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Thirdparty extends CI_Controller { | |
//bufferapp application token | |
private $access_token = ''; | |
private $client_id = ''; | |
private $client_secret = ''; | |
private $callback_url = 'thirdparty/auth/'; | |
private $token_url = 'https://api.bufferapp.com/1/oauth2/token.json'; | |
// Create Post Url | |
private $create_url = 'https://api.bufferapp.com/1/updates/create.json?access_token='; | |
// Media Attributes WIP | |
private $media = 'http://bufferapp.com'; | |
// Place holder for msgs to be buffered | |
private $queue = array(); | |
function __construct(){ | |
parent::__construct(); | |
$this->load->library('session'); | |
$this->load->model('thirdparty_model'); | |
} | |
// Authenticate buffer account for user | |
public function auth(){ | |
switch ($this->session->userdata('stage')) { | |
case 2: | |
// Store Code | |
$this->session->set_userdata(array('code' => $this->input->get('code'))); | |
$this->session->set_userdata(array('stage' => 3)); | |
header('Location: '.base_url() . $this->callback_url); | |
break; | |
case 3: | |
$post_data = array( | |
'client_id' => urlencode($this->client_id), | |
'client_secret' => urlencode($this->client_secret), | |
'callback_url' => urlencode(base_url() . $this->callback_url), | |
'code' => urlencode($this->session->userdata('code')), | |
'grant_type' => 'authorization_code' | |
); | |
// Initializing curl | |
$ch = curl_init($this->token_url); | |
// Setting curl options | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data ); | |
// Getting results | |
$this->result = curl_exec($ch); // Getting jSON result string | |
$this->session->set_userdata(array('stage' => 4)); | |
// // Get profile data | |
$data = file_get_contents('https://api.bufferapp.com/1/profiles.json?access_token=' . $this->access_token); | |
break; | |
default: // Stage 1 | |
header( "Location: " . 'https://bufferapp.com/oauth2/authorize?client_id='.$this->client_id.'&redirect_uri='.base_url() . $this->callback_url.'&response_type=code'); // Send Request | |
$this->session->set_userdata(array('stage' => 2)); | |
break; | |
} | |
} | |
// store user token for buffer account | |
function storeProfileAuth($data = array()){ | |
$this->thirdparty_model->storeProfileAuth($data); | |
} | |
} | |
/* End of file thirdparty.php */ | |
/* Location: ./application/modules/thirdparty/controllers/thirdparty.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment