Ruby on rails Rails 3使用omniauth获取gmail联系人?

Ruby on rails Rails 3使用omniauth获取gmail联系人?,ruby-on-rails,gmail,omniauth,Ruby On Rails,Gmail,Omniauth,我正在使用omniauth使用google凭据成功登录?omniauth正在提供uid,如下链接所示 通过使用上面的链接可以获得gmail联系人或他们的任何其他方式获得gmail联系人不,Omniauth只提供身份验证 您可能会对一块宝石感兴趣: 引用:“Contacts是一个通用接口,可以从Hotmail、AOL、Gmail、Plaxo和Yahoo等不同的提供商那里获取联系人列表信息。” 编辑:也看看这篇博文:从中获取您的客户id和客户机密。这是一个粗略的脚本,非常好用。根据您的需要进行修改

我正在使用omniauth使用google凭据成功登录?omniauth正在提供uid,如下链接所示


通过使用上面的链接可以获得gmail联系人或他们的任何其他方式获得gmail联系人

不,Omniauth只提供身份验证

您可能会对一块宝石感兴趣:

引用:“Contacts是一个通用接口,可以从Hotmail、AOL、Gmail、Plaxo和Yahoo等不同的提供商那里获取联系人列表信息。”


编辑:也看看这篇博文:

从中获取您的客户id和客户机密。这是一个粗略的脚本,非常好用。根据您的需要进行修改

    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
需要“net/http”
需要“net/https”
需要“uri”
需要“rexml/文档”
类ImportController令牌,'client\u id'=>client\u id,'client\u secret'=>client\u secret,'redirect\u uri'=>googleauth\u url,'grant\u type'=>授权码')
request.content_type='application/x-www-form-urlencoded'
response=http.request(请求)
响应代码
access\u keys=ActiveSupport::JSON.decode(response.body)
uri=uri.parse(“https://www.google.com/m8/feeds/contacts/default/full?oauth_token=“+access_key['access_token'].to_s+”&max results=50000&alt=json”)
http=Net::http.new(uri.host,uri.port)
http.use_ssl=true
http.verify\u mode=OpenSSL::SSL::verify\u NONE
request=Net::HTTP::Get.new(uri.request\u uri)
response=http.request(请求)
contacts=ActiveSupport::JSON.decode(response.body)
联系人['feed']['entry']。每个带有索引的|联系人,索引|
姓名=联系人['title']['$t']
联系['gd$email'],发送电子邮件给a|
电子邮件地址=电子邮件['address']
Invite.create(:full\u name=>name,:email=>email\u address,:Invite\u source=>Gmail,:user\u id=>current\u user.id)#用于测试,我正在将其推送到数据库中。。
结束
结束
营救异常=>ex
短信
结束
重定向到根路径:notice=>“邀请或关注您的Google联系人。”
结束
结束
设置的屏幕截图

MultiJson::LoadError(795:错误401处的意外标记(客户端错误)