Understanding JSON and Dropbox API pulls - json

so I'm jumping in head first here and trying to learn something new... I have an Angular Function that will pull in all the contents of a folder using the Dropbox API and list it into a JSON file (and later display it on a webpage.)
what I want to do next is to get a share link for all those files using the create_shared_link_with_settings API and put them into a JSON file for all the files in the folder.
here's what I have for the first part. If you could help me out or point me in the right direction on the proper way to tackle this.
var app = angular.module("content-review", []);
app.controller("Content-folders-4K", function ($scope, $http) {
$http({
url: 'https://api.dropboxapi.com/2/files/list_folder',
method: 'POST',
processData: false,
contentType: 'application/json',
data: ({
"path": "/MIG/Projects/Hippo content Watermark/HD/011_Blue"
}),
headers: {
"Authorization": "Bearer APIKEYTHATIMNOTSHARING",
},
})
.then(function (response) {
$scope.UHD = response.data.entries;
$scope.json = angular.toJson(response.data, true);
console.log($scope.json)
});
});

To create a shared link for a file or folder in Dropbox, the /2/sharing/create_shared_link_with_settings endpoint you mentioned is the correct endpoint to use. You can find the documentation here. There's also an example of calling it in curl that you can translate for use with your own HTTPS client.
You specify which item you want the link for using the path parameter similar to how you did for the /2/files/list_folder call you showed. You can get the path value from the path_lower value for each "entry" in the /2/files/list_folder responses.
Note that if a shared link already exists for the item, you'll get a shared_link_already_exists error. The error will include the existing shared link only if the settings for the existing link match the settings you requested. Otherwise, you'll need to call /2/sharing/list_shared_links to get the existing link.
Either way, you can parse the result of the successful call to get the SharedLinkMetadata.url value for use in your app.

Related

google app script: same api with different file

I'm a beginner in google app script. The two function in separate file id declared and it works on google debug well.
Therefore, I deploy to web application but when I connect the API and it only returns my first function (onGet).
Is possible like node or php that could use a different name to call another API? i.e. API/A_WEB_SERVICE . API/B_WEB_SERVICE.
Or I need to add a new project to handle my situation?
my.js
$.ajax({
type: "get",
url: "https://script.google.com/macros/s/my_key/exec",
data: data,
dataType: "JSON",
success: function (response) {
console.log(response);
alert('cool works.');
}
});
gs
doGet(){} // in my first file.
doDebug(){} // in my second file.

Shopify - Loading data from an External REST API in a Liquid template

I need to load some data (A WordPress Menu) from an external REST API into my Shopify template. I'm assuming I need to use an App proxy to do this. I've looked through the documentation but I'm a little confused as to how to go about this.
Can anyone point me in the right direction?
I often use jquery with an ajax call to an api end point that either
sends me back formatted html
send me back json data that I parse and form the html via javascript.
jQuery(window).load(function(){
data = {};
jQuery.ajax({
type: 'GET',
url: 'https://yourapp.herokuapp.com/yourendpoint.json',
data: data,
dataType: 'json',
success: function(data) {
console.log(data);
$.each( data, function(i, item) {
console.log(item);
// do something with your data here
});
}
});
});

How to update json file in AngularJS

I want to update the existing json file in AngularJS
JSON file:
{
"text1": "Click here to edit!",
"text2": "Click here to edit!",
"text3": "Click here to edit!",
"text4": "Click here to edit!"
}
I want to update this JSON file as:
text1: "Abc"
and save this changes in JSON file
You can not update a json file without using a server-side language like PHP or python. Basically it is security compliance. For more understanding kindly go through
https://docs.angularjs.org/guide/security
https://docs.angularjs.org/api/ng/directive/ngCsp
and
https://developer.mozilla.org/en-US/docs/Web/Security/CSP
Imagine you have getjson.php and savejson.php in the server which work exactly as their names suggest.
Now use $http service of Angular to retrieve your json from the server.
$http.get("getjson.php").then(function(response){
$scope.myJsonObject = response.data;
//Your json becomes JS object here. Change it the way you want
$scope.myJsonObject.text1 = "Abc";
});
Use $http service again to send your json back to the server.
$http({
method: "post",
url: "savejson.php",
data: $scope.myJsonObject,
headers: { 'Content-Type': 'application/json; charset=utf-8' }
});
This is the basic. Please note that you need to do your php part to save/load your json file. Also you should handle errors of the $http service.
Please see how $http service and promises work.

