Skip to content

Instantly share code, notes, and snippets.

@paulchubatyy
Created August 25, 2011 15:59
Show Gist options
  • Save paulchubatyy/1171022 to your computer and use it in GitHub Desktop.
Save paulchubatyy/1171022 to your computer and use it in GitHub Desktop.
Vkontakte oauth code for blog
<?php
class Controller_Profile extends Controller {
public function action_index()
{
$user = Auth::instance()->get_user();
$vkontakte_api = Vkapi::instance($user->token);
// Get user profile
$result = $vkontakte_api->getProfiles(array('uid' => $user->vk_user_id));
echo Debug::dump($result);
// Post to user's wall
$vkontakte_api->wall_post(array('message' => 'Hello World', 'friends_only' => 1));
echo Debug::dump($result);
}
}
<?php
class Controller_OAuth extends Controller {
/**
* @var OAuth2
*/
protected $oauth;
/**
* @var OAuth2_Provider
*/
protected $provider;
/**
* @var OAuth2_Client
*/
protected $client;
public function before()
{
parent::before();
$this->provider = Oauth2_Provider::factory('vkontakte');
$this->client = OAuth2_Client::factory(
Kohana::$config->load("oauth.vkontakte"));
}
public function action_authorized()
{
try {
$error = $this->request->query('error');
if ($error) {
$error_description = $this->request->query('error_description');
throw new Kohana_Exception($error_description);
}
$code = $this->request->query('code');
// Get the token by authorization code
$token = $this->provider->access_token($this->client, $code);
// Log received token
Kohana::$log->add(Log::DEBUG, 'Token received: :token', array(':token' => Debug::dump($token)));
// Token received, lets try to save it
$token_model = Model::factory('oauth_token')->create_token($token);
echo 'Token received';
} catch (Kohana_Exception $e) {
Kohana::$log->add(Kohana_Log::ERROR, $e->getMessage());
Session::instance()->set('error', __($e->getMessage()));
return $this->request->redirect(Route::get('default')
->uri(array('page' => 'vkontakte_error')));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment