[file-get-contents]: failed to open stream - facebook-graph-api

I have the correct access_token, but when I call file_get_contents, I receive the following error:
[function.file-get-contents]: failed to open stream: HTTP request failed!
HTTP/1.1 400 Bad Request in..
Pasting the echo $fql_query_url in the browser gives the correct result?
Is it an encoding problem? I ask because other fql queries work fine. Here's the offending code:
<?php
require_once('facebook.php');
//GET access token
echo $uid = $facebook->getUser();
$access_token = $facebook->getAccessToken();
echo '<br>';
// Run fql query
$fql_query_url = 'https://graph.facebook.com/'
. '/fql?q=SELECT page_id from page_admin WHERE uid='.$uid.''
. '&access_token=' . $access_token;
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
//display results of fql query
echo '<br><pre>';
print_r("query results:");
print_r($fql_query_obj);
echo '</pre>';

Related

Google API 0Auth2 cannot retrieve access token

I am new to Google API. I have a script to import all mails into Google Groups but I cannot get the API to work.
I have my client_id, client_secret
then I used this link:
https://accounts.google.com/o/oauth2/auth?client_id=[CLIENID]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/apps.groups.migration&response_type=code
where I replaced the [CLIENDID] with my ClientID, I can authenticate and get back the AuthCode which I then used to run this command:
curl --request POST --data "code=[AUTHCODE]&client_id=[CLIENTID]&client_secret=[CLIENTSECRET]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token
This works and shows me the refresh token, however, the script does say authentication failed. So I tried to run the command again and it says
"error": "invalid_grant"
"error_description": "Bad Request"
If I reopen the link above, get a new authcode and run the command again, it works but only for the first time. I am on a NPO Google Account and I activated the Trial Period.
Can anyone help me out here?
Complete script:
client_id="..."
client_secret="...."
refresh_token="......"
function usage() {
(
echo "usage: $0 <group-address> <mbox-dir>"
) >&2
exit 5
}
GROUP="$1"
shift
MBOX_DIR="$1"
shift
[ -z "$GROUP" -o -z "$MBOX_DIR" ] && usage
token=$(curl -s --request POST --data "client_id=$client_id&client_secret=$client_secret&refresh_token=$refresh_token&grant_type=refresh_token" https://accounts.google.com/o/oauth2/token | sed -n "s/^\s*\"access_token\":\s*\"\([^\"]*\)\",$/\1/p")
# create done folder if it doesn't already exist
DONE_FOLDER=$MBOX_DIR/../done
mkdir -p $DONE_FOLDER
i=0
for file in $MBOX_DIR/*; do
echo "importing $file"
response=$(curl -s -H"Authorization: Bearer $token" -H'Content-Type: message/rfc822' -X POST "https://www.googleapis.com/upload/groups/v1/groups/$GROUP/archive?uploadType=media" --data-binary #${file})
result=$(echo $response | grep -c "SUCCESS")
# check to see if it worked
if [[ $result -eq 0 ]]; then
echo "upload failed on file $file. please run command again to resume."
exit 1
fi
# it worked! move message to the done folder
mv $file $DONE_FOLDER/
((i=i+1))
if [[ $i -gt 9 ]]; then
expires_in=$(curl -s "https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=$token" | sed -n "s/^\s*\"expires_in\":\s*\([0-9]*\),$/\1/p")
if [[ $expires_in -lt 300 ]]; then
# refresh token
echo "Refreshing token..."
token=$(curl -s --request POST --data "client_id=$client_id&client_secret=$client_secret&refresh_token=$refresh_token&grant_type=refresh_token" https://accounts.google.com/o/oauth2/token | sed -n "s/^\s*\"access_token\":\s*\"\([^\"]*\)\",$/\1/p")
fi
i=0
fi
done

How to convert raw cookie into Netscape jar cookie and save to file?

I want to pass a cookie file to youtube-dl coming from a browser extension. The extension sends cookie in raw format i.e. like VISITOR_INFO1_LIVE=_SebbjYciU0; YSC=tSqadPjjfd8; PREF=f4=4000000
But youtube-dl takes netscape jar format cookies (as far as I know).
If I put the raw text cookie in a file and pass it to --cookies=file.txt argument youtube-dl raises an exception.
I cannot manage to to convert my raw cookies to jar cookies and save to a file in the disk. I have searched for a solution but did not find any acceptable solution.
I had very similar problem to convert "curl" cookies into wget one ...
Netscape CookieJar format is straight forward see *
so I took few minutes to write a quick perl script and generate a cookiejar
here is the code (downloadable here)
#!/usr/bin/perl
# usage
# perl cookiejar.pl {{url}} '{{cookie-string}}'
use YAML::Syck qw(Dump);
my $expires = $^T + 86400;
my $path = '/';
my $url = shift;
my $cookies = shift;
printf "--- # %s at %u\n",__FILE__,$^T;
my $domain;
my $p = index($url,'://')+3;
my $l = index($url,'/',$p);
$domain = substr($url,$p,$l-$p);
my $dots = () = $domain =~ /\./g;
printf "dots: %s\n",$dots;
if ($dots > 1) {
$domain = substr($domain,index($domain,'.'));
} else {
$domain = '.'.$domain;
}
printf "domain: %s\n",$domain;
printf "url: %s\n",$url;
local *F; open F,'>','cookiejar.txt' or warn $!;
print F <<EOT;
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file! Do not edit.
# domain: $domain
# url: $url
EOT
my #cookies = split'; ',$cookies;
printf "--- %s...\n",Dump(\#cookies);
foreach my $cookie (#cookies) {
my ($key,$value) = split('=',$cookie);
if (! $seen{$key}++) {
# domain access path sec expire cookie value
printf F "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",$domain,'TRUE',$path,'FALSE',$expires,$key,$value;
}
}
close F;
print "info: cookiejar.txt created\n";
printf "cmd: wget --load-cookie-file cookiejar.txt --referer=%s -p %s\n",$domain,$url;
printf "cmd: youtube-dl --cookie cookiejar.txt -referer %s %s\n",$domain,$url;
exit $?;
1; # $Source: /my/perl/scripts/cookiejar.pl $
you run it as followed :
perl cookiejar.pl https://example.com/ 'cookie1=value1; cookie2=value2'
note:
You might need to install YAML::Syck with
cpan install YAML::Syck
or just comment out the Dump() call and the use YAML::Syck line.

How can i perform a keyword search for all the public feeds in facebook via FB api?

A Keyword search which helps to give me all the recent feeds.
I found the graph api - search
https://developers.facebook.com/docs/reference/api/examples/
but i dont know to use them!
all i tried was
$keyword = $_POST['keyword'];
$graph_url = "https://graph.facebook.com/search?";
$graph_url .= "&type=post";
$graph_url .= "&q=$keyword";
$results = file_get_contents( $graph_url );
$json = json_decode($results);
foreach($json->data as $show ) {
echo $show->from->name . "<br />";
echo $show->message . "<br />";
echo $show->created_time . "<br />";
echo "<hr>";
}
Stilll i get errors like
Warning : failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

Using Proxy for WebRequest in Powershell

I use WebRequest in a script powershell for check url is valid and detect webservice is available.
My script WebRequest
$request = [System.Net.WebRequest]::Create($WebServiceSSRSRDL)
$request.Method = 'HEAD'
$request.Credentials = [System.Net.CredentialCache]::DefaultCredentials
if ($request.Proxy -ne $null)
{
$request.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
}
$response = $request.GetResponse()
$httpStatus = $response.StatusCode
$urlIsValid = ($httpStatus -eq 'OK')
$tryError = $null
$response.Close()
But I get the error The remote server returned an error: (500) Internal Server Error.
If I use WebClient I get not error, all is OK.
My script WebClient
$webclient = New-Object Net.WebClient
$webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
if($webclient.Proxy -ne $null)
{
$webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
}
$webclient.DownloadString($WebServiceSSRSRDL) | Out-Null
I would like use WebRequest in my script. Any suggestions about it ?

PHP header location - not displaying error?

When I echo something before header("LOCATION: page.php"); - I expect to show an error but it didnt, it just redirect to that page. What gone wrong?
Example:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo "Hello";
header("LOCATION: page.php");
?>
In the php.ini
output_buffering = off
error_reporting = E_ALL
display_errors = on
I am using Wamp, PHP 5.3.0
var_dump(ini_get('output_buffering'));
and what about this one
var_dump(ob_get_status(1));
?