weibo_17up 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in weibo.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 liguang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Weibo
2
+
3
+ 新浪微博开放平台新版接口sdk 支持站外与站内应用 oauth2
4
+
5
+ ## 安装
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'weibo_17up'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ or
16
+
17
+ $bundle update
18
+
19
+ ## 使用
20
+
21
+ 配置文件: Rails.root + "/config/service.yml"
22
+
23
+ weibo:
24
+ production:
25
+ app_key: 'XXXXX446XXX'
26
+ app_secret: 'XXXXXX784a52b45XXXXXXXXXXXXXX'
27
+ redirect_uri: 'http://www.url.com/callback'
28
+
29
+ 授权:
30
+
31
+ 站外应用:
32
+
33
+ class WeiboController < ApplicationController
34
+ def connect
35
+ redirect_to Weibo::Oauth.authorize_url
36
+ end
37
+
38
+ def callback
39
+ access_token = Weibo::Oauth.get_access_token_by_code(params[:code])
40
+ render :text => access_token.inspect
41
+ end
42
+ end
43
+
44
+ 站内应用:
45
+
46
+ access_token = Weibo::Oauth.parse_signed_request(signed_request) #返回加密前的数据
47
+
48
+ rest api 请求:
49
+
50
+ client = Weibo::Client.new(weibo_access_token, weibo_uid)
51
+ client.update("Hi! what are you doing!")
52
+ client.upload("Hi! what are you doing!", pic_path)
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,108 @@
1
+ require "rest-client"
2
+
3
+ module Weibo
4
+ class Client
5
+ attr_reader :oauth
6
+
7
+ def initialize(access_token, weibo_uid = nil)
8
+ @oauth = Oauth.new(access_token)
9
+ @weibo_uid = weibo_uid
10
+ end
11
+
12
+ ################# 微博接口
13
+
14
+ def statuses_update(status, options = {})
15
+ self.oauth.post "statuses/update", :status => status
16
+ end
17
+
18
+ def statuses_upload(status, pic_path, options = {})
19
+ self.oauth.post 'statuses/upload', :status => status, :pic => File.new(File.expand_path(pic_path))
20
+ end
21
+
22
+ def statuses_upload_url_text(status, url, options = {})
23
+ default_params = { :status => status, :url => url }
24
+ self.oauth.post 'statuses/upload_url_text', default_params.merge(options)
25
+ end
26
+
27
+ def statuses_show(status_id, options = {})
28
+ self.oauth.get "statuses/show", :id => status_id
29
+ end
30
+
31
+ def statuses_user_timeline(options = {})
32
+ default_params = { :uid => @weibo_uid }
33
+ self.oauth.get "statuses/user_timeline", default_params.merge(options)
34
+ end
35
+
36
+ def statuses_querymid(id, options = {})
37
+ default_params = { :id => id, :type => 1 }
38
+ self.oauth.get "statuses/querymid", default_params.merge(options)
39
+ end
40
+
41
+ def statuses_queryid(mid, options = {})
42
+ default_params = { :mid => mid, :type => 1, :isBase62 => 1 }
43
+ self.oauth.get "statuses/queryid", default_params.merge(options)
44
+ end
45
+
46
+ def statuses_repost_by_me(options = {})
47
+ self.oauth.get "statuses/repost_by_me", options
48
+ end
49
+
50
+ def statuses_repost(status_id, options = {})
51
+ default_params = { :id => status_id }
52
+ self.oauth.post 'statuses/repost', default_params.merge(options)
53
+ end
54
+
55
+
56
+ ################# 关系接口
57
+
58
+ def friendships_followers(options = {})
59
+ default_params = { :cursor => 0, :count => 200, :uid => @weibo_uid }
60
+ self.oauth.get "friendships/followers", default_params.merge(options)
61
+ end
62
+
63
+ def friendships_friends(options = {})
64
+ default_params = { :cursor => 0, :count => 200, :uid => @weibo_uid }
65
+ self.oauth.get "friendships/friends", default_params.merge(options)
66
+ end
67
+
68
+ def friendships_friends_bilateral(uid, options = {})
69
+ default_params = { :page => 1, :count => 50, :sort => 0 }
70
+ self.oauth.get "friendships/friends/bilateral", default_params.merge(options)
71
+ end
72
+
73
+ def friendships_create(uid)
74
+ self.oauth.post("friendships/create", :uid => uid)
75
+ end
76
+
77
+ def friendships_friends_uids(options = {})
78
+ default_params = { :cursor => 0, :count => 200, :uid => @weibo_uid }
79
+ self.oauth.get "friendships/friends/ids", default_params.merge(options)
80
+ end
81
+
82
+ def friendships_destroy(uid)
83
+ self.oauth.post("friendships/destroy", :uid => uid)
84
+ end
85
+
86
+
87
+ ################# 用户接口
88
+
89
+ def users_show(options = {})
90
+ default_params = { :uid => @weibo_uid }
91
+ self.oauth.get "users/show", default_params.merge(options)
92
+ end
93
+
94
+
95
+ ################# 标签
96
+
97
+ def tags(options = {})
98
+ default_params = { :uid => @weibo_uid }
99
+ self.oauth.get "tags",default_params.merge(options)
100
+ end
101
+
102
+
103
+ # 1.0接口
104
+ def app_send(uids, title, content)
105
+ self.oauth.post("https://api.t.sina.com.cn/notice/app/send", :uids => uids.join(","), :title => title, :content => content)
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "yaml"
3
+
4
+ module Weibo
5
+ module Config
6
+
7
+ class << self
8
+ attr_reader :app_key, :app_secret, :redirect_uri
9
+
10
+ def load_config
11
+ filename = "#{Rails.root}/config/service.yml"
12
+ config = YAML.load(File.open(filename))[Rails.env]
13
+ @app_key, @app_secret, @redirect_uri = config['weibo']['app_key'], config['weibo']['app_secret'], config['weibo']['redirect_uri']
14
+ unless @app_key && @app_secret && @redirect_uri
15
+ puts "|>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
16
+ puts "|"
17
+ puts "Please configure weibo app_key app_secret redirect_uri in #{filename}."
18
+ puts "|"
19
+ puts "|>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
20
+ end
21
+ end
22
+
23
+ def temp_change_config(config)
24
+ o_app_key, o_app_secret, o_redirect_url = @app_key, @app_secret, @redirect_uri
25
+ @app_key, @app_secret, @redirect_uri = config.app_key, config.app_secret, config.redirect_uri
26
+ result = yield
27
+ @app_key, @app_secret, @redirect_uri = o_app_key, o_app_secret, o_redirect_url
28
+ result
29
+ end
30
+ end
31
+
32
+
33
+ end
34
+ end
@@ -0,0 +1,59 @@
1
+ require "digest/md5"
2
+ require "base64"
3
+
4
+ module Weibo
5
+ class Oauth
6
+ AUTHORIZE_URL = "https://api.weibo.com/oauth2/authorize"
7
+ ACCESS_TOKEN_URL = "https://api.weibo.com/oauth2/access_token"
8
+ API_URL = "https://api.weibo.com/2/"
9
+
10
+ def initialize(access_token)
11
+ @access_token = access_token
12
+ end
13
+
14
+ def get(path, parameters = {})
15
+ JSON.parse RestClient.get(api_url(path), :params => parameters, :Authorization => "OAuth2 #{@access_token}")
16
+ end
17
+
18
+ def post(path, parameters = {})
19
+ JSON.parse RestClient.post(api_url(path), parameters, :Authorization => "OAuth2 #{@access_token}")
20
+ end
21
+
22
+ def self.authorize_url(options = {})
23
+ url = AUTHORIZE_URL + "?client_id=#{Config.app_key}&redirect_uri=#{Config.redirect_uri}&response_type=code"
24
+ options.keys.length > 0 ? (url + "&" + options.map {|k,v| "#{k}=#{v}"}.join("&")) : url
25
+ end
26
+
27
+ def self.get_access_token_by_code(code)
28
+ response = RestClient.post(ACCESS_TOKEN_URL, :client_id => Config.app_key, :client_secret => Config.app_secret,
29
+ :grant_type => "authorization_code", :code => code, :redirect_uri => Config.redirect_uri)
30
+ JSON.parse(response)
31
+ end
32
+
33
+ ######################################################
34
+ #
35
+ # @param string $signed_request 应用框架在加载iframe时会通过向Canvas URL post的参数signed_request
36
+ #
37
+ #####################################################
38
+ def self.parse_signed_request(signed_request)
39
+ encoded_sig, payload = signed_request.split(".")
40
+ sig = Base64.decode64_url(encoded_sig)
41
+ begin
42
+ data = JSON.parse(Base64.decode64_url(payload))
43
+ rescue Exception => e
44
+ return nil
45
+ end
46
+ return nil if data["algorithm"].upcase != "HMAC-SHA256"
47
+
48
+ expected_sig = OpenSSL::HMAC.digest("sha256", Config.app_secret, payload)
49
+ (sig != expected_sig) ? nil : data
50
+ end
51
+
52
+ private
53
+ def api_url(path)
54
+ path = path.gsub /^\//, ""
55
+ path.starts_with?("http") ? (path + ".json") : "#{API_URL}#{path}.json"
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Weibo
3
+ class Railtie < ::Rails::Railtie
4
+ initializer "加载rails环境下的config" do
5
+ Weibo::Config.load_config
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Weibo
2
+ VERSION = "0.4.6"
3
+ end
data/lib/weibo.rb ADDED
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/weibo/version')
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/weibo/config')
4
+ require File.expand_path(File.dirname(__FILE__) + '/weibo/oauth')
5
+ require File.expand_path(File.dirname(__FILE__) + '/weibo/client')
6
+
7
+ if defined?(Rails)
8
+ require File.expand_path(File.dirname(__FILE__) + '/weibo/railtie')
9
+ end
10
+
data/weibo.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/weibo/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["veggie"]
6
+ gem.email = ["kkxlkkxllb@gmail.com"]
7
+ gem.description = %q{新浪微博oauth2}
8
+ gem.summary = %q{新浪微博oauth2}
9
+ gem.homepage = "https://github.com/kkxlkkxllb/weibo_17up"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "weibo_17up"
15
+ gem.require_paths = ["lib"]
16
+
17
+ gem.add_dependency 'rest-client'
18
+
19
+ gem.version = Weibo::VERSION
20
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weibo_17up
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.6
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - veggie
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: &70226629015920 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70226629015920
25
+ description: 新浪微博oauth2
26
+ email:
27
+ - kkxlkkxllb@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - Gemfile
33
+ - LICENSE
34
+ - README.md
35
+ - Rakefile
36
+ - lib/weibo.rb
37
+ - lib/weibo/client.rb
38
+ - lib/weibo/config.rb
39
+ - lib/weibo/oauth.rb
40
+ - lib/weibo/railtie.rb
41
+ - lib/weibo/version.rb
42
+ - weibo.gemspec
43
+ homepage: https://github.com/kkxlkkxllb/weibo_17up
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.15
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: 新浪微博oauth2
67
+ test_files: []