I am running a clojurescript (cljs) browser repl and I want to be able to write some text to a local file.
I tried the following:
(spit "abc.txt" "hello")
but this returns:
#<TypeError: Cannot read property 'call' of undefined>
TypeError: Cannot read property 'call' of undefined
Under a clojure repl this will create file "abc.txt" in the root dir of my project.
I realize that 'spit is a clojure function, but I wonder if there is also some easy way to do this in cljs?
Or is this strictly a JavaScript question and not relevant to clojurescript proper?
From a browser I think you can't (like in JS), because security.
From nodejs check the nodejs doc :)
Now if you mix in a little bit of flash (yuk :|...) there's a JS library for that (that you can use in cljs).
https://github.com/dcneiner/Downloadify
I am posting this as an example solution, and I am not saying this is how you should do it. I did experience a few "gotchas" however, so I
think it's worthwhile to document.
Thank you for your answer. I was just trying to find out if there was some easier way before doing what I knew I would have to do otherwise. I decided to go with a server-side write.
I am using cider, chestnut, and a brepl server interfacing with a chrome client. Since I am not familiar with ring servers, compojure, and embedded jetty servers, I did not know how to add a web service to the brepl server (port 10555). Instead I added one to my local apache server.
Because the request comes in to apache from the the jetty server (as opposed to from a browser), I was getting "cross origin resource sharing" issues, namely message:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
whenever I submitted a request to my service's URL.
I was able to get around this issue by adding the following to my http headers:
-access_control_allow_credentials => 'true',
-access_control_allow_origin => 'http://localhost:10555',
That was about the only gotcha. Beyond that, it's a standard cgi solution (yeah, old school -- I come from a perl background). And yes, I know it would have been best if I could have done it in clojure (or Rails).
My cgi ended up looking something like:
#!/usr/bin/perl
use CGI qw(:standard);
print header(
-type => 'text/html',
-access_control_allow_credentials => 'true',
-access_control_allow_origin => 'http://localhost:10555',
);
my $q = CGI->new();
my $src = $q->param('src');
# write to file
open(my $fh, ">>", "vt-src-out.txt")
or die "cannot open < vt-src-out.txt: $!";
print $fh "$src\n";
I used ajax.core as my client api:
:require [ajax.core :refer [GET POST]]
and called the service like:
(GET "http://localhost/cgi-bin/cljs-write-src.cgi" {:params {:src "(defn foo [])(+ 1 1)"}})
node.js example.
Less hassle than writing a cgi.
New to node.js, so just a starter. Did not convert to cljs.
// to run:
// node write_file.js
// to call from cmd line:
// curl localhost:9090?fn=test.txt\&msg=hello%20world
var http = require('http');
var fs = require('fs');
var url = require('url');
var server = http.createServer(function(req, res) {
var fn = url.parse(req.url, true).query['fn'];
var msg = url.parse(req.url, true).query['msg'];
fs.writeFile(process.env.HOME + "/vtstuff/tmp/" + fn, msg + "\n",
function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
console.log("fn=" + fn);
console.log("msg=" + msg);
process.argv.forEach(function (val, index, array) {
console.log(index + ': ' + val);
});
});
res.writeHead(200,{"Content-Type": "text/plain"});
res.end("wrote file\n");
});
server.listen(9090);
Related
So I was trying to connect our SMS (Student Management System) to a government Service or Site. The process requires an authentication token coming from Vanguard. Successfully, I am able to obtain the token... but due to lack of documentation and sample codes in PHP I am having problem communicating to the said service. I was wondering if is it my code that causes the problem or is it my calls that has conflict interfacing to the webservice: Please see code below:
<?php
require_once 'VanguardClient.php';
$endpoint = 'https://3pt.portal.usi.gov.au/Service/v2/UsiService.svc';
function get_sts($endpoint){
$test = true;
$auskey_path = 'Keystore.xml';
$auskey_password = 'Password1!';
$v = new VanguardClient($test);
$v->loadAuskey($auskey_path, $auskey_password);
try {
return $v->requestToken($endpoint);
} catch (SoapFault $e) {
echo "Error1:";
echo $e;
}
}
//get token from Vanguard
$token = get_sts($endpoint);
//create soap client
try{
$wsdl = 'https://3pt.portal.usi.gov.au/service/V2/UsiService.wsdl';
$client = new SoapClient($wsdl,
array(
'trace' =>1,
//'soap_version' => SOAP_1_2,
'keep_alive' => false,
//'connection_timeout' => 500000,
'cache_wsdl' => WSDL_CACHE_NONE
)
);
} catch (SoapFault $e) {
echo "SoapClient Error:<br />";
var_dump($e);
}
try {
$result=$client->__setSoapHeaders( $token );
} catch (SoapFault $e) {
echo "__setSoapHeaders:";
var_dump($e);
}
$data = array(
'OrgCode' => '970003',
'USI' => 'U6Q8JN6UD9',
'FirstName' => 'Myrna',
'FamilyName' => 'Macknight',
'DateOfBirth' => '1971-04-19'
);
try{
$response=$client->__soapCall('VerifyUSI',$data);
} catch (SoapFault $e) {
echo "__soapCall Error:<br />";
echo $e;
}
var_dump($response);
The result on the browser that I am seeing is this:
SoapFault exception: [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. in /var/www/html/usitest/example1.php:73 Stack trace: #0 [internal function]: SoapClient->__doRequest('__soapCall('VerifyUSI', Array) #2 {main}NULL
Thanks in advance guys!!!
Your content type is probably caused by the SOAP version.
Try setting the SOAP version to 1.2:
'soap_version' => SOAP_1_2
See SoapClient connection to SoapServer
However, I think there are other issues in your code - particularly with the Vanguard token.
We managed to solve this however it took many classes, templates, external packages and months of work to solve and is not something we can put up online. However there are some things I'd suggest you do to solve it in your situation.
This does not work like a normal SOAP service. Use XML templates for all steps of the various process (Vanguard, USI, components of these sections etc).
Reverse engineer the .Net example code, we had major issues with the Java code.
We made major headway by using a proxy and capturing the content sent and received.
Unless you are using composer to manage your security dependencies you're going to have a bad time, even with composer it was a pain.
There are about 10 sections to do with security that have to be reverse engineered, don't forget to canonicalise the content to get the encryption correct.
Use Guzzle for the requests, it's easier
Most of the stuff in the PHP example is wrong, or at least impossible to follow and debug to fix. At the end we couldn't see a way that it would work.
Expect to spend at least a couple of weeks on it and you need to know a lot about security, hashing and ssl certificates.
Sorry I can't give you a full working solution but knowing these steps above would have definitely helped us and so I hope they'll help you.
For reasons which are not important, sometimes our backend server will fail.
For both refreshing the page and requesting support, it would be useful for our application NOT to change the URL in the browser's address bar.
I've tried the following WITHOUT success:
const handleError = reason => {
let windowLocation = window.location.href
this.replaceRoute('error', new Ember.Error(reason));
window.history.pushState(page, title, windowLocation);
};
Any suggestions which might work better?
The correct answer was:
const handleError = () => this.intermediateTransitionTo('error');
In my javascript I have a click event that triggers an ajax call to the php page where I send my notification from. I chose to do it this way because the documentation advises against using your app secret in any client side code, and the notifications parameters requires an access token that you can only get using the app secret.
The problem I'm having is that even though I'm logged in, $facebook->getUser() is returning 0 in php, so the api call I make afterwards to send the notification wont work. My user is already logged in via the client side code, so how do I get the message to the php that they're logged in so the notification can be sent.
//JS
$.ajax({
url : "http://xxxxxo/bn/notification.php",
type : 'POST',
data: {notify: notify },
success : function (result) {
console.log(result);
},
error : function () {
alert("error sending notification");
}
});//closes ajax
//PHP
<?php
require_once(dirname(__FILE__).'/php-sdk/facebook.php') ;
$APPLICATION_ID = '1402xxxxx7';
$APPLICATION_SECRET = 'ce71d6bbxxxxx5f55a';
$fb_app_url = "http://apps.facebook.com/myAPP";
$config = array();
$config['appId'] = $APP_ID;
$config['secret'] = $APP_SECRET;
$config['cookie'] = true;
$facebook = new Facebook($config) or die('Error is here!');
$facebook = new Facebook(array(
'appId' => $APP_ID,
'secret' => $APP_SECRET,
'fileUpload' => true
));
$notify = $_REQUEST['notify'];
$userid = $facebook->getUser();
/*IF WE HAVE A LOGGED IN USER AND THE 'NOTIFY' REQUEST VALUE, THEN SEND THE NOTIFICATION.
BUT MY USER ID IS 0. HOW DO I GET PHP TO RECOGNIZE ME AS LOGGED IN WITHOUT HAVING TO FORCE MY USER TO LOG IN VIA PHP AFTER THEY'VE ALREADY LOGGED IN CLIENT SIDE?*/
if($userid && $notify){
$token_url ="https://graph.facebook.com/oauth/access_token?" .
"client_id=" . $APP_ID .
"&client_secret=" . $APP_SECRET .
"&grant_type=client_credentials";
$app_token = file_get_contents($token_url);
$app_token = str_replace("access_token=", "", $app_token);
$data = array(
'href'=> 'https://apps.facebook.com/thebringernetwork/',
'access_token'=> $app_token,
'template'=> 'test'
);
$sendnotification = $facebook->api('/1622649653/notifications', 'post', $data);
}else{
//handle error
}
?>
The first thing I noticed is that you define your app id as $APPLICATION_ID but use it as $APP_ID (and the same goes for your app secret). But since you didn't mention any errors and $facebook->getUser(); executes I'm guessing this is just a bad copy-paste.
Now for the sake of answering this question I'm going to presume that you are using the latest versions of both JS and PHP SDKs. These use oauth 2.0 and change the way you pass the login information from JS to PHP.
According to Facebook Developer Blog removing $config['cookie'] = true; and setting oauth to true in your JS configuration should work. Just make sure to refresh the site after the login.
The solution I've found in my own project is to disable cookies altogether and simply pass the access token to my PHP script.
In your JS call your PHP script like this (make sure to call this after the JS login!):
$.ajax({
url : "http://xxxxxo/bn/notification.php",
type : 'POST',
data: {
notify: notify,
token: FB.getAuthResponse()['accessToken'] // add your access token
},
success : function (result) {
console.log(result);
},
error : function () {
alert("error sending notification");
}
});
And in your PHP script add this after creating the FB object.
$facebook->setAccessToken($_POST['token']); // set the users access token
Doing things this way will also get rid of any need to refresh the website after the login.
Yes, this is a common problem when using the PHP SDK in combination with AJAX:
When you make an AJAX request, the PHP SDK deletes the cookies where the authorization information are stored, and then the next call to getUser will just return 0, because this method tries to find the current user id in those cookies – apparently there is something in the OAuth 2.0 spec that demands this behavior to prevent some sort of click-jacking attack.
But the info will still be stored in the session, so you can read the user id (and the user access token, should you need it) from there:
$user_id = $_SESSION['fb_YourAppIdHere_user_id'];
$user_access_token = $_SESSION['fb_YourAppIdHere_access_token'];
Replace YourAppIdHere with your app id (so it becomes fb_1234567890_user_id resp. fb_1234567890_access_token) to get the correct names of those session keys.
I've developed REST services. I can test the GET methods by the browser, or by a Client Application. But those who have PUT methods I don't know how to consume them by the browser...
For example, I have this method that turns a lamp on, after I insert the userId:
#PUT
#Path("/lampon")
#Produces({"application/json", "text/plain"})
#Consumes("multipart/form-data")
public boolean turnOnLamp(#FormParam("userId") String userId) throws Exception
{
boolean response = new LampManager().turnOnLamp(userId);
return response;
}
In my client application I do this, and it works:
String webPage = "http://localhost:8080/BliveServices/webresources/services.actuators/lampon";
URL urlToRequest = new URL(webPage);
//Authentication
urlConnection = (HttpURLConnection) urlToRequest.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("PUT");
urlConnection.setRequestProperty("Authorization", basicAuth);
urlConnection.setRequestProperty("Content-type", "multipart/form-data");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userId", "2"));
(...)
But how can I send the userId by the browser?
Another thing, I get this message when I build my project:
SEVERE: Resource methods utilizing #FormParam and consuming "multipart/form-data" are no longer supported. See #FormDataParam.
Thanks
If you want to test the REST-Webservice with your Browser you must install an plugin.
If you use Google Chrome you can install REST Console I also use these plugin to test my Webservice.
https://chrome.google.com/webstore/detail/rest-console/cokgbflfommojglbmbpenpphppikmonn
For Firefox install these REST-Client
https://addons.mozilla.org/en-us/firefox/addon/restclient/
REST CLient is also available for Safari
http://restclient.net/
For Opera you can check out the Simple REST-Client
https://addons.opera.com/en/extensions/details/simple-rest-client/
For your second Question
please try as Consumes value 'application/x-www-form-urlencoded'
To issue a put-request from a browser you could use jQuery's jQuery.ajax(). (http://api.jquery.com/jQuery.ajax/)
For example:
$.ajax({
url: "test-url",
type: "PUT",
data: {userid: 1}
})
Would send a put-request to test-url with the specified data.
I'm aware you can force update a page's cache by entering the URL on Facebook's debugger tool while been logged in as admin for that app/page:
https://developers.facebook.com/tools/debug
But what I need is a way to automatically call an API endpoint or something from our internal app whenever somebody from our Sales department updates the main image of one of our pages. It is not an option to ask thousands of sales people to login as an admin and manually update a page's cache whenever they update one of our item's description or image.
We can't afford to wait 24 hours for Facebook to update its cache because we're getting daily complaints from our clients whenever they don't see a change showing up as soon as we change it on our side.
Page metadata isn't the sort of thing that should change very often, but you can manually clear the cache by going to Facebook's Debug Tool and entering the URL you want to scrape
There's also an API for doing this, which works for any OG object:
curl -X POST \
-F "id={object-url OR object-id}" \
-F "scrape=true" \
-F "access_token={your access token}" \
"https://graph.facebook.com"
An access_token is now required. This can be an app or page access_token; no user authentication is required.
If you'd like to do this in PHP in a with-out waiting for a reply, the following function will do this:
//Provide a URL in $url to empty the OG cache
function clear_open_graph_cache($url, $token) {
$vars = array('id' => $url, 'scrape' => 'true', 'access_token' => $token);
$body = http_build_query($vars);
$fp = fsockopen('ssl://graph.facebook.com', 443);
fwrite($fp, "POST / HTTP/1.1\r\n");
fwrite($fp, "Host: graph.facebook.com\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($body)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");
fwrite($fp, $body);
fclose($fp);
}
If you're using the javascript sdk, the version of this you'd want to use is
FB.api('https://graph.facebook.com/', 'post', {
id: [your-updated-or-new-link],
scrape: true
}, function(response) {
//console.log('rescrape!',response);
});
I happen to like promises, so an alternate version using jQuery Deferreds might be
function scrapeLink(url){
var masterdfd = $.Deferred();
FB.api('https://graph.facebook.com/', 'post', {
id: [your-updated-or-new-link],
scrape: true
}, function(response) {
if(!response || response.error){
masterdfd.reject(response);
}else{
masterdfd.resolve(response);
}
});
return masterdfd;
}
then:
scrapeLink([SOME-URL]).done(function(){
//now the link should be scraped/rescraped and ready to use
});
Note that the scraper can take varying amounts of time to complete, so no guarantees that it will be quick. Nor do I know what Facebook thinks about repeated or automated usages of this method, so it probably pays to be judicious and conservative about using it.
This is a simple ajax implementation. Put this on any page you want facebook to scrape immediately;
var url= "your url here";
$.ajax({
type: 'POST',
url: 'https://graph.facebook.com?id='+url+'&scrape=true',
success: function(data){
console.log(data);
}
});
An alternative solution from within a Drupal node update using curl could be something like this :
<?php
function your_module_node_postsave($node) {
if($node->type == 'your_type') {
$url = url('node/'.$node->nid,array('absolute' => TRUE));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v1.0/?id='. urlencode($url). '&scrape=true');
$auth_header = 'Oauth yOUR-ACCESS-TOKEn';
curl_setopt($ch, CURLOPT_HTTPHEADER, array($auth_header));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$r = curl_exec($ch);
curl_close ($ch);
}
}
Notice the hook_node_postsave() implementation which is not standard Drupal core supported.
I had to use www.drupal.org/project/hook_post_action in order to get this facebook scrape pickup last made changes to the node, since hook_node_update() is not triggered after databases have been updated.
Facebook requires now the access token in order to get this done.
Guidelines to acquire a token can be found here :
https://smashballoon.com/custom-facebook-feed/access-token/
I'm the author of Facebook Object Debugger CLI, a command-line interface written in PHP, aim to refresh the Facebook cache for a single URL or a bunch of URLS using as input a text file. The package is also available on Packagist and can be installed using Composer.
There are changes in Graph API v2.10:
When making a GET request against a URL we haven't scraped before, we will also omit the og_object field. To trigger a scrape and populate the og_object, issue a POST /{url}?scrape=true. Once scraped, the og_object will remain cached and returned on all future read requests.
We will require an access token for these requests in all versions of the Graph API beginning October 16, 2017.
Source: Introducing Graph API v2.10
So now we should use POST-method for scraping:
POST /{url}?scrape=true
Not
A solution with the PHP Facebook SDK:
<?php
try {
$params = [
'id' => 'https://www.mysitetoscrape.com/page',
'scrape' => 'true',
];
$response = $fb->post('/', $params);
print_r($response);
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
?>
Here's my Ruby solution using Koala gem and Facebook API v2.9
api = Koala::Facebook::API.new(access_token)
response = api.put_object(nil, nil, {scrape: true, id: "url-of-page-to-scrape"})
response should be a hash of attributes retrieved from the og: meta tags on the page which was scraped.
I was facing this same problem.
There is a simple way to clear cache.
http://developers.facebook.com/tools/debug
Enter the URL following by fbrefresh=CAN_BE_ANYTHING
Examples: http://www.example.com?fbrefresh=CAN_BE_ANYTHING