S
S
savva32018-07-21 09:41:50
Python
savva3, 2018-07-21 09:41:50

How to remove the opening in the 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 remove the opening in the browser, i.e. this fragment
parameters = {
            'request_token' : request_token,
            'redirect_uri' : redirect_uri }

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

But if this is done, an error occurs:
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

What to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2018-07-21
@dimonchik2013

if in OAUTH, the browser cannot be avoided
by literate or able to work with a headless browser
see example, adapt
or another user authentication method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question