Facebook Graph API - Post a remote image to an album - facebook-graph-api

At the moment I have to download images on the server and post them like this:
$photo = array(
'message' => 'Status',
'source' => '#/full/path/of/the/image.png'
);
$response = $fb->api('/'.$album.'/photos', 'POST', $photo);
I'm using curl on the backend to post this request and it's working like a charm.
I'm wondering if it's possible to post the remote image directly instead to download a local copy?
I tried to do something like this:
$photo = array(
'message' => 'Status',
'source' => file_get_contents('http://www.domain.com/image.png')
);
$response = $fb->api('/'.$album.'/photos', 'POST', $photo);
But I got an exception from the graph API: "(#324) Requires upload file"
It looks like this is happening when you are not sending the multipart/data header which is set automatically when sending an array of data ($data is an array).
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
So I'm doubtful that it's possible to post a remote image.
What do you think?

It is possible to upload a photo by just giving that photo’s URL under the parameter name url.
This info is a little hidden in the description of the photo endpoint, where it just says,
“You can also publish a photo by providing a url param with the photo's URL.”
So instead of providing the sourceparameter, just provide url with the value of the photo’s publicly reachable URL. (All other parameters except source stay the same and are still usable in the same way.)
I tried this recently, and it worked fine. Although, I only tried it for photos with URLs from my app domain – I can’t say for sure if it works for URLs from “anywhere” on the web as well (although i can’t see a good reason for why it shouldn’t).

Related

How authenticate listclients request

I try to get from PHP the information about the currently connected clients.
http://10.0.0.2:8000/admin/listclients?mount=/stream.mpeg
but this required an authenfication, which is not possible from a program. So I tried
http://10.0.0.2:8000/admin/listclients?mount=/stream.mpeg&user=xyz&pass=wert
but Icecast does not accept the user and pass. If I do it inside a browser it works?
In which way I have to provide user and pass?
Or is some additional information required?
Google did not help me.
It's not possible to put user and pass directlyin the URL. These information has to be base64 encoded and put into the header. Below you find a php example:
$url = "http://www.domain.eu:80004/admin/listclients?mount=/stream.mpeg";
$user = "source";
$pass= "password";
$opts = array('http' =>
array(
'method' => 'GET',
'header' => array ('Authorization: Basic '.base64_encode("$user:$pass"))
)
);
$context = stream_context_create($opts);
$list1 = file_get_contents($url1, false, $context);
You can use the admin password or the source password. I the example I have used the source password.

how can I fix conflicting query string params for S3 uploads?

