Python: How to sign a request? - python

My question is so superficial, it will probably be deleted right away. But I just can't get this to work. I'm sorry.
I am trying to work with an api (https://api.immobilienscout24.de/). But I can't get anything to work.
There a two ways for authentication. I figure that for my needs (searching for and viewing listings) the two-legged OAuth should be sufficient. The way I undestand it, that way I simply need to give my username and usersecret for each request.
But I can't just give those in the request-headers or anything. I somehow have to sign the request with my credentials. But I don't understand what that means and how to do that.
There is even a postman-collection, but it can't be not faulty. The request doesn't use any kind of authentication. It's just a request without any headers and parameters which of course does not work.
All the github projects, that use python and work with this website are just webcrawlers. Thus I couldn't find anything helpful there either.

Related

Authentication with POST and PATCH requests from third party

I'm pretty inexperienced with any form of http request more complicated than a basic GET query. I've tried to do research online, but I'm having trouble figuring out where to start because I don't know all the required terminology.
For several years I've worked a side job for a data entry company. Basically what I do is Google several things, find the results from a few specific webpages, and copy those URLs into the company's system. About two years ago I wrote a very basic Python program to do the Googling part for me, and now I want to rewrite it and expand it to do the rest of it as well.
The website uses a combination of POST and PATCH requests to update the information on the database, and because the information is attached to my account I assume there is some form of authentication involved. I don't have access to the system's backend so the best I can do is head to the Network tab under Inspect Element. I can't find anything in the requests' headers that seem to attach to my account.
What do I need to do to authenticate, and if it's not that simple, where's the best place to start learning?
Let me know if you need more information and I'll try to give you what you need--I don't know exactly what's required.

Cookie Option is request.get()

I am not experienced in web development, and trying to use requests.get to get some authenticated data. So far the internet appears to tell me to just do it, and i think i am formatting it wrong, but unsure how. After some trial and error, i was able to grab my cookie for the website. The following is some a made up version of what i grabbed with similar formating.
cookie = "s:abcDEfGHIJ12k34LMNopqRst5UvW-6xy.ZAbCd/eFGhi7j8KlmnoPqrstUvWXYZ90a1BCDE2fGH3"
Then, in python, i am trying to send a request. Following is a bit more pseudo code for what i am doing
r = requests.get('https://www.website.com/api/getData', cookies={"connect.sid": cookie})
After all this, the site keeps sending me a 400 error. Wondering if you guys had any idea if I am putting in the wrong cookie/part of cookie. If everything looks right and it is probably the site at fault, or what.
Grabbed a wireshark capture, and found there were other fields in the cookie that were sent that i had not filled out.
_ga
_gid
___gads
Filled those out with the relevant values, and it works.

How do I send the output of a script to a server?

Goal: I want to send the result of a <script> tag in HTML to a server of any kind.
I am working with the Spotify authorization API for a project and one of the required keys is contained in a query string at the end of my app’s redirect URI. I need to get this key.
My solution is to set the redirect URI to a redirect page. Then, on the page, automatically run a script that gets the current URL and send it to my Python script for use. If there’s another way, please tell me, because I’m pretty stuck.
However, to send variables between HTML and Python, I have found I need to use a simple server. Setting this up is the hard part. I’ve made Java, Node.js, and Python servers, but nothing seems to want to work with the <string> tag, and I’m doubtful something like this would even get the actual output of the script.
Is there a way to do this? This is a pretty long question and I apologize, and it should probably be noted I’m a beginner so an explanation and code examples would be nice. Thank you to anyone who reads this!

Filtering on Top of Already Developed Proxy

I'm currently trying to design a filter that with it I can block certain URLs and also
block based on keywords that may be at the data that came back from the http response.
Just for clarification I'm working on a Windows 10 x64 machine for this project.
In order to be able to do such thing, I quickly understood that I would need a web proxy.
I checked out about 6 proxies written in python that I found on github.
These are the project I tried to use (some are Python3 some 2):
https://github.com/abhinavsingh/proxy.py/blob/develop/proxy.py
https://github.com/inaz2/proxy2/blob/master/proxy2.py
https://github.com/inaz2/SimpleHTTPProxy - this one is the earlier version of the top one
https://github.com/FeeiCN/WebProxy
Abhinavsingh's Proxy (first in the list):
what I want to happen
I want the proxy to be able to block sites based on the requests and the content came back, I also need the filter to be in a separated file and to be generic
so I can apply it on every site and every request/response.
I'd like understand where is the correct place to put a filter on this proxy
and how to do redirect or just send a block page back when the client tries to access
sites that has specific urls, or when the response is a page that contains some keywords.
what I tried
I enabled the proxy on google chrome's 'Open proxy settings'
and executed the script. It looked pretty promising, and I noticed that I can insert a filterer's function call in line 383 at the _process_request function so that I can return
to it maybe another host to redirect to or just block. It worked partially for me.
The problems
First of all, I couldn't fully redirect/block the sites. Sometimes it worked, Sometimes it
didn't.
Another problem I ran into was that I realized that I can't access the content of site that returned, if it is https.
Also, filter the response was unfortunately not clear for me.
I noticed also that proxy2 (the second in the list) can solve that problem I had
of filtering https page's content, but I couldn't find out how to make this feature work (and also I think it requires linux utilities anyhow).
The process I described above was pretty much the one that I tried to work on every proxy in the list. At some proxies, like proxy2.py I couldn't understand at all what I needed to do.
If anybody managed to make a filter on top of this proxy or any other from this list and can help me understand how to do it, I'll be grateful if you'll comment down below.
Thank you.

Python request get data through HTTPS tunnel

I am currently pulling data from a public series data from https://www3.bcb.gov.br/expectativas/publico/en/serieestatisticas
This is a public page that uses apache wicket I believe.
I usually am ok with scraping, whether GET or POST. Here I and my colleagues are stuck. Can anyone help understand what URL needs to be used to actually make the request. Here's what I've got so far:
The form with inputs:
The Fiddler capture manually executed:
Text View:
form19_hf_0=&indicador=0&calculo=0&linhaPeriodicidade%3Aperiodicidade=0&tfDataInicial=11%2F10%2F2015&tfDataFinal=11%2F24%2F2015&divPeriodoRefereEstatisticas%3AgrupoAnoReferencia%3AanoReferenciaInicial=16&divPeriodoRefereEstatisticas%3AgrupoAnoReferencia%3AanoReferenciaFinal=16&btnCSV=Generate+CSV
Form data I'm passing in the request:
Summary:
I need some help, I can't seem to get the POST working correctly, it takes me to a different page, and I'm not sure of how to work through this one.
NB: I'm trying to grab back a CSV.
The libraries I'm using are primarily Requests (I was going to use LXML but I don't think its going to be applicable here).
I've been trying to figure out the right form with Postman and Fiddler to understand what the request needs to be.
So,
The solution to this was somewhat indirect. We were not able to do a straight POST because the the page incremented the actual POST url in a way that was generally impossible to predict.
The solution that we used was installing Selenium web driver and using that to simulate the dropdown visible values and button clicks.
This worked out very cleanly.
Thanks and HTH anyone else who might have a similar problem.

Categories