Coder Social home page Coder Social logo

Social login with VK about django-rest-auth HOT 12 CLOSED

tivix avatar tivix commented on September 16, 2024
Social login with VK

from django-rest-auth.

Comments (12)

gorros avatar gorros commented on September 16, 2024 1

Thanks for reply.

I use following view:'

class VKLogin(SocialLogin):
    adapter_class = VKOAuth2Adapter

and receive following error

TypeError at /api/auth/vk/

string indices must be integers

which is result of following line in SocialLoginSerializer

 login = self.adapter.complete_login(request, app, token,
                                                response=access_token)

since complete_login in VK0Auth2Adapter is defined a little bit deferent that in Facebook0Auth2Adapter:

def complete_login(self, request, app, token, **kwargs):
        uid = kwargs['response']['user_id']
        resp = requests.get(self.profile_url,
                            params={'access_token': token.token,
                                    'fields': ','.join(USER_FIELDS),
                                    'user_ids': uid})
        resp.raise_for_status()
        extra_data = resp.json()['response'][0]
        return self.get_provider().sociallogin_from_response(request,
                                                             extra_data)

Mainly you a need also to pass user_id (kwargs['response']['user_id']) to obtain relevant response according http://vk.com/dev/users.get. And as I understand from VK API docs on this step access_token is not required.

from django-rest-auth.

khasanovbi avatar khasanovbi commented on September 16, 2024 1

The problem is actual. Please reopen issue.

from django-rest-auth.

MIRAMAXED avatar MIRAMAXED commented on September 16, 2024 1

I have the same problem too:
File ... in complete_login
37. uid = kwargs['response']['user_id']

Exception Type: TypeError at /api/vk/
Exception Value: string indices must be integers

What to do?

from django-rest-auth.

mateusz-sikora avatar mateusz-sikora commented on September 16, 2024

Sure, you can create your own SocialLoginSerializer and define your own view class, like:

from rest_auth.views import Login
class CustomLogin(Login):
    serializer_class = CustomSocialLoginSerializer
    adapter_class = VKOAuth2Adapter

Could you paste error traceback which you get when using VK adapter?

from django-rest-auth.

mateusz-sikora avatar mateusz-sikora commented on September 16, 2024

So the only solution for you at this moment is to define you own social login serializer basing on SocialLoginSerializer and override validate method. There is no way to make it simpler, unfortunately.

You can also fork this project, make a patch and create a pull request to apply it to DRA code.

from django-rest-auth.

gorros avatar gorros commented on September 16, 2024

That's exactly what I was planing to do.

from django-rest-auth.

gorros avatar gorros commented on September 16, 2024

By the way, I am glad you fixed UserDetailsSerializer import in rest_auth/registration/views.py in v0.3.3 (e9fee3a).

from django-rest-auth.

sburavtsov avatar sburavtsov commented on September 16, 2024

Hi gorros, sorry I write it here, did you solve vk authorization issue? Can you help me with that?
My email is [email protected]

from django-rest-auth.

gorros avatar gorros commented on September 16, 2024

@sburavtsov This was a while ago, but according to my code this should solve the problem.


class CustomVKOAuth2Adapter(VKOAuth2Adapter):

    def complete_login(self, request, app, token, **kwargs):
        uid = kwargs['response']['user_id']
        resp = requests.get(self.profile_url,
                            params={'fields': ','.join(USER_FIELDS),
                                    'user_ids': uid})
        resp.raise_for_status()
        extra_data = resp.json()['response'][0]
        return self.get_provider().sociallogin_from_response(request,
                                                             extra_data)

from django-rest-auth.

sburavtsov avatar sburavtsov commented on September 16, 2024

Than you! I will try it.


With the best regards, Sergey
Skype: serobur_skype
Phone: +7 960 102 40 40

On Feb 8, 2016, at 23:24, Gor Hayrapetyan [email protected] wrote:

@sburavtsov https://github.com/sburavtsov This was a while ago, but according to my code this should solve the problem.

class CustomVKOAuth2Adapter(VKOAuth2Adapter):

def complete_login(self, request, app, token, **kwargs):
    uid = kwargs['response']['user_id']
    resp = requests.get(self.profile_url,
                        params={'fields': ','.join(USER_FIELDS),
                                'user_ids': uid})
    resp.raise_for_status()
    extra_data = resp.json()['response'][0]
    return self.get_provider().sociallogin_from_response(request,
                                                         extra_data)


Reply to this email directly or view it on GitHub #45 (comment).

from django-rest-auth.

kamirt avatar kamirt commented on September 16, 2024

I had the same problem so I had to find the solvation. The point is here:

  File "/home/kamirt/nissaenv/lib/python3.4/site-packages/rest_auth/registration/serializers.py", line 247, in validate
    login = self.get_social_login(adapter, app, social_token, access_token)
  File "/home/kamirt/nissaenv/lib/python3.4/site-packages/rest_auth/registration/serializers.py", line 180, in get_social_login
    social_login = adapter.complete_login(request, app, token, response=response)

This "response" parameter in case VK must be like this: {user_id:*******, access_token: *******, expire_time: *******}. But this function passes already parsed response (only access_code).

from django-rest-auth.

taime avatar taime commented on September 16, 2024

So what is the final solution?
Here is the whole code in urls.py:

from allauth.socialaccount.providers.vk.views import VKOAuth2Adapter

class CustomVKOAuth2Adapter(VKOAuth2Adapter):

    def complete_login(self, request, app, token, **kwargs):
        uid = kwargs['response']['user_id']
        resp = requests.get(self.profile_url,
                            params={'fields': ','.join(USER_FIELDS),
                                    'user_ids': uid})
        resp.raise_for_status()
        extra_data = resp.json()['response'][0]
        return self.get_provider().sociallogin_from_response(request,
                                                             extra_data)

in urlpatterns:

url(r'^rest-auth/vk/$', CustomVKOAuth2Adapter, name='vk_login'),

And I get error in browser what open http://127.0.0.1:8000/rest-auth/vk/:
AttributeError at /rest-auth/vk/ 'CustomVKOAuth2Adapter' object has no attribute 'status_code'

How to fix it?

from django-rest-auth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.