如何从GPlus API中提取Google+朋友的数量?

时间:2014-03-29 15:19:31

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

我想通过omniauth-gplus:the gplus gem提取用户在成功关联Google+帐户后获得的Google+朋友数量。

我能够显示我的用户使用omniauth-twitter和omniauth-facebook gems的Twitter粉丝和Facebook好友的数量,这更容易,因为这两个宝石和API的文档更清晰

请参阅以下代码,了解我如何在Users :: OmniauthCallbacksController下拉动Facebook和Twitter粉丝/朋友:

授权:

class Authorization < ActiveRecord::Base
  attr_accessible :provider, :uid, :token, :secret, :first_name, :last_name, :link, :name, :connections_count

  def update_connections_number(provider)
    if provider == 'Facebook'
      connections_count = Koala::Facebook::API.new(self.token).get_connections("me", "friends").count
      self.update_attributes(connections_count: connections_count)
    elsif provider == 'LinkedIn'
      client = LinkedIn::Client.new(ENV['FR_LINKEDIN_KEY'], ENV['FR_LINKEDIN_SECRET'])
      client.authorize_from_access(self.token, self.secret)
      self.update_attributes(connections_count: client.connections.total)
    end
  end

end

用户

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def find_for_ouath(provider, access_token, resource=nil)
    user, email, name, uid, auth_attr = nil, nil, nil, {}
    case provider
      when "Facebook"
        uid = access_token['uid']
        email = access_token['info']['email']
        auth_attr = { :uid => uid, :token => access_token['credentials']['token'],
          :secret => nil, :first_name => access_token['info']['first_name'],
          :last_name => access_token['info']['last_name'], :name => access_token['info']['name'],
          :link => access_token['extra']['raw_info']['link'] }
      when "Twitter"
        uid = access_token['extra']['raw_info']['id']
        name = access_token['extra']['raw_info']['name']
        auth_attr = { :uid => uid, :token => access_token['credentials']['token'],
          :secret => access_token['credentials']['secret'], :first_name => access_token['info']['first_name'],
          :last_name => access_token['info']['last_name'], :name => name,
          :link => "http://twitter.com/#{name}", :connections_count => access_token['extra']['raw_info']['followers_count'] }
      when 'LinkedIn'
        uid = access_token['uid']
        name = access_token['info']['name']
        auth_attr = { :uid => uid, :token => access_token['credentials']['token'],
          :secret => access_token['credentials']['secret'], :first_name => access_token['info']['first_name'],
          :last_name => access_token['info']['last_name'],
          :link => access_token['info']['public_profile_url'] }
       when 'GPlus'
        uid = access_token['uid']
        name = access_token['info']['email']
        auth_attr = { :uid => uid, :token => access_token['credentials']['token'],
          :secret => access_token['credentials']['secret'], :first_name => access_token['info']['first_name'],
          :last_name => access_token['info']['last_name'],
          :link => access_token['info']['public_profile_url'] }
    else
      raise 'Provider #{provider} not handled'
    end
    if resource.nil?
      if email
        user = find_for_oauth_by_email(email, resource)
      elsif uid && name
        user = find_for_oauth_by_uid(uid, resource)
        if user.nil?
          user = find_for_oauth_by_name(name, resource)
        end
      end
    else
      user = resource
    end

    auth = user.authorizations.find_by_provider(provider)
    if auth.nil?
      auth = user.authorizations.build(:provider => provider)
      user.authorizations << auth
    end
    auth.update_attributes auth_attr
    auth.update_connections_number(provider)
    return user
  end

  def find_for_oauth_by_uid(uid, resource=nil)
    user = nil
    if auth = Authorization.find_by_uid(uid.to_s)
      user = auth.user
    end
    return user
  end

  def find_for_oauth_by_email(email, resource=nil)
    if user = User.find_by_email(email)
      user
    else
      user = User.new(:email => email, :password => Devise.friendly_token[0,20])
      user.save
    end
    return user
  end

  def find_for_oauth_by_name(name, resource=nil)
    if user = User.find_by_name(name)
      user
    else
      first_name = name
      last_name = name
      user = User.new(:first_name => first_name, :last_name => last_name, :password => Devise.friendly_token[0,20], :email => "#{UUIDTools::UUID.random_create}@host")
      user.save(:validate => false)
    end
    return user
  end

end

如何使用Google +执行此操作?

干杯!

0 个答案:

没有答案