Chrome API responseHeaders

Based on this documentation: https://developer.chrome.com/extensions/webRequest.html#event-onHeadersReceived
I tried to display the response via the console like:
console.log(info.responseHeaders);
But its returning undefined.
But this works though:
console.log("Type: " + info.type);
Please help, I really need to get the responseHeaders data.
You have to request the response headers like this:
chrome.webRequest.onHeadersReceived.addListener(function(details){
console.log(details.responseHeaders);
},
{urls: ["http://*/*"]},["responseHeaders"]);
An example of use. This is one instance of how I use the webRequest api in my extension. (Only showing partial incomplete code)
I need to indirectly access some server data and I do that by making use of a 302 redirect page. I send a Head request to the desired url like this:
$.ajax({
url: url,
type: "HEAD"
success: function(data,status,jqXHR){
//If this was not a HEAD request, `data` would contain the response
//But in my case all I need are the headers so `data` is empty
comparePosts(jqXHR.getResponseHeader('redirUrl')); //where I handle the data
}
});
And then I silently kill the redirect while scraping the location header for my own uses using the webRequest api:
chrome.webRequest.onHeadersReceived.addListener(function(details){
if(details.method == "HEAD"){
var redirUrl;
details.responseHeaders.forEach(function(v,i,a){
if(v.name == "Location"){
redirUrl = v.value;
details.responseHeaders.splice(i,1);
}
});
details.responseHeaders.push({name:"redirUrl",value:redirUrl});
return {responseHeaders:details.responseHeaders}; //I kill the redirect
}
},
{urls: ["http://*/*"]},["responseHeaders","blocking"]);
I actually handle the data inside the onHeadersReceived listener, but this way shows where the response data would be.

Consuming broadbandmap.gov json service errors

I'm trying to consume the json services from broadbandmap.gov so that I can display broadband providers and their speeds in an area. Here is a sample url:
http://www.broadbandmap.gov/internet-service-providers/70508/lat=30.1471824/long=-92.033638/%3Ejson
I'm using jquery to consume the service, however it's giving me an invalid label error in firebug:
var url = "http://www.broadbandmap.gov/internet-service-providers/70508/lat=30.1471824/long=-92.033638/%3Ejson";
//var url = "http://www.broadbandmap.gov/broadbandmap/broadband/fall2010/wireline?latitude=" + lat + "&longitude=" + long + "&format=json";
$.ajax({
url: url,
dataType: 'json',
type: 'POST',
contentType: "application/json; charset=utf-8",
success: function (result) {
console.debug("in success");
console.debug(result);
//success, execute callback function.
},
error: function (result) {
console.debug("in error");
console.debug(result);
}
});
The strange thing is that under the Invalid Label error in Firebug it actually has the correct response:
{"status":"OK","responseTime":7,"messa...//www.cscic.state.ny.us/broadband/"}}}
I have tried setting the dataType to json, jsonp, and other types as well to no avail. I have also tried GET instead of POST but that didn't work either. Does anyone know what I'm missing?
That error is occurring because the service is returning JSON and not JSONP. Your browser is not going to let you process straight JSON from a cross-domain source.
In order to make the service return JSONP you have to use a specially formatted URL. If you go to the search results page without the "/>json" modifier (link) you'll see a link on the page that reads "API Call". If you hover over this link it will give you the correct URL to use for the wireless/wired API call. Use one of those URL's in your ajax call with a JSONP return type & callback and you should be all set.
I created an updated fiddle at http://jsfiddle.net/qsY7h/1/.
This is a cross-domain request so you should use JSONP datatype - the API supports this return type. The URL you provided in your example didn't return anything for me so I checked out Broadbandmap's Developer Docs and found an alternate call. Please find an example at http://jsfiddle.net/szCAF/.
The most important point to note is "callback=?" in the URL. jQuery uses this to tell the API what function name to wrap around the output (this is all done transparently by jQuery).