HttpRequest, accessing third party web service

Just starting out? Need help? Post your questions and find answers here.
_Marc_
Posts: 23
Joined: Wed Mar 30, 2016 7:49 am

HttpRequest, accessing third party web service

Post by _Marc_ »

The documentation mentions that I can use HttpRequest only on local domain addresses? Is this saying that I cannot access web services on other domains? I hope not. An example would be nice.
Last edited by _Marc_ on Sat Apr 02, 2016 11:22 am, edited 1 time in total.
User avatar
SparrowhawkMMU
Posts: 291
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: HttpRequest, accessing web service

Post by SparrowhawkMMU »

This is a security feature baked into web protocols I believe.

You need the remote page or API to allow access from your own domain. Or all domains using an asterisk as a wild card:

Code: Select all

Access-Control-Allow-Origin: *
Search the forums for this, there are several threads about it. Which go into detail.
_Marc_
Posts: 23
Joined: Wed Mar 30, 2016 7:49 am

Re: HttpRequest, accessing web service

Post by _Marc_ »

SparrowhawkMMU wrote:This is a security feature baked into web protocols I believe.

You need the remote page or API to allow access from your own domain. Or all domains using an asterisk as a wild card:

Code: Select all

Access-Control-Allow-Origin: *
Search the forums for this, there are several threads about it. Which go into detail.
Believe me, I searched. Like this one:

http://forums.spiderbasic.com/viewtopic ... igin#p1739

Problem is that most of the web services I like to access are third party open web services like:

https://developer.wordpress.com/docs/ap ... ite/users/
http://del.icio.us/api/:username/bookmarks/

I cannot control them (eg. set the response header), but they're known for working from other websites/domains. Also I know that these are working from my own domain/ASP.NET web service, therefore must allow it.

The documentation of PB says: "Due to security constraints, it is only possible to execute a request on the same domain.".

If you say it can work then it would be great if you could provide me with a working example accessing a third party REST service (not on the same domain or local).
bbanelli
Posts: 107
Joined: Mon Jul 13, 2015 7:40 am

Re: HttpRequest, accessing web service

Post by bbanelli »

SparrowhawkMMU wrote:If you say it can work then it would be great if you could provide me with a working example accessing a third party REST service (not on the same domain or local).
I am really far from expert in this, so kindly take my writing with a grain of salt.

As far as I have understood, your logic is faulty. You can call a local script (PHP or CGI written in PureBasic, for example), and access REST API from desired site. That is how I do it and it work flawlessly.

How do you expect to protect your credentials for REST API in JavaScript? http://mypasskey@example.com/api/ It would be completely visible to client, which is something you don't want, right?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
http://www.pci-z.com/
_Marc_
Posts: 23
Joined: Wed Mar 30, 2016 7:49 am

Re: HttpRequest, accessing web service

Post by _Marc_ »

bbanelli wrote:
SparrowhawkMMU wrote:If you say it can work then it would be great if you could provide me with a working example accessing a third party REST service (not on the same domain or local).
I am really far from expert in this, so kindly take my writing with a grain of salt.

As far as I have understood, your logic is faulty. You can call a local script (PHP or CGI written in PureBasic, for example), and access REST API from desired site. That is how I do it and it work flawlessly.

How do you expect to protect your credentials for REST API in JavaScript? http://mypasskey@example.com/api/ It would be completely visible to client, which is something you don't want, right?
In my case I would use ASP.NET but I know what you're getting at. This would mean I cannot directly connect to the third party web service but use my own ASP call as intermediare.

NB: I'm using a user input of credentials combined with 2FA and hashing. So yes, I know I cannot put the credentials as constants in my code ;).
_Marc_
Posts: 23
Joined: Wed Mar 30, 2016 7:49 am

Re: HttpRequest, accessing web service

Post by _Marc_ »

SparrowhawkMMU wrote:This is a security feature baked into web protocols I believe.

You need the remote page or API to allow access from your own domain. Or all domains using an asterisk as a wild card:

Code: Select all

Access-Control-Allow-Origin: *
Search the forums for this, there are several threads about it. Which go into detail.
I've added the header via web.config as documented here:
http://enable-cors.org/server_iis7.html

Now it's working for my own web service. For now that's the most important thing.

BTW: What are the disadvantages of allowing this? We give external developers access to it for native applications like OSX and Windows. That was already working before this fix. Why is access from a external website not allowed by standard?
User avatar
SparrowhawkMMU
Posts: 291
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: HttpRequest, accessing third party web service

Post by SparrowhawkMMU »

The cons are reduced security, so you may want to specify specific calling domains if possible, rather than allowing from all. Of course this may not b possible.

Good article here: https://developer.mozilla.org/en-US/doc ... ntrol_CORS
_Marc_
Posts: 23
Joined: Wed Mar 30, 2016 7:49 am

Re: HttpRequest, accessing third party web service

Post by _Marc_ »

SparrowhawkMMU wrote:The cons are reduced security, so you may want to specify specific calling domains if possible, rather than allowing from all. Of course this may not b possible.

Good article here: https://developer.mozilla.org/en-US/doc ... ntrol_CORS
The final/live SB project will be hosted on the same domain, so eventually I can remove that header (if I understand it all correctly). I only need to be able to test from my developer Mac with PB's IDE. Is it possible to add something that will allow me to test the web service from my PB IDE; eg. by adding my IP address?
Fred
Site Admin
Posts: 1820
Joined: Mon Feb 24, 2014 10:51 am

Re: HttpRequest, accessing third party web service

Post by Fred »

You can fake it with the following step on your dev computer:

1) edit your 'hosts' file (C:\Windows\System32\drivers\etc\hosts) and add a line like:

127.0.0.1 spider.test

2) Add to your server:

Access-Control-Allow-Origin: spider.test

3) Set in Spiderbasic IDE -> Compiler Options -> Compile/Run -> Web Server Address:

spider.test:8080

It should work
Post Reply