【问题标题】:How to use google contacts api to allow my users to invite their gmail contacts to my django website如何使用 google 联系人 api 允许我的用户邀请他们的 gmail 联系人访问我的 django 网站
【发布时间】:2016-08-09 15:39:54
【问题描述】:

我有一个使用 django 1.7、python 3.4 构建的网站。我想让我的用户能够邀请他们的 gmail 联系人访问我的网站(就像linkedin 和许多其他网站一样)。我正在使用 Oauth2.0 并且能够获得访问他们联系人的权限。但我不知道如何进行以及采取什么步骤。

谁能帮我大致了解一下我需要采取的所有步骤,并解释一下如何做到这一点。

即使是指向合适帖子的链接也会有所帮助。

【问题讨论】:

    标签: django python-3.x oauth-2.0 gdata


    【解决方案1】:

    请参阅,当您需要在您的网站中实现这些功能时,您必须了解 API 等才能充分利用它。

    通过这个https://developers.google.com/google-apps/contacts/v3/?csw=1#audience

    让我们只谈谈谷歌。其他提供者也可以通过类似的步骤进行管理。在这里,您正在使用 django-allauth 来完成此任务。

    涉及的基本步骤是:

    1. 使用提供程序创建和配置您的应用程序。为此,您将需要在 google(或 facebook 等)中的开发人员资料。你必须在谷歌开发者控制台中创建一个应用程序,你会在互联网上找到大量的教程。这已由您完成,因为您已在您的网站上激活了 google 注册。那是Oauth2.0的服务器端

    2. 现在您需要定义您需要的授权范围。您可能只需要查看公共个人资料的权限即可。这可能包括名字、姓氏、电子邮件、ID、性别等。对于您的应用程序,您需要用户的联系人,为此您也必须将其包含在范围内。 这仅在 settings.py 中完成。

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

    3. 现在,您可以访问联系人了。现在,您只需要在数据所有者(用户)同意的情况下提取联系人即可。

    为此,您可以点击答案中的第一个链接。你需要做的是你必须向某个 url('https://www.google.com/m8/feeds/contacts/default/full' + '?access_token=' + access_token) 发送一个获取请求。该请求仅使用它为该特定用户提供给您的授权令牌发送给提供者(谷歌)。您将在 db 表 socialtoken 中找到它。一旦您发送正确的请求,您将得到的响应是 xml 格式的用户联系人。

    一旦你得到它,你可以很容易地解析它以提取所需的信息。

    如果您了解流程,事情就很简单。 django-allauth 仅帮助您注册和登录,您可以通过定义范围获得不同的权限。

    要提取联系人,您可以编写自己的代码。

    一个简单的例子是:

    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())
    

    【讨论】:

      【解决方案2】:
      user =request.user
      a = SocialAccount.objects.get(user=user)
      b = SocialToken.objects.get(account=a)
      # access = b.token
      access_token = b.token
      SCOPES = ['SCOPE_URL']
      creds = client.AccessTokenCredentials(access_token, 'USER_AGENT') 
      service = build('calendar', 'v3', credentials=creds)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-25
        • 1970-01-01
        • 1970-01-01
        • 2013-10-20
        • 2013-03-05
        • 2012-10-18
        • 2015-01-08
        相关资源
        最近更新 更多