djangopython-3.xoauth-2.0gdata

How to use google contacts api to allow my users to invite their gmail contacts to my django website


I have a website built in django 1.7, python 3.4. I want to enable my users to invite their gmail contacts to my website (like linkedin & many other websites do). I am using Oauth2.0 and am able to get permission to access their contacts. But i am not getting an idea how to proceed and what steps to take.

Can somebody help me to get an overview of all the steps that i need to take and a little explanation as to how to do that.

Even a link to suitable post would be helpful.


Solution

  • See, When you need to implement these features in your website, you will have to understand the APIs etc to utilize it to the fullest.

    Go through this https://developers.google.com/google-apps/contacts/v3/?csw=1#audience

    Let's talk only about google only. The rest providers can also be managed with similar steps. Here you are using django-allauth for this task.

    The basic steps involved are:

    1. Get your app created and configured with the provider. for that you will need a developer profile in google(or facebook etc.). You will have to create an app in google developer console and you will find a plenty of tutorial for this on internet. That has been done by you as you have signup with google activated on your site. That is server side of Oauth2.0

    2. Now you need to define the scope of authorization you need. You might only need the access to view the public profile thing. that may include first name, last name, email, id, gender, etc. For your app, you need contacts of users and for that you will have to include it in the scope too. That is done in settings.py only.

      'google': {'SCOPE': ['profile', 'email', 'https://www.googleapis.com/auth/contacts'], 'AUTH_PARAMS': {'access_type': 'online'}} }

    3. Now here, you have got the access to the contacts. Now, you only need to extract the contacts with the consent of data owner(user).

    For this purpose,you may follow the first link in the answer. What you have to do is you have to send a get request to some url('https://www.google.com/m8/feeds/contacts/default/full' + '?access_token=' + access_token). The request goes to provider only(google) with the authorization token it has provided you for that particular user. That you will find in the db table socialtoken. Once you send proper request, the response you will get is the contacts of the user in xml format.

    Once you get it, you can easily parse it to extract the required information.

    Things are simple if you understand the flow. django-allauth only helpy you upto signup & signin where you can get different permissions through defining the scope.

    For extracting the contacts, you can write your own code.

    A simple example is:

    def get_email_google(request):
        # social = request.user.social_auth.get(provider='google-oauth2')
        user =request.user
    
        # Code dependent upon django-allauth. Will change if we shift to another module
    
        # if request.user.userprofile.get_provider() != "google":
        a = SocialAccount.objects.get(user=user)
        b = SocialToken.objects.get(account=a)
        # access = b.token
        access_token = b.token
        url = 'https://www.google.com/m8/feeds/contacts/default/full' + '?access_token=' + access_token + '&max-results=100'
        req = urllib2.Request(url, headers={'User-Agent' : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/11.04 Chromium/12.0.742.112 Chrome/12.0.742.112 Safari/534.30"})
        contacts = urllib2.urlopen(req).read()
        contacts_xml = etree.fromstring(contacts)
        # print
        # return render(request, 'search/random_text_print.html', locals())
    
        result = []
    
    
        for entry in contacts_xml.findall('{http://www.w3.org/2005/Atom}entry'):
            for address in entry.findall('{http://schemas.google.com/g/2005}email'):
                email = address.attrib.get('address')
                result.append(email)
        return render(request, 'search/random_text_print.html', locals())