I'm attempting to upload raw image data to S3 in the context of a react-native app.
I have the raw data correct and for the most part I think my code inside react native is working correctly to capture image data.
On my rails server, I'm using the amazon ruby gem to build the details of the url and associated authentication data required to post data to the bucket in question which I'm then rendering into react-native just like a regular react web front end.
# inside the rails server controller
s3_data = S3_BUCKET.presigned_post(key: "uploads/#{SecureRandom.uuid}/${filename}", success_action_status: '201', acl: 'public-read', url: 'https://jd-foo.s3-us-west-2.amazonaws.com')
render json: {s3Data: {fields: s3_data.fields, url: s3_data.url}}
At the moment I attempt to post to S3, I'm using ES6 fetch like the below to build my http request.
saveImage(data) {
var url = data.url
var fields = data.fields
var headers = {'Content-Type': 'multipart/form-data'}
var body = `x-amz-algorithm=${encodeURIComponent(fields['x-amz-algorithm'])}&` +
`x-amz-credential=${encodeURIComponent(fields['x-amz-credential'])}&` +
`x-amz-date=${encodeURIComponent(fields['x-amz-date'])}&` +
`x-amz-signature=${encodeURIComponent(fields['x-amz-signature'])}&` +
`acl=${encodeURIComponent(fields['acl'])}&` +
`key=${encodeURIComponent(fields['key'])}&` +
`policy=${encodeURIComponent(fields['policy'])}&` +
`success_action_status=${encodeURIComponent(fields['success_action_status'])}&` +
`file=${encodeURIComponent('12foo')}`
console.log(body);
return fetch(url, {method: 'POST', body: body, headers: headers})
.then((res) => {console.log('s3 inside api res', res['_bodyText']) ; res.json()} );
}
the logging of the body looks like
x-amz-algorithm=AWS4-HMAC-SHA256&x-amz-credential=AKIAJJ22D4PSUNBB5RAQ%2F20151027%2Fus-west-1%2Fs3%2Faws4_request&x-amz-date=20151027T223159Z&x-amz-signature=42b09d7ae134f803b10ef72d220fe74a630a3f826c7f1f625448277d0a6d93c7&acl=public-read&key=uploads%2F46be8ca3-6d3a-4bb7-a658-f2c8e058bc28%2F%24%7Bfilename%7D&policy=eyJleHBpcmF0aW9uIjoiMjAxNS0xMC0yN1QyMzozMTo1OVoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJqZC1mb28ifSxbInN0YXJ0cy13aXRoIiwiJGtleSIsInVwbG9hZHMvNDZiZThjYTMtNmQzYS00YmI3LWE2NTgtZjJjOGUwNThiYzI4LyJdLHsic3VjY2Vzc19hY3Rpb25fc3RhdHVzIjoiMjAxIn0seyJhY2wiOiJwdWJsaWMtcmVhZCJ9LHsieC1hbXotY3JlZGVudGlhbCI6IkFLSUFKSjIyRDRQU1VOQkI1UkFRLzIwMTUxMDI3L3VzLXdlc3QtMS9zMy9hd3M0X3JlcXVlc3QifSx7IngtYW16LWFsZ29yaXRobSI6IkFXUzQtSE1BQy1TSEEyNTYifSx7IngtYW16LWRhdGUiOiIyMDE1MTAyN1QyMjMxNTlaIn1dfQ%3D%3D&success_action_status=201&file=12foo
It seems like my problems could be tied to both
Bad format of the post body including problems with special characters
Not providing S3 with enough data in post body including keys and other information, the documentation feels a bit unclear about what is/is not required.
The error back from S3 servers looks like
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>MalformedPOSTRequest</Code><Message>The body of your POST request is not well-formed multipart/form-data.</Message> <RequestId>DCE88AC349D7B2E8</RequestId><HostId>AKE1xctETuZMAhBFLfyuFlDxikYUlbAC7YufkM7h8Z8eVQdtLA25Z0Od/a4cMUbfW1nWnGjc+vM=</HostId></Error>
I'm pretty unclear on what my actual problems are and where I should be digging in.
Any input would be greatly appreciated.
<Message>The body of your POST request is not well-formed multipart/form-data.</Message>
It may not be that you're missing values from the body. The most significant issue here is that the structure of your body does not resemble multipart/form-data.
See RFC 2388 for how multipart/form-data works. (Or find a library that builds this for you.)
What you are sending looks more like the application/x-www-form-urlencoded format, which is used by some AWS APIs, but not S3.
There is an example in the S3 docs showing what an example POST body might look like. You should see a substantial difference there.
Note also that POST is intended for browser based uploads. If you are uoloading from code, you're doing a lot of extra work. PUT Object is much more straightforward. The request body is the binary file contents. Or, if this will eventually be done by a browser, then test it with a browser, and let the browser build your form.

Amazon Product API - URL response

is there anyway to dynamically generate the response for the Amazon Product API with using just a URL string?
I see there are PHP and C# libraries but I am just trying to browse to a URL and see the response. I noticed one of the required fields of the URL is a timestamp which makes this tricky. The following page helped to generate the URLs but I can't seem to find a way to do it dynamically?
http://associates-amazon.s3.amazonaws.com/scratchpad/index.html
Thanks!
This is the dynamic search for amazon product
Download aws_signed_request.php from this url
include('aws_signed_request.php');
$public_key = 'xxxxxxxx';
$private_key = 'xxxxxxxxxx';
$associate_tag = 'xxxxxx';
$keywords= 'PHP';
$search_index = 'Books';
// generate signed URL
$request = aws_signed_request('com', array(
'Operation' => 'ItemSearch',
'Keywords' => "Php Books",
"SearchIndex" => "Books",
"Count" => '24',
'ResponseGroup' => 'Large,EditorialReview'), $public_key, $private_key, $associate_tag);
// do request (you could also use curl etc.)
$response = #file_get_contents($request);
Documentation URL HERE
I'm not sure I totally understand your question, but I think the answer is "the data in the Amazon product API is only available via 'signed' URLs". That way, Amazon can track abuse, etc back to the source (i.e. the signer).
If it were possible to get the data with a "static" URL, then you could post that URL all over the Internet and anyone could get the data without signing up with Amazon. It's their data, and they have rules on it's use, so that wouldn't fly with them.
That said, you can usually create URLs with a timestamp in the future (months or even years). But you would still be responsible for it's use/abuse.

