I'm doing two calls to an API. The only thing that differ the two calls is the URL. What i basically wonder is how i can structure this a little (or alot) better? Maybe run them after each other in a que? Sometimes the second calls returns a "critical error" on the Wordpress page where it gets called.
Any suggestions?
private $oauth_Key = 'xxx';
private $oauth_consumer = 'xxx';
private $api_url = 'xxx';
private $cat_url = 'xxx';
try {
$oauth = new OAuth($this->oauth_Key, $this->oauth_consumer, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->fetch($this->api_url);
$response_info = $oauth->getLastResponseInfo();
header("Content-Type: {$response_info["content_type"]}");
$res = $oauth->getLastResponse();
$oauth2 = new OAuth($this->oauth_Key, $this->oauth_consumer, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$oaut2->fetch($this->cat_url);
$response_info2 = $oauth2->getLastResponseInfo();
header("Content-Type: {$response_info2["content_type"]}");
$res2 = $oauth2->getLastResponse();
} catch(OAuthException $e) {
echo "Exception caught!\n";
echo "Response: ". $e->lastResponse . "\n";
}
Just figured it out. Re structured the code a bit. See below:
private $oauth_Key = 'xxx';
private $oauth_consumer = 'xxx';
private $api_url = 'xxx';
private $cat_url = 'xxx';
try {
$oauth = new OAuth($this->oauth_Key, $this->oauth_consumer, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->fetch($this->api_url);
$response_info = $oauth->getLastResponseInfo();
header("Content-Type: {$response_info["content_type"]}");
$res = $oauth->getLastResponse();
$oauth2 = new OAuth($this->oauth_Key, $this->oauth_consumer, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth2->fetch($this->cat_url);
$response_info2 = $oauth2->getLastResponseInfo();
header("Content-Type: {$response_info2["content_type"]}");
$res2 = $oauth2->getLastResponse();
} catch(OAuthException $e) {
echo "Exception caught!\n";
echo "Response: ". $e->lastResponse . "\n";
}
Related
I have followed etsy guide to authenticate my app and connect to a user, and I was able to get through the first process to get a oauth_token, token secret and verifier. But after setting token for the oauth it fails the getAccessToken function with this message
Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)bad
this is my code, as you can tell i tried many options, my ultimate goal is to have all credentials stored in file then in a database but first i want to learn whats wrong with my app
<?php
header('Content-type: text/plain');
ini_set('max_execution_time', 600);
$ksecrFile = fopen("key_secret.txt", "r") or die("Unable to open file!");
$key = trim(fgets($ksecrFile),"\n");
$secret = trim(fgets($ksecrFile),"\n");
$verifier = "";
fclose($ksecrFile);
$lines = file("key_secret.txt");
$oauth = new OAuth($key, $secret);
//$oauth->setAuthType(OAUTH_AUTH_TYPE_URI);
$oauth->disableSSLChecks();
function getToken($oauth, $verifier){
$req_token = $oauth->getRequestToken("https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20listings_r", "http://localhost/ksec.php");
if (!empty($_GET))
{
print_r($req_token);
$verifier = $_GET["oauth_verifier"];
$token = $req_token['oauth_token'];
$token_secret = $req_token['oauth_token_secret'];
//$tokenFile = fopen("token.txt", "w");
//fwrite($tokenFile, $verifier . "\r\n");
//fwrite($tokenFile, $token . "\r\n");
//fwrite($tokenFile, $token_secret);
//fclose($tokenFile);
//header("Location: http://localhost/ksec.php");
echo $verifier . " " . $token . " " . $token_secret . "\n";
$ksecrFile = fopen("key_secret.txt", "r") or die("Unable to open file!");
$key = trim(fgets($ksecrFile),"\n");
$secret = trim(fgets($ksecrFile),"\n");
$oauth1 = new OAuth($key, $secret);
$oauth1->disableSSLChecks();
$oauth1->setToken($req_token['oauth_token'], $req_token['oauth_token_secret']);
try {
// set the verifier and request Etsy's token credentials url
$acc_token = $oauth1->getAccessToken("https://openapi.etsy.com/v2/oauth/access_token", null, $_GET["oauth_verifier"]);
echo "good";
} catch (OAuthException $e) {
print_r($e->getMessage());
echo "bad";
}
}
else
{
$login_url = sprintf(
"%s?oauth_consumer_key=%s&oauth_token=%s",
$req_token['login_url'],
$req_token['oauth_consumer_key'],
$req_token['oauth_token']
);
header("Location: " . $login_url);
}
}
$tokenFile = fopen("token.txt", "r") or die(getToken($oauth, $verifier));
//$verifier = trim(fgets($tokenFile),"\n");
//$token = trim(fgets($tokenFile),"\n");
//$tokenSecret = trim(fgets($tokenFile),"\n");
//fclose($tokenFile);
//echo $verifier . " " . $token . " " . $tokenSecret . "\n";
//echo $verifier . " " . $token;
?>
Ok i figured it out, i just dont know how to explain but having the code
<?php
header('Content-type: text/plain');
ini_set('max_execution_time', 600);
$ksecrFile = fopen("key_secret.txt", "r") or die("Unable to open file!");
$key = trim(fgets($ksecrFile),"\n");
$secret = trim(fgets($ksecrFile),"\n");
fclose($ksecrFile);
$oauth = new OAuth($key, $secret);
$oauth->disableSSLChecks();
$tokenFile = fopen("token.txt", "r") or die(getToken($oauth, $key, $secret));
function getToken($oauth, $key, $secret){
$req_token = $oauth->getRequestToken("https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20listings_r", "http://localhost/ksec.php");
$tokenFile = fopen("token.txt", "w") or die("Unable to open file!");
fwrite($tokenFile, $req_token['oauth_token'] . "\n");
fwrite($tokenFile, $req_token['oauth_token_secret'] . "\n");
$login_url = sprintf(
"%s?oauth_consumer_key=%s&oauth_token=%s",
$req_token['login_url'],
$req_token['oauth_consumer_key'],
$req_token['oauth_token']
);
header("Location: " . $login_url);
}
if (empty($_GET))
{
getToken($oauth, $key, $secret);
}
else
{
$tokenFile = fopen("token.txt", "r") or die("Unable to open file!");
$token = trim(fgets($tokenFile),"\n");
$tokenSecret = trim(fgets($tokenFile),"\n");
fclose($tokenFile);
$oauth1 = new OAuth($key, $secret);
$oauth1->disableSSLChecks();
$oauth1->setToken($token, $tokenSecret);
try {
// set the verifier and request Etsy's token credentials url
$acc_token = $oauth1->getAccessToken("https://openapi.etsy.com/v2/oauth/access_token", null, $_GET["oauth_verifier"]);
echo "good";
} catch (OAuthException $e) {
print_r($e->getMessage());
echo "bad";
}
}
?>
I'm getting the following error:
Error processing your OAuth request: Invalid oauth_verifier parameter
I keep reading that the issue is that in the library TwitterOAuth by Abraham we need to set the oauth_verifier in the header and not the body.
I don't know how to do this.
Here is my code:
// twitterlogin.php
session_start();
$twitterKey = 'xxx';
$twitterSecret = 'xxx';
$callback = "https://xxx/socialmedia/twittercallback.php";
require_once("/xxx/twitteroauth/vendor/autoload.php");
use Abraham\TwitterOAuth\TwitterOAuth;
$twitter = new TwitterOAuth($twitterKey,$twitterSecret);
$result = $twitter->oauth('oauth/request_token', ['oauth_callback' => $callback]);
$_SESSION['oauth_token'] = $result['oauth_token'];
$_SESSION['oauth_token_secret'] = $result['oauth_token_secret'];
$url = $twitter->url('oauth/authorize', array('oauth_token' => $result['oauth_token']));
header("Location: $url");
// twittercallback.php
session_start();
require_once("/xxx/api");
$twitterKey = 'xxx';
$twitterSecret = 'xxx';
require_once("/xxx/twitteroauth/vendor/autoload.php");
$twitter = new \Abraham\TwitterOAuth\TwitterOAuth($twitterKey,$twitterSecret,$_SESSION['oauth_token'],$_SESSION['oauth_token_secret']);
try {
$access_token = $twitter->oauth("oauth/access_token", ["oauth_verifier" => $_GET['oauth_verifier']]);
$_SESSION['access_token'] = $access_token;
} catch(\Exception $e){
print "<pre>".print_r($e,true)."</pre>";
}
var_dump($access_token);
How do I set the oauth_verifier in the header?
Thanks!
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');
Using the default Tumblr v2 API, I'm able to connect to my application, and retrieve posts from my own account. However, I'd like users to be able to connect their own accounts. I've tried to figure this out but I'm not entirely sure how to use OAuth (im using this class). How is this done?
The code I'm using to retrieve dashboard posts is:
$consumerKey = 'xxx';
$consumerSecret = 'xxx';
$tumblr = new Tumblr\API\Client(
$consumerKey,
$consumerSecret
);
var_dump( $tumblr->getDashboardPosts() ); // using var_dump for testing purposes only
This code works, but it's only returning the code for my PERSONAL account.
I figured it out, thanks to Github user seejohnrun.
require_once 'include/util.php';
$consumerKey = 'XXX';
$consumerSecret = 'XXX';
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');
// If we are visiting the first time
if (!$_GET['oauth_verifier']) {
// grab the oauth token
$resp = $requestHandler->request('POST', 'oauth/request_token', array());
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
// tell the user where to go
echo ' GO ';
$_SESSION['t']=$data['oauth_token'];
$_SESSION['s']=$data['oauth_token_secret'];
} else {
$verifier = $_GET['oauth_verifier'];
// use the stored tokens
$client->setToken($_SESSION['t'], $_SESSION['s']);
// to grab the access tokens
$resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $verifier));
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
// and print out our new keys we got back
$token = $data['oauth_token'];
$secret = $data['oauth_token_secret'];
echo "token: " . $token . "<br/>secret: " . $secret;
// and prove we're in the money
$client = new Tumblr\API\Client($consumerKey, $consumerSecret, $token, $secret);
$info = $client->getUserInfo();
echo "<br/><br/>congrats " . $info->user->name . "!";
}
Has anyone any idea how to tie in the dropbox php api http://code.google.com/p/dropbox-php/ in Yii. I downloaded the source files and put in ext.dropbox and then inserted the following code
$dropbox = Yii::getPathOfAlias('ext.dropbox');
spl_autoload_unregister(array('YiiBase','autoload'));
Yii::registerAutoloader(array('Dropbox_autoload','autoload'));
$consumerKey = '***';
$consumerSecret = '***';
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
try {
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
$dropbox = new Dropbox_API($oauth);
$info = $dropbox->getMetaData('Files');
} catch (Exception $e) {
$error = "error: " . $e->getMessage();
}
spl_autoload_register(array('YiiBase','autoload'));
I get the error Fatal error: Class 'CExceptionEvent' not found in *
I'm not sure about Dropbox specifically, but this is how I included SwiftMailer:
Yii::import('swift.classes.Swift', true);
Yii::registerAutoloader(array('Swift','autoload'));
Yii::import('swift.swift_init', true);
where the setPathOfAlias looks like:
Yii::setPathOfAlias('swift', '/var/www/lib');
(I'm using it for other apps, which is why it isn't in the Yii tree. Other libs I keep in the extensions dir and for simple ones, a basic "import" is often enough.)
Try this one:
$dropbox = Yii::getPathOfAlias('ext.dropbox');
spl_autoload_unregister(array('YiiBase','autoload'));
Yii::registerAutoloader(array('Dropbox_autoload','autoload'));
$consumerKey = '***';
$consumerSecret = '***';
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
try {
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
$dropbox = new Dropbox_API($oauth);
$info = $dropbox->getMetaData('Files');
} catch (Exception $e) {
$error = "error: " . $e->getMessage();
}
spl_autoload_register(array('YiiBase','autoload'));
Yii::import('swift.classes.Swift', true);
Yii::registerAutoloader(array('Swift','autoload'));
Yii::import('swift.swift_init', true);
Yii::setPathOfAlias('swift', '/var/www/lib');
Also there is a API libary for download in this Dropbox Lib
and also a PHP 5.3 SDK for the Dropbox REST API