0
votes

When I use urllib, urllib2, or requests on Python 2.7, neither one ends up at the same URL as I do when I copy and paste the starting URL into Chrome or FireFox for Mac.

EDIT: I suspect this is because one has to be signed in to vk.com to be redirected. If this is the case, how do I add the sign-in to my script? Thanks!

Starting URL: https://oauth.vk.com/authorize?client_id=PRIVATE&redirect_uri=https://oauth.vk.com/blank.html&scope=friends&response_type=token&v=5.68

Actual final (redirected) URL: https://oauth.vk.com/blank.html#access_token=PRIVATE_TOKEN&expires_in=86400&user_id=PRIVATE

PRIVATE, PRIVATE_TOKEN = censored information

The following is one of several attempts at this:

import requests

APPID = 'PRIVATE'
DISPLAY_OPTION = 'popup' # or 'window' or 'mobile' or 'popup'
REDIRECT_URL = 'https://oauth.vk.com/blank.html'
SCOPE = 'friends' # https://vk.com/dev/permissions
RESPONSE_TYPE = 'token' # Documentation is vague on this. I don't know what 
        # other options there are, but given the context, i.e. that we want an
        # "access token", I suppose this is the correct input

URL = 'https://oauth.vk.com/authorize?client_id=' + APPID + \
      '&display='+ DISPLAY_OPTION + \
      '&redirect_uri=' + REDIRECT_URL + \
      '&scope=' + SCOPE + \
      '&response_type=' + RESPONSE_TYPE + \
      '&v=5.68'

# with requests

REQUEST = requests.get(URL)

RESPONSE_URL = REQUEST.url

I hope you notice whatever it is that's wrong with my code.

Extra info: I need the redirect because the PRIVATE_TOKEN value is necessary for further programming.

I tried some debugging but neither the interpreter nor IPython print out the debugging info.

Thanks!

1
we cannot see the server response from https://oauth.vk.com/authorize?client_id=PRIVATE&redirect_uri=https://oauth.vk.com/blank.html&scope=friends&response_type=token&v=5.68 if you don't provide private token. It needs a client id - mehulmpt
@mehulmpt Try 3882511, it is the ID for a public app. - Herman Autore

1 Answers

0
votes

The problem is the result of not being signed in in the Python environment.

Solution:

Use twill to create browser in Python and sign in.

Code:

from twill.commands import *

BROWSER = get_browser()

BROWSER.go(URL) # URL is the URL concatenated in the question

RESPONSE_URL = BROWSER.get_url()