trms / cablecast-api-examples

A place to showcase code snippets using the Cablecast API
MIT License
1 stars 0 forks source link

Basic HTTP Authentication Example? #3

Open MCDELTAT opened 7 years ago

MCDELTAT commented 7 years ago

I'm currently working on adding some cool Cablecast integrations features into our new website and I'm having trouble understanding how to send the Authentication header with my request. I'd appreciate it if I can just get a super quick example of sending that header and then carrying out an innocent action like perhaps "POST v1/digitalfiles/{id}/reindex" since any valid file will just get reindexed and be valid again.

Once that's out of the way, I'll be sure to merge my code here. We have some cool examples in the works. Thanks.

EDIT: I made a few new attempts using modified stuff from SO (let's not lie here) and reading the jQuery documentation. The code below (with username, pass, and IP obviously changed to correct values) gives me the following error: "request header field is not allowed by access-control-allow-headers"

Ideas?

``var username = "plaintextUser"; var password = "plaintextPass";

function make_base_auth(user, password) { var tok = user + ':' + password; var hash = btoa(tok); return "Basic " + hash; } $.ajax ({ method: "POST", //use for jquery above 1.9.0 else type: 'POST' url: "http://xxx.xxx.xxx.xxx/cablecastapi/v1/digitalfiles/867/reindex", dataType: 'json', async: false, beforeSend: function (xhr){ xhr.setRequestHeader('Authorization', make_base_auth(username, password)); }, success: function (){ alert('The file is being reindexed!'); } });

console.log("Testing connection.");

MCDELTAT commented 7 years ago

Cool this has been resolved. More cool stuff coming soon.

For anyone looking to get started with any of the authenticated API requests, here's some tips.

1) Make sure to enable the desired CORS parameters as indicated in indicated in that section of the documentation. The web.config file is found in C:\TRMS\Web\CablecastAPI\web.config As of this writing, the lines you're looking for are around lines 100.

2) Realize the {id} that is used in the API calls are different from the ShowID. In my case above, show 100 was id 867. I'm guessing the best way is figure this out is to call "Get v1/digitalFiles/" with a query specific enough to only return 1 value.

3) Otherwise you can just try my code out above. All you have to do is create an html document with jquery in the header and then that code following. Of course changing 'username' and 'password' to your credentials and changing the url. I'll post more detailed example later.

raytiley commented 7 years ago

@MCDELTAT hey... sorry, just saw this now. Glad you figured it out. Look forward to seeing your examples :)

nwatv commented 7 years ago

Super helpful, @MCDELTAT. What might a similar snippet look like that both authenticates and posts a new show record on a given channel?

MCDELTAT commented 7 years ago

@nwatv I'll try and write one out for you tomorrow. Be aware though, this is obviously not an optimal or secure solution, as it would expose your Cablecast credentials to the public. I was just highlighting the code above so that if desired, people could play around with the API and make some cool stuff.

We are currently developing our website with a MERN stack so I can hide these credentials away on the server side. One of the cooler features I'm about to release is the ability for our Public Access members to remotely upload their shows, scan the file for viruses, and then transfer it to our server for playback, with a brand new show record to match it.

nwatv commented 7 years ago

@MCDELTAT That'd be incredible. Masking the credentials is easy enough, but the framework is hugely appreciated. The member uploads system sounds great--do you have a central space to follow your public access-related projects?