I'm trying to call to Photo.upload on the the Facebook API server. As far as I can tell the code to construct the call is good but I can't understand the responce I'm getting back from the server. As far as I can see, this call is ment to work and other people don't get this problem. I can only reason that something is wrong with the code. The commented stuff is an artifact of me trying different things to get a different responce from the server. The original code that I've changed was in part taken from an example of how to do this which I couldn't get to work either really:
http://www.jaisenmathai.com/blog/2008/11/27/using-the-facebook-api-to-upload-photos/
Server Responce:
12 This API version is deprecated method photos.upload api_key b92cee19a33c861275bfce4695896e44 call_id 1250194789.61 garden_jpg /var/www/vivaladan/pictureyourselfhull/garden.jpg v 0 sig 896ee95339cad24ce7e64a05ca764123
Code:
$key = b92cee19a33c861275bfce4695896e44;
$ver = 1.0;
$cid = microtime(true);
$uid = BIGINT;
$file= "garden.jpg";
$args = array(
//amethod => photos.upload,
v => $ver,
api_key => $key,
//uid => $uid,
call_id => $cid,
//format => XML
);
$args[basename($file)] = realpath($file);
signRequest($args,$sec);
$postString = "";
foreach($args as $index => $value) {
$postString .= $index ."=".$value."&";
}
$postString = trim($postString, '&');
$ch = curl_init();
$url = "http://api.facebook.com/restserver.php?method=photos.upload";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$data = curl_exec($ch);
echo $data;
function signRequest(&$args, $secret){
ksort($args);
$sig = "";
foreach($args as $k => $v){
$sig .= $k . '=' . $v;
}
$sig .= $secret;
$args[sig] = md5($sig);
}
Rest API call is not working anymore.
Try GraphApi
I'm guessing it's because you're using $ver = 0.0; - there's no 0.0 version of the API.
try using api.new.facebook.com API URL instead
Related
am new in api trying to see if my Api works once I execute the code am geting Error code 32601 'Method not found' I have gone through the code and though am not sure I think the error is syntax related any guidance will be appreciated
<?php
$key = '';
$secret = '';
$path = 'https://api.cloudtrax.com/voucher/network_id=254281';
$data = array(
"code"=> "b3c34test",
"duration"=> 1,
"max_users"=> 1,
"up_limit"=> 10,
"down_limit"=> 20,
"comment"=> "Free access for guests",
"purge_days"=> 2);
$headers = array(
'nonce' => 'ThisIsANonce',
'authorization' => "key=" . $key . ",timestamp=" . time() . ",nonce=" . $nonce,
'authorization_header' => "Authorization: " . $authorization,
'signature' => hash_hmac('sha256', $authorization . $path . $data, $secret),
);
$ch = curl_init($path);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if ($result == FALSE) {
if (curl_errno($ch) == 0)
echo "#### NOTE ####: nil HTTP return: This API call appears to be broken" . "\n";
else
throw new Exception(curl_error($ch), curl_errno($ch)); }
else
echo "RESULT: \n" . $result . "\n";
?>
I'm working on Instagram basic api, in my project user can connect their Instagram feed to our project. For that I'm generating a short live authentication code but could not get the long live access code. Its returning null.
Here is my code for getting long live access code.
$client_secret = "###############";
$client_id = "###############";
$redirect_uri = '#########';
$short_access_token = "####"; // getting this from another function
$ig_rtu = 'https://api.instagram.com/oauth/access_token?client_id='.$client_id.'&client_secret='.$client_secret.'&grant_type=authorization_code&redirect_uri='.$redirect_uri.'&access_token='.$short_access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ig_rtu);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ig_new = curl_exec($ch);
curl_close ($ch);
return $ig_new = json_decode($ig_new, true);
In postman this works fine but I could not get it done in Laravel.
What I'm missing here ?
Any lead is appreciated.
I've done this in 3 steps. I will leave the code out for step 1 and include my implementation for step 2 and 3.
Steps:
get the code
get the short lived token using the code
get the long lived token using the short lived token
Here are the public variables used.
public $appId = null;
public $appSecret = null;
public $redirectUrl = null;
// Instagram request data
public $socialUserId = null;
public $socialAuthCode = null;
public $socialUserName = null;
public $socialShortAccessToken = null; // 1 hour
public $socialLongAccessToken = null; // 60 days
public $socialLongAccessTokenExpiresInSeconds = null; // 5183910 seconds || 59.99896 days - relative to date created
Step 2:
public function getUserIdAndShortAccessToken(){
/* Get UserId and access Token
https://api.instagram.com/oauth/access_token \
-F client_id={app-id} \
-F client_secret={app-secret} \
-F grant_type=authorization_code \
-F redirect_uri={redirect-uri} \
-F code={code}
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'https://api.instagram.com/oauth/access_token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'client_id' => $this->appId,
'client_secret' => $this->appSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->socialAuthCode,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$curlResult = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
curl_close($ch);
return;
}else{
curl_close($ch);
}
$this->socialUserId = json_decode($curlResult)->user_id;
$this->socialShortAccessToken = json_decode($curlResult)->access_token;
}
Step 3:
public function getLongAccessToken(){
/* Get long-lived token
curl -i -X GET "https://graph.instagram.com/access_token
?grant_type=ig_exchange_token
&client_secret={instagram-app-secret}
&access_token={short-lived-access-token}"
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret='
.$this->appSecret.'&access_token='
.$this->socialShortAccessToken
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlResult = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
curl_close($ch);
return;
}else{
curl_close($ch);
}
$this->socialLongAccessToken = json_decode($curlResult)->access_token;
$this->socialLongAccessTokenExpiresInSecond = json_decode($curlResult)->expires_in;
}
I have a problem in getting access token. After getting auth code, when I called my get_access_token it returns "invalid_client" error. I researched about it but nothing helped me. Please, look at my code and help me to solve this problem. Thank you in advance.
Here is my code:
public function get_access_token($zoho_code)
{
$headers = array(
);
$taskurl = 'https://accounts.zoho.com/oauth/v2/token';
$cdata = array(
'code' => $zoho_code,
'grant_type' => 'authorization_code',
'client_id' => $this->client_id,
'client_secret' => $this->client_secret_id,
'redirect_uri' => 'http://localhost/callback.php',
'scope' => 'ZohoMail.accounts.UPDATE,ZohoMail.accounts.READ,ZohoMail.partner.organization.READ,ZohoMail.partner.organization.UPDATE,ZohoMail.organization.accounts.CREATE,ZohoMail.organization.accounts.UPDATE,ZohoMail.organization.accounts.READ,ZohoMail.organization.domains.CREATE,ZohoMail.organization.domains.UPDATE,ZohoMail.organization.domains.DELETE,ZohoMail.organization.domains.READ',
'state' => '55555sfdfsdfgbcv',
);
$curlresult = $this->docurl($taskurl, $cdata, $headers);
return $curlresult;
}
public function docurl($taskurl, $cdata, $headers, $method = 'post',$sendjson=true) {
$ch = curl_init();
if ($method == 'get') {
if ($cdata) {
$query = '?' . http_build_query($cdata);
$taskurl .= $query;
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
} elseif ($method == 'delete') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
} elseif ($method == 'put') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
} elseif ($method == 'patch') {
if($sendjson) $cdata = json_encode($cdata);
curl_setopt($ch, CURLOPT_POSTFIELDS, $cdata);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
} else {
if($sendjson) $cdata = json_encode($cdata);
curl_setopt($ch, CURLOPT_POSTFIELDS, $cdata);
}
curl_setopt($ch, CURLOPT_URL, $taskurl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$res = curl_exec($ch);
$information = curl_getinfo($ch);
print_r($information);
print_r($cdata);
curl_close($ch);
$resj = json_decode($res);
return $resj;
}
If I read your code correctly, you are sending your client_secret as part of a POST request body encoded as JSON.
You should make a POST request with application/x-www-form-urlencoded body and you should include the Authorization header with the client_secret encoded in the Basic scheme. For more info, see the OAuth2 RFC.
You need to change accounts.zoho.com according to your data centre. For example accounts.zoho.in for India. You can see this in the URL of your Zoho web app. It is written in their docs link.
I'm developing with localbitcoin API and i am using path “/api/dashboard/closed/” and this is my code:
<?php
function localbitcoinsquery($path, $nonce,array $req = Array()) {
global $random;
$key='mykey';
$secret='secretkey';
if ($req) {
$get=httpbuildquery($req);
$path=$path.'?'.$get;
}
$postdata=$nonce.$key.$path;
$sign = strtoupper(hashhmac('sha256', $postdata, $secret));
$headers = array(
'Apiauth-Signature:'.$sign,
'Apiauth-Key:'.$key,
'Apiauth-Nonce:'.$nonce
);
$ch = null;
$ch = curlinit('https://localbitcoins.com'.$path);
curlsetopt($ch, CURLOPTRETURNTRANSFER, true);
curlsetopt($ch, CURLOPTHTTPHEADER, $headers);
curlsetopt($ch, CURLOPTSSLVERIFYPEER, TRUE);
curlsetopt($ch, CURLOPTCONNECTTIMEOUT, 20);
$res = curlexec($ch);
if ($res === false) throw new Exception('Curl error: '.curlerror($ch));
$dec = jsondecode($res, true);
if (!$dec) throw new Exception('Invalid data: '.$res);
curl_close($ch);
return $dec;
}
$getinfo = array();
$url='/api/dashboard/closed/';
$mt = explode(' ', microtime());
$random = $mt[1].substr($mt[0], 2, 6);
$getinfo = localbitcoinsquery($url,$random);
echo "<pre>";
printr($getinfo);
echo "</pre>";
?>
This works OK, but show only 50 trades,
Also I get this at result:
[pagination] => Array
(
[next] => https://localbitcoins.com/api/dashboard/closed/?order_by=-closed_at&start_at=2017-10-26+17%3U50%3A49%2B00%9A00
)
But I don't know how to use pagination, when I try to use this link at my code I get error:
[message] => HMAC authentication key and signature was given, but they
are invalid. Error 41
I already investigated at google large time but the information is scarce.
I'm using the python library and had the same issue. When I spoke to technical support they said the issue was in the way I was calculating the authentication.
Basically you have to include the pagination url as part of the signature.
On the python library at least, you do not have to change the api endpoint since arguments are being delivered as part of the form data.
So you still access for example "/api/dashboard/closed/" when getting the second page and the "?order_by=-closed_at&start_at=2017-10-26+17%3U50%3A49%2B00%9A00" stuff goes in the form somehow.
The python API does all this for you, you just have to copy the example from the github page.
I fixed error no. 41. I modified your example to show that works, (read my NOTE: comments to understand better where is the problem) Read my NOTE: comments.
<?php
function localbitcoins_query($path, array $req = Array()) {
$key='yourkey';
$secret='yoursecret';
$array_mt = explode(' ', microtime());
$nonce = $array_mt[1].substr($array_mt[0], 2, 6);
$get = "";
if ($req) {
$get=http_build_query($req);
}
$postdata=$nonce.$key.$path.$get; // NOTE: here $postdata goes without '?' char before the parameters!
$sign = strtoupper(hash_hmac('sha256', $postdata, $secret));
$headers = array(
'Apiauth-Signature:'.$sign,
'Apiauth-Key:'.$key,
'Apiauth-Nonce:'.$nonce
);
$ch = null;
$ch = curl_init('https://localbitcoins.com'.$path.( $get=="" ? "" : "?".$get)); // NOTE: here it's necesary '?' char before the parameters!
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
$res = curl_exec($ch);
if ($res === false) throw new Exception('Curl error: '.curlerror($ch));
$dec = json_decode($res, true);
if (!$dec) throw new Exception('Invalid data: '.$res);
curl_close($ch);
return $dec;
}
$getinfo = array();
$api_endpoint = '/api/dashboard/closed/';
$array_params = array( "order_by" => "-closed_at"
, "start_at" => "2019-08-14 18:00:26+00:00"
);
$getinfo = localbitcoins_query($api_endpoint,$array_params);
echo "<pre>"; print_r($getinfo); echo "</pre>";
?
Im looking to get all cohorts, i can get the info if i have the id but i want all the cohorts names and ids. Please let me know if theres a way to do this. Thanks in advance
$functionname = 'core_cohort_get_cohorts';
$cohortIDS = array( '1' );
$data_string = http_build_query(array('cohortids' => $cohortIDS));
$utoken = 'mytoken';
$adduser = 'yoururl/webservice/rest/server.php? wstoken='.$utoken.'&wsfunction='.$functionname.'&moodlewsrestformat=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$adduser);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
echo '<pre>';
print_r(json_decode($server_output));
echo '</pre>';
You could add more method to core_cohort to get all cohort as the following:
In the cohort/externallib.php, add more method:
public static function get_all_cohorts(){
global $DB;
$cohortids = $DB->get_records('cohort', null, null, 'id');
$arrids = array();
foreach($cohortids as $id){
$arrids[] = $id->id;
}
return (new core_cohort_external())->get_cohorts($arrids);
}
In addition, you have to register this webservice called 'core_cohort_get_all_cohorts' to call this webservice from outside. Please tell me if you have further question.