Parse web application credentials and sending push to different credentials - javascript

I'm creating a web application and use Parse to manage users, I use my Parse applicationID and JavaScriptId.
Parse.initialize("my-app-id", "my-js-id");
Parse.User.logIn(username, password,...
When the user login to the site, he/she enter their Parse app-id and js-Id that belong to their mobile app that they want to push a notification to. And the website send notification to them:
Parse.initialize("their-android-app-id", "their-android-js-id");
Parse.Push.send({ where: new Parse.Query(Parse.Installation), data: ...
I'm not clear when to call initialize and how I distinguish between my javascript app and the android/iOS app that I want to send to.

As long as i know it isn't possible to use the same api with two diffrent api-keys. I think it would be the most easiest way to use the normal JavaScript-API for application A, but the REST-API for application B, since you only need to send push notifications for the application B.
In JQuery this could look for example like this:
$.ajax({
url: 'https://api.parse.com/1/push',
type: 'post',
data: {
where : {
...
},
data : {
...
}
},
headers: {
"X-Parse-Application-Id": "their-app-id",
"X-Parse-REST-API-Key": "their-rest-api-key",
},
contentType: 'application/json',
dataType: 'json',
success: function (data) {
//data: response data from the Parse.com REST-API
}
});
A quide from Parse about how to use push notifications through the REST-API is available here.

Related

Authenticate Javascript Program for Gcloud AutoML Vision API

I am currently working on a project whereby I have used gcloud automl to train an image classifier. I have got it working fine and it is able to handle my requests using access-tokens. However, my issue lies in that access-tokens only last for an hour. I would like to be able to create a method in my client-side javascript or another way to authenticate such that it is indefinite and I do not need to manually generate new tokens.
I have looked through the samples and docs on gcloud and found a sample that describes a possibility with the People API but I am not sure how to translate this to the Vision API.
Currently I am sending a request like the following:
$.ajax({
url: "https://automl.googleapis.com/v1beta1/projects/(project-id)/locations/us-central1/models/(model-id):predict",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + "access-token");
},
type: 'POST',
dataType: 'json',
contentType: 'application/json',
processData: false,
data: objString,
success: function (data) {
alert(JSON.stringify(data));
},
error: function(){
alert("Cannot get data");
}
});
If there is a way to alter this method (or write a new one) to make a permanent token or another authentication form that is indefinite, then that would be much appreciated.
You should use an environment variable along with a service account. Depending on your OS, you could use it until you turn off your machine, or set it as a permanent environment variable.

How to get list of playlists in channel on youtube-api without OAuth2?

Can I get playlists from a channel without using authentication (OAuth2)?
I found this: https://developers.google.com/youtube/v3/docs/channels/list#usage. But it seem I have to use OAuth2, Which I think is redundant because it's not a private information.
I need this in JavaScript.
you can use jQuery to get playlists of a particular channel, you need an API key.
$.ajax({
type: "GET",
url:'https://content.googleapis.com/youtube/v3/playlists',
dataType: 'jsonp',
data: {'channelId': 'CHANNEL_ID',
'maxResults': '25',
'part': 'snippet,contentDetails',
'key': "YOUR_API_KEY"},
success: function(response){
// handle response
}
});

How to call RESTful from another server by JavaScript

I have a server A running a web application (in ASP.NET MVC 4), using JavaScript to call a REST API residing on server B.
JavaScript in Server A:
$.ajax({
type: "POST",
url: "http://serverB/api/Employee/Manpower",
contentType: "application/json; charset=utf-8",
data: convertTojsonWithData(REQHEAD(), EMPDETAIL()),
success: function (Result) {
//alert(data);
$("#txtOption").val(Result);
alert($("#txtOption").val());
}
});
When I run my project, I received nothing from the REST API on Server B. And when I changed my url to api/Employee/Manpower, the API endpoint becomes http://localhost:1111/api/Employee/Manpower.
Please guide me!
What is Server B's ip address ?
Try with Ip Address of server B.
for example call http://192.168.5.1:8080/api/Employee/Manpower

Submit a form to DJANGO from Phonegap Application using JAVAScript

I have written a Phonegap application and I would like to know how can I submit a form to Django using Javascript or Jquery. I have already implemented a JSON API using Tastypie.
Can anybody provide me with some ideas, or any guidance on how to implement such a function?
You just have to do ajax call to tastypie api url in the below format.
$.ajax({
type: 'POST',
url: site_url+'api/v2/user/login/',
data: '{"username": "developer", "password":"imagine2"}',
processData: false,
crossDomain: true,
success: function (response, textStatus, xhr) {
console.log('jquery worked');
//results=JSON.stringify(response);
setCookie( "sessionid", response.sessionid);
setCookie( "app_version", response.app_version);
window.location.href = 'home/';
$('#overlay').hide();
},
error: function (res) {
msg = (res.responseText) ? res.responseText : res.statusText;
console.log(msg+",res.status="+res.status);
alert(msg);
},
dataType: "json",
contentType: "application/json; charset=utf-8"
});
Also you need to enable cross domain.
There are multiple ways to do this. Since you are using an API with tastypie I would suggest not submitting the form itself, but extracting the form data before submitting the form with JavaScript. On submit you pass the data you want to submit in the request body to your API endpoint.
As an example you could have an api endpoint for blog entries, lets say its http://example.com/api/v1/entry. Considering your API allows cross-domain requests (e.g. via CORS) you could do something simple as this to "submit" your form (e.g. creating a new entry):
$('#myForm #mySubmitButton').click(function() {
$.post('http://example.com/api/v1/entry', $('#myForm').serialize(), function(data) {
// deal with the response data
}
});
I am not sure tastypie supports this kind of data structure. A little longer version of this could be to do something like this (here I am sure tastypie does support it), which submits the data as a JSON:
$('#myForm #mySubmitButton').click(function() {
var data = {'title': $('#myForm #titleInput').val(), 'description': $('#myForm #descriptionInput').val()};
$.post('http://example.com/api/v1/entry', data, function(data) {
// deal with the response data
}
});
This examples do not deal with form validation, error handling etc.. You could have look into tastypie validation for more details and potentially also add frontend specific validation with HTML5. I would also suggest you to use a more sophisticated JavaScript framework like AngularJS for single page apps like a PhoneGap app. JQuery will bloat your app soon, when it grows.

How do I do an ajax call to a database using Javascript, a URL and a key

I have been given a url of type xxxx.xxxxx.com as well as a key of type FGHyehgvc787vbhj
in order to gain read-only access to an sql database and retrieve data from it using javascript.
I have no prior experience with databases and maybe my question will sound completely stupid but I was wondering how can I combine the above information in order to get access to the database (e.g. do an ajax call and retrieve data from it..)
I'm familiar with doing ajax calls to a webpage and getting data from it using jQuery, as in :
$.ajax(/*url of website*/, function (data)
{
var dataRetrieved = $(data);
// do something with the data retrieved...
});
so I was wondering whether there is something equivalent to the above when it comes to making an ajax call to a database, using however a key.
Thank you for any help, and please delete this post if you find it completely pointless and excuse me in advance for that by the way.
It is usually very bad design to allow client side code to interact with your database in any way. This can be a huge security issue. Generally you will want your server side code to do this (e.g PHP, node, etc.). You would send a request to your server with client side code, and the server side code would do the actual work of updating the database.
you can create a wcf service and call that service through ajax, that wont be huge security issue.
try this
$.ajax({
cache: false,
type: "GET",
async: false,
data: {},
url: http:xxxxxxxxxxxx.svc/webBinding/Result?metaTag=" + meta,
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain: true,
success:function(result){},
error: function(){alert(err);}
});
Use this
$.ajax({
url: 'path/to/server-side/script.php', /*url*/
data: '', /* post data e.g name=christian&hobbie=loving */
type: '', /* POST|GET */
complete: function(d) {
var data= d.responseTXT;
/* Here you can use the data as you like */
$('#elementid').html(data);
}
});
Hope this helps...

Categories

Resources