微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Rails 3使用omniauth获取Gmail联系人?

如何解决Rails 3使用omniauth获取Gmail联系人?

| 我可以使用omniauth使用Google凭据成功登录吗? omn​​iauth正在提供uid,如下所示 https://www.google.com/accounts/o8/id?id=xxxxxxxxxx 通过使用上面的链接可以获取gmail联系人或以其他任何方式获取gmail联系人     

解决方法

不,Omniauth仅提供身份验证。 有一个宝石可能会让您感兴趣:https://github.com/cardmagic/contacts Quote:\“联系人是一个通用界面,可从包括Hotmail,AOL,Gmail,Plaxo和Yahoo在内的各种提供商那里获取联系人列表信息。” 编辑:也看一下这个博客文章:http://rtdptech.com/2010/12/importing-gmail-contacts-list-to-rails-application/     ,从此处获取您的client_id和client_secret。这是一个粗糙的脚本,效果很好。根据您的需要进行修改。
    require \'net/http\'
    require \'net/https\'
    require \'uri\'
    require \'rexml/document\'

    class ImportController < ApplicationController

      def authenticate
        @title = \"Google Authetication\"

        client_id = \"xxxxxxxxxxxxxx.apps.googleusercontent.com\"
        google_root_url = \"https://accounts.google.com/o/oauth2/auth?state=profile&redirect_uri=\"+googleauth_url+\"&response_type=code&client_id=\"+client_id.to_s+\"&approval_prompt=force&scope=https://www.google.com/m8/feeds/\"
        redirect_to google_root_url
      end

      def authorise
        begin
          @title = \"Google Authetication\"
          token = params[:code]
          client_id = \"xxxxxxxxxxxxxx.apps.googleusercontent.com\"
          client_secret = \"xxxxxxxxxxxxxx\"
          uri = URI(\'https://accounts.google.com/o/oauth2/token\')
          http = Net::HTTP.new(uri.host,uri.port)
          http.use_ssl = true
          http.verify_mode = OpenSSL::SSL::VERIFY_NONE
          request = Net::HTTP::Post.new(uri.request_uri)

          request.set_form_data(\'code\' => token,\'client_id\' => client_id,\'client_secret\' => client_secret,\'redirect_uri\' => googleauth_url,\'grant_type\' => \'authorization_code\')
          request.content_type = \'application/x-www-form-urlencoded\'
          response = http.request(request)
          response.code
          access_keys = ActiveSupport::JSON.decode(response.body)

          uri = URI.parse(\"https://www.google.com/m8/feeds/contacts/default/full?oauth_token=\"+access_keys[\'access_token\'].to_s+\"&max-results=50000&alt=json\")

          http = Net::HTTP.new(uri.host,uri.port)
          http.use_ssl = true
          http.verify_mode = OpenSSL::SSL::VERIFY_NONE
          request = Net::HTTP::Get.new(uri.request_uri)
          response = http.request(request)
          contacts = ActiveSupport::JSON.decode(response.body)
          contacts[\'feed\'][\'entry\'].each_with_index do |contact,index|

             name = contact[\'title\'][\'$t\']
             contact[\'gd$email\'].to_a.each do |email|
              email_address = email[\'address\']
              Invite.create(:full_name => name,:email => email_address,:invite_source => \"Gmail\",:user_id => current_user.id)  # for testing i m pushing it into database..
            end

          end  
        rescue Exception => ex
           ex.message
        end
        redirect_to root_path,:notice => \"Invite or follow your Google contacts.\"


      end

    end
设置的屏幕截图。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。