I was using the facebook graph api without batch requests to schedule posts of pictures to my pages.
Now I am trying to work with batch requests but I have an issue, the photo are posted immediately, even if I set the parameters to delay it.
Example:
$args = array(
'message' => $this->message,
);
$args['published'] = false;
$args['scheduled_publish_time'] = strtotime($this->programmed_dt);
$appsecretProof = hash_hmac('sha256', $facebookPage['access_token'], self::APP_SECRET);
$queries[] = array('method' => 'POST',
'relative_url' => urlencode('/' . $facebookPage['id'] . '/photos?access_token=' . $facebookPage['access_token'] . '&appsecret_proof=' . $appsecretProof),
'body' => $args,
'attached_files' => 'file1',
);
$params['file1'] = '#' . realpath('images/' . $timestamp . '.jpg');
$urlPost = '?batch=' . json_encode($queries) . '&access_token=' . self::ACCESS_TOKEN;
$res = $this->fb->api($urlPost, 'POST', $params);
I have a response with the post ID, but the photo post is not scheduled ... Do you see anything wrong?
Thanks.
Found:
published and scheduled_publish_time should be passed to the relative_url
$relativeUrl = '/' . $facebookPage['id'] . '/photos?published=' . (string) $args['published'];
$relativeUrl .= $args['published'] ? '' : '&scheduled_publish_time=' . $args['scheduled_publish_time'] . '&access_token=' . $facebookPage['access_token'] . '&appsecret_proof=' . $appsecretProof;
Related
enter code hereI just started working with OAuth1 API's calling from php using GuzzleHttp\Client.
In postman it's working fine.In netsuite log file I can see only required parameters is missing. I'm not understanding where I'm going wrong. whenever I'm calling the API I'm getting response as
#message: """
Client error: `GET https://7085372.suitetalk.api.netsuite.com/services/rest/record/v1/customer/` resulted in a `400 Bad Request` response:
{"type":"https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1","title":"Bad Request","status":400,"o:errorD (truncated...)
"""
#code: 400
php code
$realm = 'xxxxxxx';
$consumer_key = 'xxxxxxxxx';
$oauth_token = 'xxxxxxxxxxxxx';
$oauth_signature_method = 'HMAC-SHA256';
$oauth_version = '1.0';
$consumer_secret = 'xxxxxxxxxxxxx';
$token_secrect = 'xxxxxxxxxxxxx';
$timeStamp = Carbon::now()->timestamp;
$oauth_none = Str::random(11);
$base = 'GET' . "&" . urlencode('https://7085372.suitetalk.api.netsuite.com/services/rest/record/v1/customer/') . "&" . urlencode("oauth_consumer_key=" . urlencode($consumer_key) . "&oauth_nonce=" . urlencode($oauth_none) . "&oauth_signature_method=" . urlencode($oauth_signature_method) . "&oauth_timestamp=" . urlencode($timeStamp) . "&oauth_token=" . urlencode($oauth_token) . "&oauth_version=" . urlencode($oauth_version) . "&realm=" . urlencode($realm));
$key = urlencode($consumer_secret) . "&" . urlencode($token_secrect);
$oauth_signature = base64_encode(hash_hmac('sha256', $base, $key, true));
$authorization = 'OAuth oauth_consumer_key=' . $consumer_key . ',oauth_nonce=' . $oauth_none . ',oauth_signature_method=' . $oauth_signature_method . ',oauth_timestamp=' . $timeStamp . ',oauth_token=' . $oauth_token . ',oauth_version=' . $oauth_version . ',realm=' . $realm . ',oauth_signature=' . $oauth_signature . '';
try {
$client = new Client();
$headers = [
'Authorization' => $authorization,
'Content-Type' => 'application/json',
'Cookie' => 'NS_ROUTING_VERSION=LAGGING'
];
$body = '';
$request = new Request('GET', 'https://7085372.suitetalk.api.netsuite.com/services/rest/record/v1/customer/', $headers, $body);
$res = $client->sendAsync($request)->wait();
dd('here', $res, $res->getBody());
} catch (RequestException $e) {
dd($e->getResponse(), $e);
}
Postman Collection
In this line:
$base = 'GET' . "&" . urlencode('https://7085372.suitetalk.api.netsuite.com/services/rest/record/v1/customer/') . "&" . urlencode("oauth_consumer_key=" . urlencode($consumer_key) . "&oauth_nonce=" . urlencode($oauth_none) . "&oauth_signature_method=" . urlencode($oauth_signature_method) . "&oauth_timestamp=" . urlencode($timeStamp) . "&oauth_token=" . urlencode($oauth_token) . "&oauth_version=" . urlencode($oauth_version) . "&realm=" . urlencode($realm));
From urlencode("oauth_consumer_key=" . to "&realm=" . urlencode($realm));, you are url_encoding twice, and you are url_encoding = symbols. So your $authorization string is absolutely malformed, and it results in missing parameters.
This can occurs when you write such a long line, and it's difficult to see. It's better to write it down this way:
$base = 'GET&' . urlencode('https://7085372.suitetalk.api.netsuite.com/services/rest/record/v1/customer/')
. '&oauth_consumer_key=' . urlencode($consumer_key)
. '&oauth_nonce=' . urlencode($oauth_none)
. '&oauth_signature_method=' . urlencode($oauth_signature_method)
. '&oauth_timestamp=' . urlencode($timeStamp)
. '&oauth_token=' . urlencode($oauth_token)
. '&oauth_version=' . urlencode($oauth_version)
. '&realm=' . urlencode($realm);
But it's even better to use an array and http_build_query() function, this way:
$url = 'https://7085372.suitetalk.api.netsuite.com/services/rest/record/v1/customer/';
$data = array(
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => $oauth_none,
'oauth_signature_method' => $oauth_signature_method,
'oauth_timestamp' => $timeStamp,
'oauth_token' => $oauth_token,
'oauth_version' => $oauth_version,
'realm' => $realm,
);
$base = 'GET&' . urlencode($url) . '&' . http_build_query($data);
Note.- Also, it's a good idea to use a variable for the url, since you are using it more than 1 time.
However, maybe there are more issues left. I'm not sure if you are are doing well the request.
And it's a good idea to have a function to generate OAuth signature.
Here you are a remaking of your code:
$url = 'https://7085372.suitetalk.api.netsuite.com/services/rest/record/v1/customer/';
$consumer_secret = 'xxxxxxxxxx';
$token_secret = 'xxxxxxxxxx';
$data = array(
'oauth_consumer_key' => 'xxxxxxxxxx',
'oauth_nonce' => Str::random(11),
'oauth_signature_method' => 'HMAC-SHA256',
'oauth_timestamp' => Carbon::now()->timestamp,
'oauth_token' => 'xxxxxxxxxx',
'oauth_version' => '1.0',
'realm' => 'xxxxxxxxxx',
);
$oauth_signature = generateOauthSignature(
'GET',
$url,
$data['oauth_consumer_key'],
$data['oauth_nonce'],
$data['oauth_signature_method'],
$data['oauth_timestamp'],
$data['outh_version'],
$consumer_secret,
$token_secret,
$data['oauth_token'],
array('realm' => $data['realm']),
);
$authorization = 'OAuth ';
foreach ($data as $key => $val) {
$authorization .= ',' . $key . '=' . $val;
}
$authorization .= ',oauth_signature=' . $oauth_signature;
try {
$client = new Client();
$headers = [
'Authorization' => $authorization,
'Content-Type' => 'application/json',
'Cookie' => 'NS_ROUTING_VERSION=LAGGING'
];
$body = '';
$request = new Request('GET', $url, $headers, $body);
$res = $client->sendAsync($request)->wait();
dd('here', $res, $res->getBody());
} catch (RequestException $e) {
dd($e->getResponse(), $e);
}
function generateOauthSignature($method, $url, $consumerKey, $nonce, $signatureMethod, $timestamp, $version, $consumerSecret, $tokenSecret, $tokenValue, $extraParams = array())
{
$base = strtoupper($method) . "&" . rawurlencode($url) . "&"
. rawurlencode("oauth_consumer_key=" . $consumerKey
. "&oauth_nonce=" . $nonce
. "&oauth_signature_method=" . $signatureMethod
. "&oauth_timestamp=" . $timestamp
. "&oauth_token=" . $tokenValue
. "&oauth_version=" . $version);
if (!empty($extraParams)) {
$base .= rawurlencode("&" . http_build_query($extraParams));
}
$key = rawurlencode($consumerSecret) . '&' . rawurlencode($tokenSecret);
$signature = base64_encode(hash_hmac('sha1', $base, $key, true));
return rawurlencode($signature);
}
So, I had issue because I don't have OAuth 1, so to access to ETSY API I had to struggle.
finally I have this :
My function __construct() :
parent::__construct();
$this->allowForUser();
$this->etsyDb = new EtsyordersModel;
$this->client = new Etsy([
'identifier' => getApp()->getConfig('identifier'),
'secret' => getApp()->getConfig('secret'),
'scope' => 'listings_r transactions_r',
'callback_uri' => 'http://*****-*****.loc/etsyapp/next',
'access_token' => getApp()->getConfig('access_token'),
'access_token_secret' => getApp()->getConfig('access_token_secret'),
]);
And here is how I get the url to access my JSON with the result :
$key = rawurlencode($oauthtoken) . "&" . rawurlencode($tokensecret);
$method = "GET";
$baseurl = "https://openapi.etsy.com/v2/shops/24749672/receipts/";
$nonce = "1234";
$timenow = time();
$oauthversion = '1.0';
$paramstring = "oauth_consumer_key=" . $oauthconsumerkey . "&oauth_nonce=" . $nonce . "&oauth_signature_method=HMAC-SHA1" . "&oauth_timestamp=" . $timenow . "&oauth_token=" . $clientsecret . "&oauth_version=" . $oauthversion;
//
// Signature GET to retrieve signature OAuth
$encodeurl = $method . "&" . rawurlencode($baseurl) . "&" . rawurlencode($paramstring);
$signature = hash_hmac( 'sha1', $encodeurl, $key, TRUE );
$signature = base64_encode( $signature );
// url JSON shop ETSY
$curlurl = $baseurl . "?" . $paramstring . "&oauth_signature=" . $signature;
// get JSON content in $orders
$orders = file_get_contents($curlurl);
And so I get my JSON :
I got 90++ items, but because of the pagination, it show me only 25 items.
I tried to add active?limit=100&offset=0 to my $baseurl (at the end of it, as parameter for signature calculation) but it say oauth_problem=signature_invalid&. I tried to put it on the $paramstring
instead, but same result.
How can I have all my result shown, or how can I access to page 2, 3, etc... of the pagination?
I am on PHP 7.4, XAMPP (one of the last version), I don't have OAuth 1 so I used a combination of package Y0lk\OAuth1 and thephpleague\oauth1-client.
Thank you for your time.
I have created a facebook app in last month.
I am trying to upload a video from my PHP code, but it throws an error that (#353) You must select a video file to upload. While I tried to upload the same video from my Facebook account directly and it gets uploaded properly.
I don't know what is wrong things that exists, PHP code is as below
$api="/me/videos";
$uploaded_videos=$facebook->api($api);
$video_file_path=$user_dir_abs_path."/NewProject20.mov";
if(file_exists($video_file_path))
{
echo "file exists...";
}else{
die("not exist");
}
$ret_obj = $facebook->api('/me/videos', 'POST', array(
'source' => '#' . $video_file_path,
'title' => "This is just a test",
'description' => 'test9000',
'privacy' => json_encode(array('value' => 'EVERYONE' )),
)
);
echo '<pre>'. $ret_obj.'</pre>';
Video I have uploaded is here
Document I refer to code is here
https://developers.facebook.com/blog/post/493/
https://developers.facebook.com/blog/post/608/
I have used following code as well, but I am getting that same error..
$id=$facebook->getUser(); /* UID of the connected user */
$api="/".$id."/videos";
echo "api -> $api";
/*$ret_obj = $facebook->api('/me/videos', 'POST', array(*/
$ret_obj = $facebook->api($api, 'POST', array(
'source' => '#' . $video_file_path,
'title' => "This is just a test",
'description' => 'test9000',
'privacy' => json_encode(array('value' => 'EVERYONE' )),
)
);
echo '<pre>'. $ret_obj.'</pre>';
From your comments, I got to know that you need to upload/post a video from your server to facebook, instead form posting method specified in documentation.
I don't know much about facebook-sdk, I would suggest you to use CURL method instead.
<?php
$app_id = "XXXXXXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXXXXXXXXXXXX";
$my_url = "http://localhost/url_of_this_page.php";
$video_title = "Video title here";
$video_desc = "Video description here";
$code = $_REQUEST["code"];
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$post_url = "https://graph-video.facebook.com/me/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&". $access_token;
$ch = curl_init();
$data = array('name' => 'file', 'file' => '#'.realpath("ipad.mp4"));// use realpath
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
if( $res === false ) {
echo curl_error($ch);
}
curl_close($ch);
?>
As a response from facebooks, you'l get video id like:
{"id":"892161284142980"}
Have a look at https://developers.facebook.com/docs/graph-api/reference/v2.0/user/videos/#publish
Videos must be encoded as multipart/form-data and published to
graph-video.facebook.com instead of the regular Graph API URL.
If anyone is still having issue uploading videos via the facebook sdk4 api, the below code worked for me:
$file_name = "/absolute/path/to/file/in/directory";
$linkData = [
'source' => new \CURLFile($file_name, 'video/m4v'),
'message' => 'Your message',
];
$ret_obj = $facebook->api('/me/videos', 'POST', $linkData);
I am trying to construct a custom upload script that uploads to a defined flickr account, I'm looking for something other than phpflickr (which uses the deprecated version of the flickr api). I know how to authorize flickr calls in the new method and have successfully used at least half-a-dozen of their methods but I'm failing to understand how to upload (mainly due to the fact of very limited documentation).
Here is my authorization call that I'm using:
<?php
$url = "format=" . $this->format;
$url .= "&method=" . $method;
$url .= "&nojsoncallback=1";
$url .= "&oauth_consumer_key=" . $this->flickr_key;
$url .= "&oauth_nonce=" . $this->nonce;
$url .= "&oauth_signature_method=" . $this->sig_method;
$url .= "&oauth_timestamp=" . $this->timestamp;
$url .= "&oauth_token=" . $access_token;
$url .= "&oauth_version=1.0";
$url .= "&photoset_id=" . $photoset_id;
$baseurl = "GET&" . urlencode( $flickr_upload_call ) . "&" . urlencode( $url );
$hashkey = $this->flickr_secret . "&" . $access_token_secret;
$oauth_signature = base64_encode( hash_hmac( 'sha1', $baseurl, $hashkey, true ));
$url_parameters = array(
'method' =>$method,
'oauth_consumer_key' =>$this->flickr_key,
'photoset_id' =>$photoset_id,
'format' =>$this->format,
'nojsoncallback' =>'1',
'oauth_nonce' =>$this->nonce,
'oauth_timestamp' =>$this->timestamp,
'oauth_signature_method'=>$this->sig_method,
'oauth_version' =>'1.0',
'oauth_token' =>$access_token,
'oauth_signature' =>$oauth_signature
);
/* Now that we have encoded the parameters for our ouath_signature
* and have reformated them for the url we need to send... we must
* re-urlencode them too. */
$parameters_string = "";
foreach ( $url_parameters as $key=>$value )
$parameters_string .= "$key=" . urlencode( $value ) . "&";
$url = $flickr_api_call . "?" . $parameters_string;
So, what I need to know is how to change this to allow for the flickr upload api to accept it; any ways I can do this better, or how can I use what is in phpflickr and convert it to the new method to suit my needs?
I am able to get access_token for multiple permissions like emails, contacts, docs, etc. using oAuth 2.0. I have access_token
I got contacts using the following code.
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max- results='.$max_results.'&oauth_token='.$access_token;
$response_contacts= curl_get_file_contents($url);
Now i want to get users Emails using this access_token.
i used this url . but it gives 401 unauthorized Error
$url = 'https://mail.google.com/mail/feed/atom&oauth_token='.$access_token;
$response_emails= curl_get_file_contents($url);
please guide me how can i get emails using access_token.
I've seen references to the Gmail feed using oauth_token as a request parameter. However, once I used the OAuth Playground I discovered that you need to pass your OAuth information as an Authorization header, as you'll see below.
<?php
$now = time();
$consumer = ...; // your own value here
$secret = ...; // your own value here
$nonce = ...; // same value you've been using
$algo = "sha1";
$sigmeth = "HMAC-SHA1";
$av = "1.0";
$scope = "https://mail.google.com/mail/feed/atom";
$path = $scope;
$auth = ...; // an object containing outputs of OAuthGetAccessToken
$args = "oauth_consumer_key=" . urlencode($consumer) .
"&oauth_nonce=" . urlencode($nonce) .
"&oauth_signature_method=" . urlencode($sigmeth) .
"&oauth_timestamp=" . urlencode($now) .
"&oauth_token=" . urlencode($auth->oauth_token) .
"&oauth_version=" . urlencode($av);
$base = "GET&" . urlencode($path) . "&" . urlencode($args);
$sig = base64_encode(hash_hmac($algo, $base,
"{$secret}&{$auth->oauth_token_secret}", true));
$url = $path . "?oauth_signature=" . urlencode($sig) . "&" . $args;
// Create a stream
$opts = array(
"http" => array(
"method" => "GET",
"header" => "Authorization: OAuth " .
"oauth_version=\"{$av}\", " .
"oauth_nonce=\"{$nonce}\", " .
"oauth_timestamp=\"{$now}\", " .
"oauth_consumer_key=\"{$consumer}\", " .
"oauth_token=\"{$auth->oauth_token}\", " .
"oauth_signature_method=\"{$sigmeth}\", " .
"oauth_signature=\"{$sig}\"\r\n"
)
);
$context = stream_context_create($opts);
$out = file_get_contents($path, false, $context);
?>