Facebook Graph API and FQL like count on photo's both incorrect?

Hey all,
I've made a facebook application for a contest, which allows users to upload their photo. Once uploaded, the photo gets posted to a dedicated album on their profile. Once the photo is there, the users should collect as many likes as possible.
Currently i have tried both the Facebook Graph API and Facebook FQL to retrieve the amount of likes, but the retrieved likes don't always match the likes on a users profile.
Some users claim to have more then 200 likes, yet the API only returned a maximum of 101 likes so far.
All users are requested to grant the application the following permissions:
user_hometown, publish_stream, read_stream, user_photos and offline_access
Using Facebook PHP SDK 3.0.1 I tried this FQL query to collect the amount of likes of a photo:
# fql query
$fql = "SELECT object_id FROM like WHERE object_id=" . $photo_id;
# api request
$request = array(
'method' => 'fql.query', 'query' => $fql
);
# run batch request
$likes = $this->facebook->api($request);
# return like count
return count($likes);
I also tried the following Graph API request (Also with Facebook PHP SDK 3.0.1) to collect the amount of likes of a photo:
$likes = $this->facebook->api($photo_id.'/likes');
return count($likes['data']);
Strangely, neither seem to return correct results. I can understand if the API is slightly inaccurate, but according to the API some pictures recieved 100 likes this morning and then 0 likes a few hours later.
Does anyone have any ideas what i might be doing wrong? Do the photo's and their albums need to be public? Do the people who liked the photo need to have a public profile in order to show up in the API? Do i need to request additional permissions?
Any help or suggestions will be greatly appreciated!
Just had the same problem here. The likes seems to be paginated by hundreds by default. Override this setting by using the "LIMIT" query option.
Does anyone have any ideas what i might be doing wrong?
yes, I believe that it is against Facebook policy to use their likes plugin for a contest. See: https://www.facebook.com/promotions_guidelines.php
# fql query
$fql = "SELECT like_info FROM photo WHERE object_id=" . $photo_id;
# api request
$request = array(
'method' => 'fql.query', 'query' => $fql
);
# run batch request
$likeInfo = $facebook->api(array(
'method' => 'fql.query', 'query' => $fql
));
var_dump($likeInfo);die;
=> get like info of photo

Facebook Graph API Photo Upload Image ID

brand new here.. I've looked everywhere, couldn't find an answer.. :/ Hopefully someone here can help me out. I'm using the graph api to upload a photo, and tag the user in it. This is currently working great. I would like to show the user the image, in the album, so I need the image id to generate the url. I cannot figure out how to do this!! Below are the lines of code that upload/tag the photo, to my app's album. What do I need to do to get this image id? Thanks a lot! -Tim
$user = $facebook->getUser();
function post_image($facebook, $user,
$image_path){
try{ $tag = array(
'tag_uid' => $facebook->getUser(),
'x' => 0,
'y' => 0
);
$tags[] = $tag;
$image = array(
'message'=> 'Your Banner Photo', 'tags' => $tags,
);
$facebook->setFileUploadSupport(true);
$image['image'] = '#'.realpath($image_path);
$facebook->api('/me/photos', 'POST', $image);
echo "";
return true;
}catch(FacebookApiException $e){
echo $e;
return false;
} }
Sadly, you have to do it as a separate query. You have to get the users galleries (first graph API call). then find the gallery ID you want and request the photos (second graph API call) then parse through the photos to find the one you want. Finally you have to request that photo.
-------------------Update:---------------------------
I'm not a very proficient php developer, but i use facebook api all the time with javascript or actionscript. The basic premise is to call the graph api, and it will return a json object of the result. Use getFile or Curl to load the graph api responses.
You can test your graph api calls here:
https://developers.facebook.com/tools/explorer/?method=GET&path=663032752
so try and walk that through from the graph api. Get the user galleries, then get the photos, then get the one you want.
To get the albums, call
"https://graph.facebook.com/me/albums?access_token="._accessToken;
To get the photos in an album you call it using:
https://graph.facebook.com/"._albumID."/photos?access_token="._accessToken;
And then to get the individual photo you would get the photo's ID property and call that.
sometihng like:
"https://graph.facebook.com/".your_photo_id;
I think using CURL is a good approach, but I'm weak on php, so use what you are comfortable with.
You have the photo ID in facebook API response, if it is uploaded successfully:
$result = $facebook->api('/me/photos', 'POST', $image);
echo $result['id'];