When using PowerShell classes with inheritance, is there any way to specify the order of parameters?
In there the inheritance example below, there are two classes (HttpResponseOK and HttpResponseBadRequest), both of which inherit another class (HttpResponse).
In this case, creating an instance of either HttpResponseOK and HttpResponseBadRequest results in the properties of those objects being listed before the properties of HttpResponse. For example -
Code
$HttpResponseOK = [HttpResponse]::OK("Everything is OK")
$HttpResponseOK | ConvertTo-Json
$HttpResponseBadRequest = [HttpResponse]::BadRequest("Something bad happened")
$HttpResponseBadRequest | ConvertTo-Json
Response
{
"notification": "Everything is OK",
"statusCode": 200
}
{
"exception": "Something bad happened",
"statusCode": 400
}
As you can see, when the response is sent to a user the statusCode property appears after the notification/exception property. Given that notification/exception will in reality be objects, this means that the statsCode is not initially visible in the response.
Is there a way to specify that statusCode should go before notification/exception?
Inheritance Example
Class HttpResponse
{
[int]$statusCode = [HttpStatusCode]::OK
[void]SetStatus(
[int]$statusCode
)
{
$this.statusCode = $statusCode
}
static [HttpResponseOK]OK(
[object]$notification
)
{
$response = [HttpResponseOK]::new($notification)
return $response
}
static [HttpResponseBadRequest]BadRequest(
[object]$exception
)
{
$response = [HttpResponseBadRequest]::new($exception)
return $response
}
}
Class HttpResponseOK : HttpResponse
{
[object]$notification
HttpResponseOK(
[object]$notification
)
{
$this.SetStatus([HttpStatusCode]::OK)
$this.notification = $notification
}
}
Class HttpResponseBadRequest : HttpResponse
{
[object]$exception
HttpResponseBadRequest(
[object]$exception
)
{
$this.SetStatus([HttpStatusCode]::BadRequest)
$this.exception = $exception
}
}
Never done much with classes in Powershell, but if I move the declarations for both [object]$notification and [object]$exception to the base class HttpResponse, The order seems to keep:
Class HttpResponse
{
[int]$statusCode = [System.Net.HttpStatusCode]::OK
[object]$notification
[object]$exception
[void]SetStatus(
[int]$statusCode
)
{
$this.statusCode = $statusCode
}
static [HttpResponseOK]OK(
[object]$notification
)
{
$response = [HttpResponseOK]::new($notification)
return $response
}
static [HttpResponseBadRequest]BadRequest(
[object]$exception
)
{
$response = [HttpResponseBadRequest]::new($exception)
return $response
}
}
Class HttpResponseOK : HttpResponse
{
HttpResponseOK(
[object]$notification
)
{
$this.SetStatus([System.Net.HttpStatusCode]::OK)
$this.notification = $notification
}
}
Class HttpResponseBadRequest : HttpResponse
{
HttpResponseBadRequest(
[object]$exception
)
{
$this.SetStatus([System.Net.HttpStatusCode]::BadRequest)
$this.exception = $exception
}
}
$HttpResponseOK = [HttpResponse]::OK("Everything is OK")
$HttpResponseOK | ConvertTo-Json
$HttpResponseBadRequest = [HttpResponse]::BadRequest("Something bad happened")
$HttpResponseBadRequest | ConvertTo-Json
Output:
{
"statusCode": 200,
"notification": "Everything is OK",
"exception": null
}
{
"statusCode": 400,
"notification": null,
"exception": "Something bad happened"
}
Related
How to create multiple 404 responses (or more broadly, multiple same HTTP code responses) using java annotations.
I've tried:
#ApiResponse(
responseCode = "404",
description = "Not Found 1"
)
#ApiResponse(
responseCode = "404",
description = "Not Found 2"
)
And also mulitple #Content:
#ApiResponse(
responseCode = "404",
content = {
#Content(schema = #Schema(name = "404-1", description = "404-1")),
#Content(schema = #Schema(name = "404-2", description = "404-2"))
}
)
The only way I can get something similar to multiple is by using #ExampleObject[]:
#ApiResponse(
responseCode = "404",
content = #Content(
mediaType = "application/json",
examples = {
#ExampleObject(name = "404-1", description = "Not Found 1 desc"),
#ExampleObject(name = "404-2", description = "Not Found 2 desc")
}
)
)
This is not ideal because it requires human interaction to view all of them and is just not wanted; the expectation is to have:
- 200
- 404 Description 1
- 404 Description 2
- 404 Description 3
or even better:
- 200
- 404 Description 1
Description 2
Description 3
I'm using springdoc and the following dep:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.4.3</version>
</dependency>
By design, and not springdoc, but OpenAPI-Specification, all the responses are stored in a type ApiResponses which extends LinkedHashMap.
https://github.com/OAI/OpenAPI-Specification/blob/3.0.1/versions/3.0.1.md#responsesObject
Eeach HTTP code, for an operation can only have one ApiResponse object assigned.
Using examples is a good way to go.
If your multiple 404 responses have different structure, you can use oneof as follow:
#RestController
public class HelloController {
#GetMapping("/hello")
#ApiResponses({
#ApiResponse(responseCode = "200"),
#ApiResponse(description = "Not found", responseCode = "404",
content = #Content(mediaType = "application/json", schema = #Schema(oneOf = {
Foo.class, Bar.class }))) })
String hello() {
return null;
}
#Schema(description = "this is bar")
class Bar {
private String bar;
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
#Schema(description = "this is foo")
class Foo {
private String foo;
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
}
I solved my issue by just adding an HTML <br/> tag to the description where I wanted a new line:
#Operation(
responses = {
#ApiResponse(responseCode = "404", content = #Content,
description =
"This is potential 404 #1 <br/>" +
"This is potential 404 #2"
)
}
)
Alternatively,
You could create an annotation to make this more readable, for example something like #ApiResponse404 and add it to the operation via OperationCustomizer:
#Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
ApiResponse404 notFounds = handlerMethod.getMethodAnnotation(ApiResponse404.class);
if (notFounds != null)
operation.getResponses()
.addApiResponse("404", new ApiResponse()
.description(String.join("<br/>", notFounds.value()))
);
return operation;
}
Of course you would have to take into consideration the #Content, which you can easily add to the annotation, but I don't need it my scenario, I just need the description.
Then in a controller you can use the annotation:
#GetMapping("/helloworld")
#ApiResponse404({"This is potential 404 #1", "This is potential 404 #2"})
String getHelloWorld() {
return "Hello. World.";
}
I'm trying to call this class function:
public function scan(&$Iterator, $pattern = null, $count = null)
{
return $this->__call('scan', array(&$Iterator, $pattern, $count));
}
From my class:
$itScan = NULL;
while($arr_keys = $this->redisClient->scan($itScan, '', 10000)) {
foreach($arr_keys as $str_key) {
echo "Here is a key: $str_key\n";
}
}
I understand this is something related to the & pointer but I can't figure who to call it from inside my class.
Thank you!
This is the proper way to iterate over your results :
$i = null;
$allResults = [];
do {
$someResults = $redis->scan($i, "*", 10000);
if (!empty($someResults)) {
$allKeys = array_merge($allKeys, $someResults);
}
}
while ($i);
i followed a tutorial for Register and Login Request in xcode.
But i have a problem with the Code because i changed some values of it.
My variables in the php script are ssiss (string, string, int, string, string). In the swift file i am using Alamofire. Here it is:
#IBAction func buttonRegister(_ sender: UIButton) {
let ageINT = Int(ageTF.text!);
//creating parameters for the post request
let parameters: Parameters=[
"name":nameTF.text!,
"username":usernameTF.text!,
"age": ageINT ?? 0,
"country":countryTF.text!,
"password":passwordTF.text!,
]
//Sending http post request
Alamofire.request(URL_USER_REGISTER, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON
{
response in
//printing response
print(response)
//getting the json value from the server
if let result = response.result.value {
//converting it as NSDictionary
let jsonData = result as! NSDictionary
//displaying the message in label
self.labelMessage.text = jsonData.value(forKey: "message") as! String?
}
}
I think the problem is i pass an String value instead of a integer value. That is why i already have changed the value for "age" from "age":ageTF.text!, to "age": ageINT ?? 0,
The error i am get is "Required parameters are missing".
I am getting this "error-response" message because of this php-script:
<?php
//importing required script
require_once '../includes/DbOperation.php';
$response = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!verifyRequiredParams(array('name', 'username', 'age', 'country', 'password'))) {
//getting values
$name = $_POST["name"];
$username = $_POST["username"];
$age = $_POST["age"];
$country = $_POST["country"];
$password = $_POST["password"];
//creating db operation object
$db = new DbOperation();
//adding user to database
$result = $db->createUser($name, $username, $age, $country, $password);
//making the response accordingly
if ($result == USER_CREATED) {
$response['error'] = false;
$response['message'] = 'User created successfully';
} elseif ($result == USER_ALREADY_EXIST) {
$response['error'] = true;
$response['message'] = 'User already exist';
} elseif ($result == USER_NOT_CREATED) {
$response['error'] = true;
$response['message'] = 'Some error occurred';
}
} else {
$response['error'] = true;
$response['message'] = 'Required parameters are missing';
}
} else {
$response['error'] = true;
$response['message'] = 'Invalid request';
}
//function to validate the required parameter in request
function verifyRequiredParams($required_fields)
{
//Getting the request parameters
$request_params = $_REQUEST;
//Looping through all the parameters
foreach ($required_fields as $field) {
//if any requred parameter is missing
if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
//returning true;
return true;
}
}
return false;
}
print_r(json_encode($response));
?>
And this is the other php script:
<?php
class DbOperation
{
private $conn;
//Constructor
function __construct()
{
require_once dirname(__FILE__) . '/Constants.php';
require_once dirname(__FILE__) . '/DbConnect.php';
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}
//Function to create a new user
public function createUser($name, $username, $age, $country, $password)
{
if (!$this->isUserExist($username)) {
$password = md5($pass);
$stmt = $this->conn->prepare("INSERT INTO user (name, username, age, country, password) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("ssiss", $name, $username, $age, $country, $password);
if ($stmt->execute()) {
return USER_CREATED;
} else {
return USER_NOT_CREATED;
}
} else {
return USER_ALREADY_EXIST;
}
}
private function isUserExist($username)
{
$stmt = $this->conn->prepare("SELECT id FROM user WHERE username = ? ");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
}
I am having some "Trying to get property of non-object" issue with facebook sign up. The server returns errors related to access_token.
i have error form line 490 and 451. on line 451 i succeed to solve it but i ve still 2 errors from line 490.
the line was fixed by changing if ( $token->access_token ) by if ( $token['access_token'] )
I think the problem is due to codeigniter.
the line 490 is the following one:
private function _token_string()
{
return 'access_token='.$this->_get('token')->access_token;
}
Here is the entire code:
class Facebook_Lib extends CI_Config
{
private $_api_url;
private $_api_key;
private $_api_secret;
private $_errors = array();
private $_enable_debug = FALSE;
function __construct()
{
$this->_obj =$CI =& get_instance();
$this->_obj->load->library('session');
$this->_obj->load->helper('url');
$this->_obj->load->helper('facebook');
$fb_api_id = $CI->db->get_where('settings', array('code' => 'SITE_FB_API_ID'))->row()->string_value;
$fb_api_secret = $CI->db->get_where('settings', array('code' => 'SITE_FB_API_SECRET'))->row()->string_value;
$this->_api_url = $this->_obj->config->item('facebook_api_url');
$this->_api_key = $fb_api_id;
$this->_api_secret = $fb_api_secret;
$this->session = new facebookSession();
$this->connection = new facebookConnection();
}
public function logged_in()
{
return $this->session->logged_in();
}
public function login($scope = NULL)
{
return $this->session->login($scope);
}
public function login_url($scope = NULL)
{
return $this->session->login_url($scope);
}
public function logout()
{
return $this->session->logout();
}
public function user()
{
return $this->session->get();
}
public function call($method, $uri, $data = array())
{
$response = FALSE;
try
{
switch ( $method )
{
case 'get':
$response = $this->connection->get($this->append_token($this->_api_url.$uri));
break;
case 'post':
$response = $this->connection->post($this->append_token($this->_api_url.$uri), $data);
break;
}
}
catch (facebookException $e)
{
$this->_errors[] = $e;
if ( $this->_enable_debug )
{
echo $e;
}
}
return $response;
}
public function errors()
{
return $this->_errors;
}
public function last_error()
{
if ( count($this->_errors) == 0 ) return NULL;
return $this->_errors[count($this->_errors) - 1];
}
public function append_token($url)
{
return $this->session->append_token($url);
}
public function set_callback($url)
{
return $this->session->set_callback($url);
}
public function enable_debug($debug = TRUE)
{
$this->_enable_debug = (bool) $debug;
}
}
class facebookConnection {
// Allow multi-threading.
private $_mch = NULL;
private $_properties = array();
function __construct()
{
$this->_mch = curl_multi_init();
$this->_properties = array(
'code' => CURLINFO_HTTP_CODE,
'time' => CURLINFO_TOTAL_TIME,
'length' => CURLINFO_CONTENT_LENGTH_DOWNLOAD,
'type' => CURLINFO_CONTENT_TYPE
);
}
private function _initConnection($url)
{
$this->_ch = curl_init($url);
curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, TRUE);
}
public function get($url, $params = array())
{
if ( count($params) > 0 )
{
$url .= '?';
foreach( $params as $k => $v )
{
$url .= "{$k}={$v}&";
}
$url = substr($url, 0, -1);
}
$this->_initConnection($url);
$response = $this->_addCurl($url, $params);
return $response;
}
public function post($url, $params = array())
{
// Todo
$post = '';
foreach ( $params as $k => $v )
{
$post .= "{$k}={$v}&";
}
$post = substr($post, 0, -1);
$this->_initConnection($url, $params);
curl_setopt($this->_ch, CURLOPT_POST, 1);
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $post);
$response = $this->_addCurl($url, $params);
return $response;
}
private function _addCurl($url, $params = array())
{
$ch = $this->_ch;
$key = (string) $ch;
$this->_requests[$key] = $ch;
$response = curl_multi_add_handle($this->_mch, $ch);
if ( $response === CURLM_OK || $response === CURLM_CALL_MULTI_PERFORM )
{
do {
$mch = curl_multi_exec($this->_mch, $active);
} while ( $mch === CURLM_CALL_MULTI_PERFORM );
return $this->_getResponse($key);
}
else
{
return $response;
}
}
private function _getResponse($key = NULL)
{
if ( $key == NULL ) return FALSE;
if ( isset($this->_responses[$key]) )
{
return $this->_responses[$key];
}
$running = NULL;
do
{
$response = curl_multi_exec($this->_mch, $running_curl);
if ( $running !== NULL && $running_curl != $running )
{
$this->_setResponse($key);
if ( isset($this->_responses[$key]) )
{
$response = new facebookResponse( (object) $this->_responses[$key] );
if ( $response->__resp->code !== 200 )
{
$error = $response->__resp->code.' | Request Failed';
if ( isset($response->__resp->data->error->type) )
{
$error .= ' - '.$response->__resp->data->error->type.' - '.$response->__resp->data->error->message;
}
throw new facebookException($error);
}
return $response;
}
}
$running = $running_curl;
} while ( $running_curl > 0);
}
private function _setResponse($key)
{
while( $done = curl_multi_info_read($this->_mch) )
{
$key = (string) $done['handle'];
$this->_responses[$key]['data'] = curl_multi_getcontent($done['handle']);
foreach ( $this->_properties as $curl_key => $value )
{
$this->_responses[$key][$curl_key] = curl_getinfo($done['handle'], $value);
curl_multi_remove_handle($this->_mch, $done['handle']);
}
}
}
}
class facebookResponse {
private $__construct;
public function __construct($resp)
{
$this->__resp = $resp;
$data = json_decode($this->__resp->data);
if ( $data !== NULL )
{
$this->__resp->data = $data;
}
}
public function __get($name)
{
if ($this->__resp->code < 200 || $this->__resp->code > 299) return FALSE;
$result = array();
if ( is_string($this->__resp->data ) )
{
parse_str($this->__resp->data, $result);
$this->__resp->data = (object) $result;
}
if ( $name === '_result')
{
return $this->__resp->data;
}
return $this->__resp->data->$name;
}
}
class facebookException extends Exception {
function __construct($string)
{
parent::__construct($string);
}
public function __toString() {
return "exception '".__CLASS__ ."' with message '".$this->getMessage()."' in ".$this->getFile().":".$this->getLine()."\nStack trace:\n".$this->getTraceAsString();
}
}
class facebookSession {
private $_api_key;
private $_api_secret;
private $_token_url = 'oauth/access_token';
private $_user_url = 'me';
private $_data = array();
function __construct()
{
$this->_obj =$CI =& get_instance();
$fb_api_id = $CI->db->get_where('settings', array('code' => 'SITE_FB_API_ID'))->row()->string_value;
$fb_api_secret = $CI->db->get_where('settings', array('code' => 'SITE_FB_API_SECRET'))->row()->string_value;
$this->_api_key = $fb_api_id;
$this->_api_secret = $fb_api_secret;
$this->_token_url = $this->_obj->config->item('facebook_api_url').$this->_token_url;
$this->_user_url = $this->_obj->config->item('facebook_api_url').$this->_user_url;
$this->_set('scope', $this->_obj->config->item('facebook_default_scope'));
$this->connection = new facebookConnection();
if ( !$this->logged_in() )
{
// Initializes the callback to this page URL.
$this->set_callback();
}
}
public function logged_in()
{
return ( $this->get() === NULL ) ? FALSE : TRUE;
}
public function logout()
{
$this->_unset('token');
$this->_unset('user');
}
public function login_url($scope = NULL)
{
$url = "http://www.facebook.com/dialog/oauth?client_id=".$this->_api_key.'&redirect_uri='.urlencode($this->_get('callback'));
if ( empty($scope) )
{
$scope = $this->_get('scope');
}
else
{
$this->_set('scope', $scope);
}
if ( !empty($scope) )
{
$url .= '&scope='.$scope;
}
return $url;
}
public function login($scope = NULL)
{
$this->logout();
if ( !$this->_get('callback') ) $this->_set('callback', current_url());
$url = $this->login_url($scope);
return redirect($url);
}
public function get()
{
$token = $this->_find_token();
if ( empty($token) ) return NULL;
// $user = $this->_get('user');
// if ( !empty($user) ) return $user;
try
{
$user = $this->connection->get($this->_user_url.'?'.$this->_token_string());
}
catch ( facebookException $e )
{
$this->logout();
return NULL;
}
// $this->_set('user', $user);
return $user;
}
private function _find_token()
{
$token = $this->_get('token');
if ( !empty($token) )
{
if ( !empty($token->expires) && intval($token->expires) >= time() )
{
// Problem, token is expired!
return $this->logout();
}
$this->_set('token', $token);
return $this->_token_string();
}
if ( !isset($_GET['code']) )
{
return $this->logout();
}
if ( !$this->_get('callback') ) $this->_set('callback', current_url());
$token_url = $this->_token_url.'?client_id='.$this->_api_key."&client_secret=".$this->_api_secret."&code=".$_GET['code'].'&redirect_uri='.urlencode($this->_get('callback'));
try
{
$token = $this->connection->get($token_url);
}
catch ( facebookException $e )
{
$this->logout();
redirect($this->_strip_query());
return NULL;
}
$this->_unset('callback');
if ( $token['access_token'] )
{
if ( !empty($token->expires) )
{
$token->expires = strval(time() + intval($token->expires));
}
$this->_set('token', $token);
redirect($this->_strip_query());
}
return $this->_token_string();
}
private function _get($key)
{
$key = '_facebook_'.$key;
return $this->_obj->session->userdata($key);
}
private function _set($key, $data)
{
$key = '_facebook_'.$key;
$this->_obj->session->set_userdata($key, $data);
}
private function _unset($key)
{
$key = '_facebook_'.$key;
$this->_obj->session->unset_userdata($key);
}
public function set_callback($url = NULL)
{
$this->_set('callback', $this->_strip_query($url));
}
private function _token_string()
{
return 'access_token='.$this->_get('token')->access_token;
}
public function append_token($url)
{
if ( $this->_get('token') ) $url .= '?'.$this->_token_string();
return $url;
}
private function _strip_query($url = NULL)
{
if ( $url === NULL )
{
$url = ( empty($_SERVER['HTTPS']) ) ? 'http' : 'https';
$url .= '://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
$parts = explode('?', $url);
return $parts[0];
}
}
I am not sure what is going on with this library. On line 451 you are looking in the $token array for the values that got returned and then it goes through a very convoluted way of setting that in the CI session and then instantly call it back from the CI session.
Line 458 sets the token into the session and then on 462 they return it but by calling a method that calls a method that returns session->userdata('token').
That seems to be why its failing on 490 because it gets the value in the session data but then tries finding the data for ->access_token on top of the returned value and that does not exist.
dump out $token before line 462 and see if it is an object or an array. If it's ann array then that would explain why ->access_token on 490 does not work. If it is an object then I would change line 462 to
return 'access_token='.$token->access_token;
or whatever the dump of $token says the value of the object is (I am assuming it is access_token) and see if that works. if it does then you have to figure out why they wrote all these extra steps and what it will be used for. If not, call it good.
Facebook has a library written that is available on github that you can just drop in the library dir in codeigniter and it will work fine. I would suggest dropping this class you are using and just use the one Facebook provides. This way you can learn how to integrate with Facebook right from their developer site and if they ever change their API calls, you do not have to rewrite code; you just replace the two files in the library dir.
https://github.com/facebook/facebook-php-sdk/tree/master/src
Hope this helps.
private $_api_key;
private $_api_secret;
private $_token_url = 'oauth/access_token';
private $_user_url = 'me';
private $_data = array();
function __construct()
{
$this->_obj =$CI =& get_instance();
$fb_api_id = $CI->db->get_where('settings', array('code' => 'SITE_FB_API_ID'))->row()->string_value;
$fb_api_secret = $CI->db->get_where('settings', array('code' => 'SITE_FB_API_SECRET'))->row()->string_value;
$this->_api_key = $fb_api_id;
$this->_api_secret = $fb_api_secret;
$this->_token_url = $this->_obj->config->item('facebook_api_url').$this->_token_url;
$this->_user_url = $this->_obj->config->item('facebook_api_url').$this->_user_url;
$this->_set('scope', $this->_obj->config->item('facebook_default_scope'));
$this->connection = new facebookConnection();
if ( !$this->logged_in() )
{
// Initializes the callback to this page URL.
$this->set_callback();
}
}
public function logged_in()
{
return ( $this->get() === NULL ) ? FALSE : TRUE;
}
public function logout()
{
$this->_unset('token');
$this->_unset('user');
}
public function login_url($scope = NULL)
{
$url = "http://www.facebook.com/dialog/oauth?client_id=".$this->_api_key.'&redirect_uri='.urlencode($this->_get('callback'));
if ( empty($scope) )
{
$scope = $this->_get('scope');
}
else
{
$this->_set('scope', $scope);
}
if ( !empty($scope) )
{
$url .= '&scope='.$scope;
}
return $url;
}
public function login($scope = NULL)
{
$this->logout();
if ( !$this->_get('callback') ) $this->_set('callback', current_url());
$url = $this->login_url($scope);
return redirect($url);
}
public function get()
{
$token = $this->_find_token();
if ( empty($token) ) return NULL;
// $user = $this->_get('user');
// if ( !empty($user) ) return $user;
try
{
$user = $this->connection->get($this->_user_url.'?'.$this->_token_string());
}
catch ( facebookException $e )
{
$this->logout();
return NULL;
}
// $this->_set('user', $user);
return $user;
}
private function _find_token()
{
$token = $this->_get('token');
if ( !empty($token) )
{
if ( !empty($token->expires) && intval($token->expires) >= time() )
{
// Problem, token is expired!
return $this->logout();
}
$this->_set('token', $token);
return $this->_token_string();
}
if ( !isset($_GET['code']) )
{
return $this->logout();
}
if ( !$this->_get('callback') ) $this->_set('callback', current_url());
$token_url = $this->_token_url.'?client_id='.$this->_api_key."&client_secret=".$this->_api_secret."&code=".$_GET['code'].'&redirect_uri='.urlencode($this->_get('callback'));
try
{
$token = $this->connection->get($token_url);
}
catch ( facebookException $e )
{
$this->logout();
redirect($this->_strip_query());
return NULL;
}
$this->_unset('callback');
if ( $token->access_token )
{
if ( !empty($token->expires) )
{
$token->expires = strval(time() + intval($token->expires));
}
$this->_set('token', $token);
redirect($this->_strip_query());
}
return $this->_token_string();
}
private function _get($key)
{
$key = '_facebook_'.$key;
return $this->_obj->session->userdata($key);
}
private function _set($key, $data)
{
$key = '_facebook_'.$key;
$this->_obj->session->set_userdata($key, $data);
}
private function _unset($key)
{
$key = '_facebook_'.$key;
$this->_obj->session->unset_userdata($key);
}
public function set_callback($url = NULL)
{
$this->_set('callback', $this->_strip_query($url));
}
private function _token_string()
{
return 'access_token='.$this->_get('token')->access_token;
}
public function append_token($url)
{
if ( $this->_get('token') ) $url .= '?'.$this->_token_string();
return $url;
}
private function _strip_query($url = NULL)
{
if ( $url === NULL )
{
$url = ( empty($_SERVER['HTTPS']) ) ? 'http' : 'https';
$url .= '://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
$parts = explode('?', $url);
return $parts[0];
}
please tell me what and all retrieved from facebook as token when user tries to sign in using fb account in a site.
I get the followin errors in this code :
Message: Trying to get property of non-object
Filename: libraries/Facebook_Lib.php
Line Number: 454
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/Facebook_Lib.php
Line Number: 493
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/Facebook_Lib.ph
Line Number: 493
all errors regarding access_token
Its was an database error due to session have not created due to facebook app not live.