How to ignore HTTP Error and finish request - python

I do a request to 'someurl' and have redirected. The redirect link does't exist, but in the link I have access_token and others important data. I like to get this redirect url with out program crash.
a = opener.open('https://connect.ok.ru/oauth/authorize?client_id=1247511808&scope=VALUABLE_ACCESS&response_type=token&redirect_uri=https://smasu.ru')
So, redirect to site smasu.ru, but this site does't exist. The link will have type as:
https://smasu.ru/#access_token=8.7fd19d96afcfc687b92bd50e2df6011837b94753e09f315818c0328e9&session_secret_key=363b5dd2ab1a1a44c25e423e892732ce&permissions_granted=VALUABLE_ACCESS&expires_in=1800
Where you can see acces_token, session_secret_key, that I want take for my program. How to do a request with ignore HTTP Error and get the redirect url?
Traceback:

Related

Post request to Amazon fails because of supposedly "invalid email"

I am trying to use Python requests to log into amazon.se. To do so, I first make a GET request to one of the pages, get redirected to the sign-in page, and make a POST request using the data from the login form + my credentials.
The problem is that in response I get the sign-in page with the following error:
I am of course sure that both the email and password are valid, but the login process still fails in both Python and Postman. I tried to compare the browser requests to my manufactured ones, and they seem almost identical except for me missing a couple of what I believe are non-essential headers. Nevertheless, there must be something going on behind the scenes that I am missing.
Postman POST request
Headers:
Body (form data from previous GET request):
Browser POST request
Headers:
Body:

Detecting HTTP redirects

I have a problem with a website called www.webhallen.com. I want to put any numbers and character after www.webhallen.com/ for example www.webhallen.com/3459gdfg. I then want to se if that website exist the problem is that the site rederect me to there own 404 page. How can I via requests see if they redirect me?
You can check for the response status code. If you get response.status_code as 404. You will have been redirected to there 404 site.

How to properly login to reddit via python requests?

I am trying to login to my reddit account using python, not Praw. I succufully extracted the CSRF token, but still don't know why it is not functional and unable to properly login. The desired behavior of the code is to be able to login to reddit through this python script and then confirm if the logging was a success or not.
When you try to login on your browser, in the developer tools (Network tab) you can preview how the request should look. The mistake you made here is that the POST request should be sent not to: https://www.reddit.com/, but https://www.reddit.com/login.
To verify the result you can check, in the debugger, the response you received to your POST request. When I ran your code with the mentioned change of URL the response to "r" was: response
You can see that the server returned response status code 400 with explanation "incorrect username or password". It means that your code should be enough to login to Reddit with inputting the correct credentials.

Why doesn't urllib2 throw a 404?

I have a public folder in Google Drive, in which I store pictures.
In Python, I am trying to detect if a picture with a particular name exist or not. I am using this code:
import urllib2
url = "http://googledrive.com/host/0B7K23HtYjKyBfnhYbkVyUld3YUVqSWgzWm1uMXdrMzQ0NlEwOXVUd3o0MWVYQ1ZVMlFSNms/0000.png"
resp = urllib2.urlopen(url)
print resp.getcode()
And even though there is no file with this name in this folder, this code is not throwing an exception and is printing "200" as the return code. I have checked in my browser and this URL (http://googledrive.com/host/0B7K23HtYjKyBfnhYbkVyUld3YUVqSWgzWm1uMXdrMzQ0NlEwOXVUd3o0MWVYQ1ZVMlFSNms/0000.png) does return a 404, after a few redirects.
Why doesn't urllib2 detect that this file actually doesn't exist?
When you make the request, your request goes to google's web servers and is processed there. If and only if google's servers were to return a 404, would you see a 404 on your end; urllub2 simply encapsulates the underlying handshaking and data transfer logic.
In this particular case, google's server side code requires the request to be authenticated, and your request url is simply unauthenticated. As such, the request is redirected to the login page, and since this is a valid existing page/response, urllib2 shows the correct code 200. You can get the same page if you open the link in a private window.
However, if you are authenticated and then open the url (basically logged into your gmail/googgle docs account), you would get the 404 error.

Google OAuth2 redirect_uri_mismatch Issue

All,
Im getting a 400 redirect_url_mismatch error upon attempting to authenticate through google. I'm using python-socal-auth through a django application to achieve this process.
Everything works smoothly, until I get to the final stages of the process where I hit a redirect_uri_mismatch issue.
On google, I receive this message.
"The redirect URI in the request: http://localhost:8000/something/complete/google-oauth2/ did not match a registered redirect URI"
`Request Details
from_login=1
scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile
response_type=code
redirect_uri=http://localhost:8000/something/complete/google-oauth2/
state=qT1RLLMa72F8NxFFubHwCVe3GgLDNcgZ
as=-55f896f3314b21af
pli=1
client_id=160177117398
authuser=0
hl=en`
Included below is a screenshot of the client ID's redirect URI.
What am I doing wrong?
Thanks!
One thing to note here is that redirect url should be exactly same till the last trailing slash. In my case, it was like http://localhot:8000/something/complete/google-oauth2
This should have been
http://localhot:8000/something/complete/google-oauth2/
This resulted in redirect_uri_mismatch.
Also define http: and https: in the console for redirect url because redirect url generated by social auth still sends http regardless of ssl setting in your server.
Make sure you added the social-auth in the urls.py as
url('', include('social.apps.django_app.urls', namespace='social')),
and on console.developers.google.com set the Authorized redirect URIs to http://localhost:8000/something/complete/google-oauth2/

Categories

Resources