Skip to content
Browse files

Weibo OAuth2 strategy for OmniAuth 1.0. Add to github.

  • Loading branch information...
0 parents commit d38b96df28f6e8601c573da8b5208c36ba807e0b @beenhero committed Mar 14, 2012
Showing with 163 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +2 −0 Gemfile
  3. +81 −0 README.md
  4. +2 −0 lib/omniauth-weibo-oauth2.rb
  5. +5 −0 lib/omniauth-weibo-oauth2/version.rb
  6. +50 −0 lib/omniauth/strategies/weibo.rb
  7. +20 −0 omniauth-weibo-oauth2.gemspec
3 .gitignore
@@ -0,0 +1,3 @@
+.bundle
+*.swp
+Gemfile.lock
2 Gemfile
@@ -0,0 +1,2 @@
+source :rubygems
+gemspec
81 README.md
@@ -0,0 +1,81 @@
+# OmniAuth Weibo OAuth2
+
+Weibo OAuth2 Strategy for OmniAuth 1.0.
+
+Read Weibo OAuth2 docs for more details: http://open.weibo.com/wiki/Oauth2
+
+## Installing
+
+Add to your `Gemfile`:
+
+```ruby
+gem 'omniauth-weibo-oauth2'
+```
+
+Then `bundle install`.
+
+Or install it yourself as:
+
+ $ gem install omniauth-weibo-oauth2
+
+## Usage
+
+`OmniAuth::Strategies::Weibo` is simply a Rack middleware. Read the OmniAuth 1.0 docs for detailed instructions: https://github.com/intridea/omniauth.
+
+Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
+
+```ruby
+Rails.application.config.middleware.use OmniAuth::Builder do
+ provider :weibo, ENV['WEIBO_KEY'], ENV['WEIBO_SECRET']
+end
+```
+
+## Authentication Hash
+
+Here's an example *Authentication Hash* available in `request.env['omniauth.auth']`:
+
+```ruby
+{
+ :provider => 'weibo',
+ :uid => '1234567890',
+ :info => {
+ :nickname => 'beenhero',
+ :name => 'beenhero',
+ :location => '浙江 杭州',
+ :image => 'http://tp4.sinaimg.cn/1640099215/50/1287016234/1',
+ :description => '移步twitter@beenhero',
+ :urls => { :Blog => 'http://beenhero.com'
+ :Weibo => 'http://weibo.com/beenhero'
+ },
+ },
+ :credentials => {
+ :token => '2.00JjgzmBd7F...', # OAuth 2.0 access_token, which you may wish to store
+ :expires_at => 1331780640, # when the access token expires (if it expires)
+ :expires => true # if you request `offline_access` this will be false
+ },
+ :extra => {
+ :raw_info => {
+ ... # data from /2/users/show.json, check by yourself
+ }
+ }
+}
+```
+*PS.* Built and tested on MRI Ruby 1.9.3
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
+
+## License
+
+Copyright (c) 2012 by Bin He
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 lib/omniauth-weibo-oauth2.rb
@@ -0,0 +1,2 @@
+require "omniauth-weibo-oauth2/version"
+require 'omniauth/strategies/weibo'
5 lib/omniauth-weibo-oauth2/version.rb
@@ -0,0 +1,5 @@
+module OmniAuth
+ module WeiboOauth2
+ VERSION = "0.1.0"
+ end
+end
50 lib/omniauth/strategies/weibo.rb
@@ -0,0 +1,50 @@
+require "omniauth-oauth2"
+
+module OmniAuth
+ module Strategies
+ class Weibo < OmniAuth::Strategies::OAuth2
+ option :client_options, {
+ :site => "https://api.weibo.com",
+ :authorize_url => "/oauth2/authorize",
+ :token_url => "/oauth2/access_token"
+ }
+ option :token_params, {
+ :parse => :json
+ }
+
+ uid do
+ raw_info['id']
+ end
+
+ info do
+ {
+ :nickname => raw_info['screen_name'],
+ :name => raw_info['name'],
+ :location => raw_info['location'],
+ :image => raw_info['profile_image_url'],
+ :description => raw_info['description'],
+ :urls => {
+ 'Blog' => raw_info['url'],
+ 'Weibo' => raw_info['domain'].present?? "http://weibo.com/#{raw_info['domain']}" : "http://weibo.com/u/#{raw_info['id']}",
+ }
+ }
+ end
+
+ extra do
+ {
+ :raw_info => raw_info
+ }
+ end
+
+ def raw_info
+ access_token.options[:mode] = :query
+ access_token.options[:param_name] = 'access_token'
+ @uid ||= access_token.get('/2/account/get_uid.json').parsed["uid"]
+ @raw_info ||= access_token.get("/2/users/show.json", :params => {:uid => @uid}).parsed
+ end
+
+ end
+ end
+end
+
+OmniAuth.config.add_camelization "weibo", "Weibo"
20 omniauth-weibo-oauth2.gemspec
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/omniauth-weibo-oauth2/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = "Bin He"
+ gem.email = "beenhero@gmail.com"
+ gem.description = %q{OmniAuth Oauth2 strategy for weibo.com.}
+ gem.summary = %q{OmniAuth Oauth2 strategy for weibo.com.}
+ gem.homepage = "https://github.com/beenhero/omniauth-weibo-oauth2"
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = "omniauth-weibo-oauth2"
+ gem.require_paths = ["lib"]
+ gem.version = OmniAuth::WeiboOauth2::VERSION
+
+ gem.add_dependency 'omniauth', '~> 1.0'
+ gem.add_dependency 'omniauth-oauth2', '~> 1.0'
+end

0 comments on commit d38b96d

Please sign in to comment.
Something went wrong with that request. Please try again.