rails 获取hotmail 联系人

很是纠结了一段时间,下面直接上代码。

 

首先到这个网站上申请你的windows live 应用

https://manage.dev.live.com

注册帐号,申请API应用。

 

在rails里安装mislav_contacts 插件。

conf下新建import.yml文件

development: #开发环境下使用
  windows_live:
    appid: #申请的ID 下面不在解释
    secret: #申请的secret
    security_algorithm: wsignin1.0
    return_url: #返回联系人列表的链接
    policy_url: #首页


test: #测试环境下使用
  windows_live:
    appid: 
    secret: 
    security_algorithm: wsignin1.0
    return_url:
    policy_url:


        
qa: #发布环境
  windows_live:
    appid: 
    secret: 
    security_algorithm: wsignin1.0
    return_url:
    policy_url:


    
production:#正式运行环境
  windows_live:
    appid: 
    secret: 
    security_algorithm: wsignin1.0
    return_url:
    policy_url:

 在 lib下新建 windows_live.rb

module Contacts
  class WindowsLive
    
    def initialize(config_file=CONFIG_FILE)
      confs = config_file.is_a?(Hash) ? config_file : YAML.load_file(config_file)['windows_live']
      @wll = WindowsLiveLogin.new(confs['appid'], confs['secret'], confs['security_algorithm'],
                                  nil, confs['policy_url'], confs['return_url'])
    end
  end
end

 在controller下新建hotmailcontroller 文件

class HotmailController < ApplicationController
  layout "import"
  
  def authenticate  #生成访问链接
    redirect_to Import::Hotmail.authenticate
  end
  
  def authorize      #返回处理
    @contacts = Import::Hotmail.authorize(params).collect{|c| c.email}.sort
    flash[:notice] = "Hotmail"
    render :action=>"import"    
  end
end

 路由内添加

  resources :hotmail do
    collection do
      get 'authenticate'
      post 'authorize'      
      get 'import'
    end
  end

在模块里创建import目录并创建hotmail.rb

module Import
  class Hotmail

    def self.authenticate 
      wl = Contacts::WindowsLive.new(KEY_SECRET["windows_live"])
      auth_url = wl.get_authentication_url
    end
    
    def self.authorize(params)
      wl = Contacts::WindowsLive.new(KEY_SECRET["windows_live"])
      wl.contacts(params.merge("action" => "delauth").to_param)      
    end
  end
end
 

 

 恩 这样就可以操作hotmail的联系人了,先记录下来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值