U
U
un1t2015-04-22 16:38:45
Django
un1t, 2015-04-22 16:38:45

Python-social-auth authorization for Vkontakte iframe application?

I am using python-social-auth with "vk-oauth2" backend. The site is working fine.
If I make an iframe application, as described here
python-social-auth.readthedocs.org/en/latest/backe...
authorization does not occur.
Initially, when the iframe pulls the application there in the GET parameters, all sorts of oauth_token, user_id and other parameters are passed. In principle, if python-social-auth allows you to log in knowing these parameters, this option is also suitable. So far I have not found such an example.
Tell me why it might not work with iframe? Or how you can authorize a user through python-social-auth, knowing the oauth_token and other parameters.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
un1t, 2015-07-21
@un1t

I had the same problem. There, when access_token and so on are passed in the iframe in the GET parameters, and you need to use it immediately to log in. A little crooked, but it was necessary to solve the problem very quickly and there was no time to delve deeply. I didn’t understand why it doesn’t work according to the documentation, but after reading the source code and looking at how the iframe opens, I wrote such code.

# settings.py

SOCIAL_AUTH_VK_OAUTH2_KEY = '1239'
SOCIAL_AUTH_VK_OAUTH2_SECRET = 'asdfdasfsdfasdfsdfsda'
SOCIAL_AUTH_VK_APP_USER_MODE = 1

# views.py

from django.contrib.auth import authenticate, login
from social.apps.django_app.utils import psa, load_strategy, load_backend
from social.exceptions import AuthTokenRevoked

def home(request):
    if 'access_token' in request.GET:
        backend = 'vk-oauth2'
        request.social_strategy = load_strategy(request)
        uri = '/'

        request.backend = load_backend(request.social_strategy,
                                                   backend, uri)

        token = request.GET['access_token']
        try:
            user = request.backend.do_auth(token)
        except AuthTokenRevoked:
            user = None
        else:
            if user:
                login(request, user)
                print('OK')
            else:
                print('ERROR')
            return redirect('/')
    return render(request, 'home.html')


# home.html

user: {{ request.user.get_full_name }}
<script src="//vk.com/js/api/xd_connection.js?2" type="text/javascript"></script>
<script type="text/javascript">
  $(function () {
      VK.init(function() {
              VK.addCallback("onApplicationAdded", requestRights);
              VK.addCallback("onSettingsChanged", onSettingsChanged);
          }
      );		
        {% if not request.user.is_authenticated %}
            startConnect();
        {% endif %}
  })


    function startConnect() {
        VK.callMethod('showInstallBox');
    }

    function requestRights() {
        VK.callMethod('showSettingsBox', 1 + 2); // 1+2 is just an example
    }

    function onSettingsChanged(settings) {
        window.location.reload()
    }
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question