How to remove the open in browser?

There is a small script
import urllib2
import urllib
import simplejson
import webbrowser

REQUEST_TOKEN_URL = 'https://getpocket.com/v3/oauth/request'
AUTHORIZE_REQUEST_URL = "https://getpocket.com/auth/authorize"
AUTHORIZE_REQUEST_TOKEN = 'https://getpocket.com/v3/oauth/authorize'

REDIRECT_URI = 'http://goo.gl/kGuI6g'
consumer_key = 'dsdddsdsd'

def get_access_token(consumer_key, redirect_uri = REDIRECT_URI):
 parameters = {
 'consumer_key' : consumer_key,
 'redirect_uri' : redirect_uri }
 headers = { 'X-Accept': 'application/json' }

 request = urllib2.Request(REQUEST_TOKEN_URL, urllib.urlencode(parameters), headers)

try:
 resp = urllib2.urlopen(request)
 except Exception, e:
 print 'Invalid response from Pocket requesting request_token: %s' % e
return


 request_token = simplejson.loads(resp.read())['code']

 print "
 print 'I will try to start a browser so you can authorize the request token'
 print "

 parameters = {
 'request_token' : request_token,
 'redirect_uri' : redirect_uri }

 webbrowser.open(AUTHORIZE_REQUEST_URL + '?' + urllib.urlencode(parameters))
 go_ahead = 'y'

 if not go_ahead: return

 parameters = {
 'consumer_key' : consumer_key,
 'code' : request_token }

 request = urllib2.Request(AUTHORIZE_REQUEST_TOKEN, urllib.urlencode(parameters), headers)
 resp = urllib2.urlopen(request)

 access_token = simplejson.loads(resp.read())

 print 'Your Pocket Access Token: %s' % access_token['access_token']
 return access_token['access_token']

token = get_access_token(consumer_key)

I need to clear the opening in the browser, ie here is the snippet
parameters = {
 'request_token' : request_token,
 'redirect_uri' : redirect_uri }

 webbrowser.open(AUTHORIZE_REQUEST_URL + '?' + urllib.urlencode(parameters))

But if you do then there is a bug:
I will try to start a browser so you can authorize the request token

Traceback (most recent call last):
 File "pocket.py", line 55, in <module>
 token = get_access_token(consumer_key)
 File "pocket.py", line 48, in get_access_token
 resp = urllib2.urlopen(request)
 File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
 return opener.open(url, data, timeout)
 File "/usr/lib/python2.7/urllib2.py", line 435, in open
 response = meth(req, response)
 File "/usr/lib/python2.7/urllib2.py", line 548, in http_response
 'http', request, response, code, msg, hdrs)
 File "/usr/lib/python2.7/urllib2.py", line 473, in error
 return self._call_chain(*args)
 File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
 result = func(*args)
 File "/usr/lib/python2.7/urllib2.py", line 556, in http_error_default
 raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden</module>

What to do?
June 3rd 19 at 19:16
1 answer
June 3rd 19 at 19:18
if in OAUTH, the browser can not be avoided

educated Taliban know how to work with headless browser

see example, prisposobili

or another authentication method rock

Find more questions by tags Python