I'm trying to add the OAuth class in codeigniter but when i call it's show error
Non-existent class: OAuth
This is my code
class Food extends My_Controller
{
public function __construct(){
$this->load->library('OAuth');
}
public function get_yelp_api(){
$unsigned_url = "http://api.yelp.com/v2/business/the-waterboy-sacramento";
$consumer_key = '***********';
$consumer_secret = '**********';
$token = '***************';
$token_secret = '**************';
$token = new OAuthToken($token, $token_secret);
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$oauthrequest = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $unsigned_url);
$oauthrequest->sign_request($signature_method, $consumer, $token);
$signed_url = $oauthrequest->to_url();
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$response = json_decode($data);
$json_string = file_get_contents($signed_url);
$result = json_decode($json_string);
echo '<pre>';
print_r($result);
echo '</pre>';
}
}
I also try to add it on this way
require_once(APPPATH.'libraries/OAuth.php');
but still got an error
Non-existent class: OAuth
you doing mistake loading a class as library load it like this
Non-existent class:OAuth
solution
just only need to load class at the top of controller like this:
require_once(APPPATH.'libraries/OAuth.php');
Related
How to send a correct authorization header for basic authentication using guzzle
use GuzzleHttp\Client;
$username='EFDEMO';
$password='EFDEMO';
$client = new Client(['auth' => [$username, $password]]);
$res = $client->request('GET', 'https://mb-
rewards.calusastorefront.p2motivate.com/client/json.php/
getMemberAccount');
$res->getStatusCode();
$response = $res->getBody();
echo $response;
the error I am getting
{"statusCode":"Error","error":
{"errorCode":"400","errorMessage":"Authentication Header ID field must
match Basic Authentication Username"}}
Referring to documentation you should pass the auth parameter in request method instead of client's constructor:
$client = new Client();
$res = $client->request(
'GET',
'past-url-here',
['auth' => [$username, $password]]
);
I solved it like below
<?php
require '../x_static_libs/guzzle/vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$username = 'xxxxxx';
$password = 'xxxxxxxx';
$token = 'xxxxxxxxxxxxxxxxxxxx';
$url = 'https://ervb-rewards.com/client/json.php';
$api_token =
base64_encode(json_encode(['id'=>'xxxxxxx','token'=>'xxxxxxxxxxxxxxxxxxxxxx']));
$res = $client->request(
'GET',
$url.'/getMemberAccount',
['auth' => [$username,$api_token]]
);
$jsonData = json_decode($res->getBody(), true);
echo "<pre>";
print_r($jsonData);
echo "</pre>";
I'm trying to create a laravel command to call Mercado Libre API. When I run this sample script in my htdocs from XAMPP folder, it returns the body data, but when I run from my laravel command it returns the body empty.
This is the script:
$app_id = 'xxx';
$secret_key = 'xxxx';
$meli = new Meli($app_id, $secret_key);
$access_token = 'xxxx';
$params = array('access_token' => $access_token);
$result = $meli->get('/users/me', $params, true);
This is the command
public function handle()
{
$app_id = 'xxx';
$secret_key = 'xxx';
$meli = new Meli($app_id, $secret_key);
$access_token = 'xxx';
$params = array('access_token' => $access_token);
$result = $meli->get('/users/me', $params, true);
}
EDIT
The scirpts are running in the same environment. I test and curl is callable.
if(is_callable('curl_init')){
//Code here
print_r("normal");
}
I am trying to auto post on Facebook page using my app it works fine if I replace my-page-numeric-id with my profile numeric id, but when I put my page numeric id instead of my-page-numeric-id and put valid access token it won't publish on Facebook page I tried to submit review permission that are as follow "manage_pages, publish_actions" but Facebook team mentioned that you don't need it all because you own this page and your currently admin of it, these permission are necessary when any other person need to authorize with your app.
Now my question is, my code works perfectly for publishing post on my profile's wall but i am unable to auto post on my page what is the problem behind it i am unable to figure out here is my code
<?php
require_once 'src/facebook.php';
class FacebookApi {
var $consumer;
var $token;
var $method;
var $http_status;
var $last_api_call;
var $callback;
var $connection;
var $access_token;
function __construct($data){
$config = array();
$config['appId'] = $data['consumer_key'];
$config['secret'] = $data['consumer_secret'];
$this->connection = new Facebook($config);
}
function share($title, $targetUrl, $imgUrl, $description, $access_token){
$this->connection->setAccessToken($access_token);
$params["access_token"] = $access_token;
if(!empty($title)){
$params["message"] = $title;
$params["name"] = $title;
}
if(!empty($targetUrl)){
$params["link"] = $targetUrl;
}
if(!empty($imgUrl)){
$params["picture"] = $imgUrl;
}
if(!empty($description)){
$params["description"] = $description;
}
// post to Facebook
try {
$ret = $this->connection->api('/my-page-numeric-id/feed', 'POST', $params);
} catch(Exception $e) {
$e->getMessage();
}
return true;
}
function getLoginUrl($params){
return $this->connection->getLoginUrl($params);
}
function getContent($url) {
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ci, CURLOPT_HEADER, false);
curl_setopt( $ci, CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ci);
curl_close ($ci);
return $response;
}
}
$access_token = 'long-live-token-here';
$facebookData = array();
$facebookData['consumer_key'] = 'app-id-here';
$facebookData['consumer_secret'] = 'app-secret-here';
$title = "Demo Content Posted on Timeline";
$targetUrl = "http://www.demo_url.com/1234-post";
$imgUrl = "http://www.demo_url.com/1234-post-image.png";
$description = "demo_description_here";
$facebook = new FacebookApi($facebookData);
$facebook->share($title, $targetUrl, $imgUrl, $description, $access_token);
?>
With API v2.3, they have split the publishing permission into two separate ones:
publish_actions is for anything you want to publish as a user
if you want to publish as a page however, you need publish_pages permission (in addition to manage_pages)
now it's time for facebook v5 sdk
$fb = new Facebook\Facebook([
'app_id' => 'app_id',
'app_secret' => 'app_secret',
'default_graph_version' => 'v2.8',
]);
// facebook auto post
$params = array(
"message" => "$title in $merchant $short",
"link" => "http://pickmyoffers.com/",
"picture" => "http://Pickmyoffers.com/images/searched/Flipkart.png",
"name" => "www.Pickmyoffers.com",
"caption" => "www.pickmyoffers.com",
"description" => "Submit Coupon and earn money through Pickmyoffers.com | Deals,Coupons and offers."
);
$post = $fb->post('/Page_id/feed',$params, $access_token);
$post = $post->getGraphNode()->asArray();
}
hope it's help
I am trying to use disqus login here: http://beta.perfectquiver.com/include/disqus/index.php
Also i set redirect URL : http://beta.perfectquiver.com/include/disqus/index.php in disqus configuration.
But it showing me error
Invalid parameter: redirect_uri (values for POST and GET arguments differ)" ["error"]=> string(13) "invalid_grant" }
After google it, someone said it is issue with mismatch of redirect URL, it should be identical.
But here its already identical.
Still same error.
Here id the code which i am using.
$PUBLIC_KEY = "PUBLIC_KEY";
$SECRET_KEY = "SECRET_KEY";
$redirect = urlencode("http://beta.perfectquiver.com/include/disqus/index.php");
$endpoint = 'https://disqus.com/api/oauth/2.0/authorize?';
$client_id = $PUBLIC_KEY;
$scope = 'read,write';
$response_type = 'code';
$auth_url = $endpoint.'&client_id='.$client_id.'&scope='.$scope.'&response_type='.$response_type.'&redirect_uri='.$redirect;
echo "<h3>Trigger authentication -> <a href='".$auth_url."'>OAuth</a></h3>";
$CODE = $_GET['code'];
if($CODE){
extract($_POST);
$authorize = "authorization_code";
$url = 'https://disqus.com/api/oauth/2.0/access_token/';
$fields = array(
'grant_type'=>urlencode($authorize),
'client_id'=>urlencode($PUBLIC_KEY),
'client_secret'=>urlencode($SECRET_KEY),
'redirect_uri'=>urlencode($redirect),
'code'=>urlencode($CODE)
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, "&");
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
curl_close($ch);
$auth_results = json_decode($data);
echo "<p><h3>The authentication information returned:</h3>";
var_dump($auth_results);
echo "</p>";
$access_token = $auth_results->access_token;
echo "<p><h3>The access token you'll use in API calls:</h3>";
echo $access_token;
echo "</p>";
echo $auth_results->access_token;
function getData($url, $SECRET_KEY, $access_token){
//Setting OAuth parameters
$oauth_params = (object) array(
'access_token' => $access_token,
'api_secret' => $SECRET_KEY
);
$param_string = '';
//Build the endpiont from the fields selected and put add it to the string.
//foreach($params as $key=>$value) { $param_string .= $key.'='.$value.'&'; }
foreach($oauth_params as $key=>$value) { $param_string .= $key.'='.$value.'&'; }
$param_string = rtrim($param_string, "&");
// setup curl to make a call to the endpoint
$url .= $param_string;
//echo $url;
$session = curl_init($url);
// indicates that we want the response back rather than just returning a "TRUE" string
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session,CURLOPT_FOLLOWLOCATION,true);
// execute GET and get the session backs
$results = curl_exec($session);
// close connection
curl_close($session);
// show the response in the browser
return json_decode($results);
}
//Setting the correct endpoint
$cases_endpoint = 'https://disqus.com/api/3.0/users/details.json?';
//Calling the function to getData
$user_details = getData($cases_endpoint, $SECRET_KEY, $access_token);
echo "<p><h3>Getting user details:</h3>";
var_dump($user_details);
echo "</p>";
//Setting the correct endpoint
$forums_endpoint = 'https://disqus.com/api/3.0/users/listForums.json?';
//Calling the function to getData
$forum_details = getData($forums_endpoint, $SECRET_KEY, $access_token);
echo "<p><h3>Getting forum details:</h3>";
var_dump($forum_details);
echo "</p>";
}
I've been able to successfully log a user in and return their details. The next step is to get them to post a comment via my app.
I tried modifying code from the reddit-php-sdk -- https://github.com/jcleblanc/reddit-php-sdk/blob/master/reddit.php -- but I can't get it to work.
My code is as follows:
function addComment($name, $text, $token){
$response = null;
if ($name && $text){
$urlComment = "https://ssl.reddit.com/api/comment";
$postData = sprintf("thing_id=%s&text=%s",
$name,
$text);
$response = runCurl($urlComment, $token, $postData);
}
return $response;
}
function runCurl($url, $token, $postVals = null, $headers = null, $auth = false){
$ch = curl_init($url);
$auth_mode = 'oauth';
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 10
);
$headers = array("Authorization: Bearer {$token}");
$options[CURLOPT_HEADER] = false;
$options[CURLINFO_HEADER_OUT] = false;
$options[CURLOPT_HTTPHEADER] = $headers;
if (!empty($_SERVER['HTTP_USER_AGENT'])){
$options[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT'];
}
if ($postVals != null){
$options[CURLOPT_POSTFIELDS] = $postVals;
$options[CURLOPT_CUSTOMREQUEST] = "POST";
}
curl_setopt_array($ch, $options);
$apiResponse = curl_exec($ch);
$response = json_decode($apiResponse);
//check if non-valid JSON is returned
if ($error = json_last_error()){
$response = $apiResponse;
}
curl_close($ch);
return $response;
}
$thing_id = 't2_'; // Not the actual thing id
$perma_id = '2daoej'; // Not the actual perma id
$name = $thing_id . $perma_id;
$text = "test text";
$reddit_access_token = $_SESSION['reddit_access_token'] // This is set after login
addComment($name, $text, $reddit_access_token);
The addComment function puts the comment together according to their API -- http://www.reddit.com/dev/api
addComment then calls runCurl to make the request. My guess is that the curl request is messed up because I'm not receiving any response whatsoever. I'm not getting any errors so I'm not sure what's going wrong. Any help would really be appreciated. Thanks!
If you are using your own oAuth solution, I would suggest using the SDK as I said in my comment, but extend it to overwrite the construct method.
class MyReddit extends reddit {
public function __construct()
{
//set API endpoint
$this->apiHost = ENDPOINT_OAUTH;
}
public function setAuthVars($accessToken, $tokenType)
{
$this->access_token = $accessToken;
$this->token_type = $tokenType;
//set auth mode for requests
$this->auth_mode = 'oauth';
}
}
You just need to make sure that you call setAuthVars before running any api calls.