Question

import urllib
import urllib2
from urllib2 import Request

url = 'https://www.google.com'
response = urllib2.urlopen(url)

login_url = (url + '/login')
data = {'email': 'john',
        'password': 'example'}
req = Request(login_url, urllib.urlencode(data))
response = urllib2.urlopen(req)


  File "test.py", line 50, in <module>
    response = urllib2.urlopen(req)
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
  urllib2.HTTPError: HTTP Error 405: Method Not Allowed

I am doing a POST method. This script works on my MAC OSX running python 2.7.1, however, on my Virtual Machine running python 2.7 it throws that error. Is there some HTTPHeaders I'm suppose to pass along when making a POST request?

Was it helpful?

Solution

you should find the doc that you need here:

http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html

For doing the auth you need to use this url https://www.google.com/accounts/ClientLogin which require service that you try to reach and it will provide you an access_token for the service specified

You need to store cookie for this type of request :

import urllib2
import cookielib
cookiejar = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(opener)
return urllib2

OTHER TIPS

Yes, for google they will probably require that you have a common user agent to login. In addition to this you will need to store the cookie they give you in python. You can use CookieJar.

This is a great tutorial that teaches how to send headers and save cookies for your requests. http://www.voidspace.org.uk/python/articles/urllib2.shtml

http://www.voidspace.org.uk/python/articles/cookielib.shtml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top