Coder Social home page Coder Social logo

oauth_china's Introduction

#简介

通过OAuth方式同步用户消息到微博平台(支持豆瓣,新浪微薄,腾讯微博,搜狐微博,网易微博)
和omini-auth的区别:omini-auth是专门提供oauth授权和获取用户信息的gem(比如用新浪微博帐号登陆这种需求)
oauth_china是一个方便的同步信息到其他微博平台的gem(用来做像follow5.com或http://fanfou.com/settings/sync这样需求)

#安装

gem install oauth_china

#使用

  • 在Gemfile里添加:

    gem 'oauth' gem 'oauth_china'

  • 添加配置文件

     配置文件路径:
     config/oauth/douban.yml
     config/oauth/sina.yml
     config/oauth/qq.yml
     config/oauth/sohu.yml
     config/oauth/netease.yml
    
     配置文件格式:
     development:
       key:    "you api key"
       secret: "your secret"
       url:    "http://yoursite.com"
       callback: "http://localhost:3000/your_callback_url"
     production:
       key:    "you api key"
       secret: "your secret"
       url:    "http://yoursite.com"
       callback: "http://localhost:3000/your_callback_url"
    
  • 演示

         #config/oauth/sina.yml
         development:
               key:    "you api key"
               secret: "your secret"
               url:    "http://yoursite.com"
               callback: "http://localhost:3000/syncs/sina/callback"
             production:
               key:    "you api key"
               secret: "your secret"
               url:    "http://yoursite.com"
               callback: "http://localhost:3000/syncs/sina/callback"
    
    
         #config/routes.rb
         match "syncs/:type/new" => "syncs#new", :as => :sync_new
         match "syncs/:type/callback" => "syncs#callback", :as => :sync_callback
    
         #app/controllers/syncs_controller.rb
         # encoding: UTF-8
         class SyncsController < ApplicationController
    
           before_filter :login_required
    
           def new
             client = OauthChina::Sina.new
             authorize_url = client.authorize_url
             Rails.cache.write(build_oauth_token_key(client.name, client.oauth_token), client.dump)
             redirect_to authorize_url
           end
    
           def callback
             client = OauthChina::Sina.load(Rails.cache.read(build_oauth_token_key(params[:type], params[:oauth_token])))
             client.authorize(:oauth_verifier => params[:oauth_verifier])
    
             results = client.dump
    
             if results[:access_token] && results[:access_token_secret]
               #在这里把access token and access token secret存到db
               #下次使用的时候:
               #client = OauthChina::Sina.load(:access_token => "xx", :access_token_secret => "xxx")
               #client.add_status("同步到新浪微薄..")
               flash[:notice] = "授权成功!"
             else
               flash[:notice] = "授权失败!"
             end
             redirect_to account_syncs_path
           end
    
           private
           def build_oauth_token_key(name, oauth_token)
             [name, oauth_token].join("_")
           end
    
         end
    
  • 注意

    系统时间要正确设置。否则会出现timstamps refused错误
    

#API文档

腾讯微博API文档:http://open.t.qq.com/resource.php?i=1,1
新浪微博API文档:http://open.t.sina.com.cn/wiki/index.php/API%E6%96%87%E6%A1%A3
豆瓣微博API文档:http://www.douban.com/service/apidoc/reference/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.