weibo_2 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -21
- data/lib/weibo_2.rb +1 -0
- data/lib/weibo_2/api/v2/statuses.rb +4 -0
- data/lib/weibo_2/client.rb +47 -31
- data/lib/weibo_2/errors.rb +20 -0
- data/lib/weibo_2/version.rb +1 -1
- data/spec/client_spec.rb +7 -7
- data/weibo_2.gemspec +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ccf4842b4765614e661c44d6da2708cff3d598aa
|
4
|
+
data.tar.gz: 73225359bbf7d3505761ba4ae1721fa4a8dc9703
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0eb62b30ba56090a4903405d603db120c03772bb73c04cde9e2071a7ed997d397cd51c068372ad30a1f360e67fb40a3d1f9e85020990c43cfd3eae8abac8190
|
7
|
+
data.tar.gz: 7ff4b89a4d248d0713d68ac2540906405ea40737adf072d034b28bf3465ec4f24bb83d739800c129adb193f3d12f1401652615b6cb29f12761341641924f3e86
|
data/README.md
CHANGED
@@ -11,7 +11,7 @@ WeioOAuth2 is a Ruby gem that provides a wrapper for interacting with sina weibo
|
|
11
11
|
3. weibo app API secret
|
12
12
|
|
13
13
|
## Installation
|
14
|
-
|
14
|
+
|
15
15
|
I call it weibo_2 because someone else took the name weibo_oauth2.
|
16
16
|
|
17
17
|
```bash
|
@@ -37,41 +37,41 @@ It should work.
|
|
37
37
|
```ruby
|
38
38
|
WeiboOAuth2::Config.api_key = YOUR_KEY
|
39
39
|
WeiboOAuth2::Config.api_secret = YOUR_SECRET
|
40
|
-
WeiboOAuth2::Config.redirect_uri = YOUR_CALLBACK_URL
|
40
|
+
WeiboOAuth2::Config.redirect_uri = YOUR_CALLBACK_URL
|
41
41
|
```
|
42
42
|
|
43
43
|
If you are developing in your localhost, you can set YOUR_CALLBACK_URL as 'http://127.0.0.1/callback' something. Then set your weibo app account's callback URL as this URL too. Weibo will call the URL using GET method, which will then enable you to retrieve the authorization code.
|
44
|
-
|
44
|
+
|
45
45
|
```ruby
|
46
46
|
client = WeiboOAuth2::Client.new
|
47
47
|
```
|
48
|
-
|
48
|
+
|
49
49
|
Or you can pass the key and secret to new a client if you did not set WeiboOAuth2::Config
|
50
|
-
|
50
|
+
|
51
51
|
Redirect to this URL. If user grants you permission, then you will get the authorization code.
|
52
|
-
|
52
|
+
|
53
53
|
```ruby
|
54
54
|
client.authorize_url
|
55
55
|
```
|
56
|
-
|
57
|
-
In your callback handling method, you should add something like the following,
|
58
|
-
|
56
|
+
|
57
|
+
In your callback handling method, you should add something like the following,
|
58
|
+
|
59
59
|
```ruby
|
60
60
|
client.auth_code.get_token(params[:code])
|
61
61
|
```
|
62
|
-
|
62
|
+
|
63
63
|
which will give permission to your client.
|
64
|
-
|
64
|
+
|
65
65
|
2. How to post a status with picture? (or call other interfaces)
|
66
|
-
|
66
|
+
|
67
67
|
Simply update a status
|
68
|
-
|
68
|
+
|
69
69
|
```ruby
|
70
70
|
client.statuses.update(params[:status])
|
71
71
|
```
|
72
|
-
|
72
|
+
|
73
73
|
Upload a picture.
|
74
|
-
|
74
|
+
|
75
75
|
```ruby
|
76
76
|
pic = params[:file].delete(:tempfile)
|
77
77
|
client.statuses.upload(params[:status], pic, params[:file])
|
@@ -80,10 +80,10 @@ It should work.
|
|
80
80
|
pass params[:file] into upload method as options could help weibo_2 to build post body, useful options as:
|
81
81
|
* filename, filename with extension of the uploading file, example 'pic.jpg'
|
82
82
|
* type, mime type of the uploading file, example 'image/jpeg'
|
83
|
-
|
83
|
+
|
84
84
|
## Setting up SSL certificates
|
85
|
-
|
86
|
-
This gem using [faraday](https://github.com/technoweenie/faraday) for connection, which supports ssl. According to [this article](https://github.com/
|
85
|
+
|
86
|
+
This gem using [faraday](https://github.com/technoweenie/faraday) for connection, which supports ssl. According to [this article](https://github.com/lostisland/faraday/wiki/Setting-up-SSL-certificates), you can do as following to support ssl connection.
|
87
87
|
|
88
88
|
### Ubuntu
|
89
89
|
|
@@ -108,7 +108,7 @@ For Fedora and CentOS, use the path and file `/etc/pki/tls/certs/ca-bundle.crt`
|
|
108
108
|
## Integrate with Devise and omniauth
|
109
109
|
|
110
110
|
1. Install gems in Gemfile
|
111
|
-
|
111
|
+
|
112
112
|
```ruby
|
113
113
|
gem 'devise'
|
114
114
|
gem 'omniauth'
|
@@ -122,8 +122,8 @@ For Fedora and CentOS, use the path and file `/etc/pki/tls/certs/ca-bundle.crt`
|
|
122
122
|
```ruby
|
123
123
|
config.omniauth :weibo, 'key', 'secret', :scope => 'user,public_repo'
|
124
124
|
```
|
125
|
-
|
126
|
-
3. Then you should handle omini callback controller by yourself, there is sample project show how to integrate devise and omniauth you can follow [devise-omniauth-example](https://github.com/holden/devise-omniauth-example)
|
125
|
+
|
126
|
+
3. Then you should handle omini callback controller by yourself, there is sample project show how to integrate devise and omniauth you can follow [devise-omniauth-example](https://github.com/holden/devise-omniauth-example)
|
127
127
|
|
128
128
|
4. After get the callback data, you will see `env['omniauth.auth']['credentials']` has the value `token` and `expires_at`, store them into session or record,now you can use WeiboOAuth2 in anywhere:
|
129
129
|
|
data/lib/weibo_2.rb
CHANGED
@@ -28,6 +28,10 @@ module WeiboOAuth2
|
|
28
28
|
hashie get("statuses/user_timeline/ids.json", :params => opt)
|
29
29
|
end
|
30
30
|
|
31
|
+
def timeline_batch(opt={})
|
32
|
+
hashie get("statuses/timeline_batch.json", :params => opt)
|
33
|
+
end
|
34
|
+
|
31
35
|
def repost_timeline(id, opt={})
|
32
36
|
hashie get("statuses/repost_timeline.json", :params => {:id => id}.merge(opt))
|
33
37
|
end
|
data/lib/weibo_2/client.rb
CHANGED
@@ -4,7 +4,7 @@ module WeiboOAuth2
|
|
4
4
|
class Client < OAuth2::Client
|
5
5
|
|
6
6
|
attr_accessor :access_token
|
7
|
-
|
7
|
+
|
8
8
|
def initialize(client_id='', client_secret='', opts={}, &block)
|
9
9
|
client_id = WeiboOAuth2::Config.api_key if client_id.empty?
|
10
10
|
client_secret = WeiboOAuth2::Config.api_secret if client_secret.empty?
|
@@ -26,7 +26,23 @@ module WeiboOAuth2
|
|
26
26
|
client.get_token_from_hash(hash)
|
27
27
|
|
28
28
|
client
|
29
|
-
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def request(verb, url, opts = {})
|
32
|
+
super
|
33
|
+
rescue OAuth2::Error => e
|
34
|
+
error_code = e.response.parsed["error_code"]
|
35
|
+
case error_code
|
36
|
+
when 21315, 21316, 21317, 21327, 21332
|
37
|
+
raise WeiboOAuth2::Errors::UnauthorizedError.new(e.response.parsed)
|
38
|
+
when 10022, 10023, 10024, 20016, 20505
|
39
|
+
raise WeiboOAuth2::Errors::RateLimitedError.new(e.response.parsed)
|
40
|
+
when 10013, 10014, 20508, 21301
|
41
|
+
raise WeiboOAuth2::Errors::PermissionError.new(e.response.parsed)
|
42
|
+
else
|
43
|
+
raise WeiboOAuth2::Errors::GeneralError.new(e.response.parsed)
|
44
|
+
end
|
45
|
+
end
|
30
46
|
|
31
47
|
def authorize_url(params={})
|
32
48
|
params[:client_id] = @id unless params[:client_id]
|
@@ -34,78 +50,78 @@ module WeiboOAuth2
|
|
34
50
|
params[:redirect_uri] = WeiboOAuth2::Config.redirect_uri unless params[:redirect_uri]
|
35
51
|
super
|
36
52
|
end
|
37
|
-
|
53
|
+
|
38
54
|
def get_token(params, access_token_opts={})
|
39
55
|
params = params.merge({:parse => :json})
|
40
56
|
access_token_opts = access_token_opts.merge({:header_format => "OAuth2 %s", :param_name => "access_token"})
|
41
57
|
super
|
42
58
|
end
|
43
|
-
|
59
|
+
|
44
60
|
def get_and_restore_token(params, access_token_opts={})
|
45
61
|
@access_token = get_token(params, access_token_opts={})
|
46
62
|
end
|
47
|
-
|
63
|
+
|
48
64
|
def get_token_from_hash(hash)
|
49
65
|
access_token = hash.delete(:access_token) || hash.delete('access_token')
|
50
66
|
@access_token = WeiboOAuth2::AccessToken.new( self, access_token, hash.merge(:header_format => 'OAuth2 %s', :param_name => 'access_token') )
|
51
67
|
end
|
52
|
-
|
68
|
+
|
53
69
|
def authorized?
|
54
70
|
@access_token && @access_token.validated?
|
55
71
|
end
|
56
|
-
|
72
|
+
|
57
73
|
def users
|
58
74
|
@users ||= WeiboOAuth2::Api::V2::Users.new(@access_token) if @access_token
|
59
75
|
end
|
60
|
-
|
76
|
+
|
61
77
|
def statuses
|
62
78
|
@statues ||= WeiboOAuth2::Api::V2::Statuses.new(@access_token) if @access_token
|
63
79
|
end
|
64
|
-
|
80
|
+
|
65
81
|
def comments
|
66
82
|
@comments ||= WeiboOAuth2::Api::V2::Comments.new(@access_token) if @access_token
|
67
83
|
end
|
68
|
-
|
84
|
+
|
69
85
|
def friendships
|
70
86
|
@friendships ||= WeiboOAuth2::Api::V2::Friendships.new(@access_token) if @access_token
|
71
87
|
end
|
72
|
-
|
88
|
+
|
73
89
|
def account
|
74
|
-
@account ||= WeiboOAuth2::Api::V2::Account.new(@access_token) if @access_token
|
90
|
+
@account ||= WeiboOAuth2::Api::V2::Account.new(@access_token) if @access_token
|
75
91
|
end
|
76
|
-
|
92
|
+
|
77
93
|
def favorites
|
78
|
-
@favorites ||= WeiboOAuth2::Api::V2::Favorites.new(@access_token) if @access_token
|
94
|
+
@favorites ||= WeiboOAuth2::Api::V2::Favorites.new(@access_token) if @access_token
|
79
95
|
end
|
80
|
-
|
96
|
+
|
81
97
|
def trends
|
82
|
-
@trends ||= WeiboOAuth2::Api::V2::Trends.new(@access_token) if @access_token
|
98
|
+
@trends ||= WeiboOAuth2::Api::V2::Trends.new(@access_token) if @access_token
|
83
99
|
end
|
84
|
-
|
100
|
+
|
85
101
|
def tags
|
86
|
-
@tags ||= WeiboOAuth2::Api::V2::Tags.new(@access_token) if @access_token
|
102
|
+
@tags ||= WeiboOAuth2::Api::V2::Tags.new(@access_token) if @access_token
|
87
103
|
end
|
88
|
-
|
104
|
+
|
89
105
|
def register
|
90
|
-
@register ||= WeiboOAuth2::Api::V2::Register.new(@access_token) if @access_token
|
106
|
+
@register ||= WeiboOAuth2::Api::V2::Register.new(@access_token) if @access_token
|
91
107
|
end
|
92
|
-
|
108
|
+
|
93
109
|
def search
|
94
|
-
@search ||= WeiboOAuth2::Api::V2::Search.new(@access_token) if @access_token
|
110
|
+
@search ||= WeiboOAuth2::Api::V2::Search.new(@access_token) if @access_token
|
95
111
|
end
|
96
|
-
|
112
|
+
|
97
113
|
def short_url
|
98
|
-
@short_url ||= WeiboOAuth2::Api::V2::ShortUrl.new(@access_token) if @access_token
|
114
|
+
@short_url ||= WeiboOAuth2::Api::V2::ShortUrl.new(@access_token) if @access_token
|
99
115
|
end
|
100
|
-
|
116
|
+
|
101
117
|
def suggestions
|
102
|
-
@suggestions ||= WeiboOAuth2::Api::V2::Suggestions.new(@access_token) if @access_token
|
118
|
+
@suggestions ||= WeiboOAuth2::Api::V2::Suggestions.new(@access_token) if @access_token
|
103
119
|
end
|
104
|
-
|
120
|
+
|
105
121
|
def remind
|
106
|
-
@remind ||= WeiboOAuth2::Api::V2::Remind.new(@access_token) if @access_token
|
122
|
+
@remind ||= WeiboOAuth2::Api::V2::Remind.new(@access_token) if @access_token
|
107
123
|
end
|
108
|
-
|
124
|
+
|
109
125
|
def auth_code
|
110
126
|
@auth_code ||= WeiboOAuth2::Strategy::AuthCode.new(self)
|
111
127
|
end
|
@@ -117,6 +133,6 @@ module WeiboOAuth2
|
|
117
133
|
def location
|
118
134
|
@location ||= WeiboOAuth2::Api::V2::Location.new(@access_token) if @access_token
|
119
135
|
end
|
120
|
-
|
121
|
-
end
|
136
|
+
|
137
|
+
end
|
122
138
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module WeiboOAuth2
|
2
|
+
module Errors
|
3
|
+
class WeiboError < StandardError
|
4
|
+
attr_reader :data
|
5
|
+
def initialize(data)
|
6
|
+
@data = data
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class GeneralError < WeiboError
|
12
|
+
end
|
13
|
+
|
14
|
+
class RateLimitedError < WeiboError
|
15
|
+
end
|
16
|
+
|
17
|
+
class UnauthorizedError < WeiboError
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/weibo_2/version.rb
CHANGED
data/spec/client_spec.rb
CHANGED
@@ -15,23 +15,23 @@ describe WeiboOAuth2::Client do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'should get authorize_url' do
|
18
|
-
authorize_url = 'https://api.weibo.com/oauth2/authorize?client_id=abc&
|
18
|
+
authorize_url = 'https://api.weibo.com/oauth2/authorize?client_id=abc&redirect_uri=https%3A%2F%2Fexample.com%2Fcallback&response_type=code'
|
19
19
|
subject.authorize_url.should == authorize_url
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'should leave Faraday::Connection#ssl unset' do
|
23
|
-
subject.connection.ssl.should
|
23
|
+
subject.connection.ssl.empty?.should be_truthy
|
24
24
|
end
|
25
25
|
|
26
26
|
it "defaults raise_errors to true" do
|
27
|
-
subject.options[:raise_errors].should
|
27
|
+
subject.options[:raise_errors].should be_truthy
|
28
28
|
end
|
29
29
|
|
30
30
|
it "allows true/false for raise_errors option" do
|
31
31
|
client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :raise_errors => false)
|
32
|
-
client.options[:raise_errors].should
|
32
|
+
client.options[:raise_errors].should be_falsey
|
33
33
|
client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :raise_errors => true)
|
34
|
-
client.options[:raise_errors].should
|
34
|
+
client.options[:raise_errors].should be_truthy
|
35
35
|
end
|
36
36
|
|
37
37
|
it "allows get/post for access_token_method option" do
|
@@ -40,5 +40,5 @@ describe WeiboOAuth2::Client do
|
|
40
40
|
client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :access_token_method => :post)
|
41
41
|
client.options[:access_token_method].should == :post
|
42
42
|
end
|
43
|
-
|
44
|
-
end
|
43
|
+
|
44
|
+
end
|
data/weibo_2.gemspec
CHANGED
@@ -21,5 +21,5 @@ Gem::Specification.new do |gem|
|
|
21
21
|
gem.add_runtime_dependency 'oauth2', "~> 0.9.1"
|
22
22
|
gem.add_runtime_dependency 'hashie', "~> 2.0.4"
|
23
23
|
gem.add_runtime_dependency 'multi_json' , "~> 1"
|
24
|
-
gem.add_runtime_dependency 'rest-client', "~> 1.
|
24
|
+
gem.add_runtime_dependency 'rest-client', "~> 1.7.3"
|
25
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weibo_2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- simsicon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -72,14 +72,14 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 1.
|
75
|
+
version: 1.7.3
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 1.
|
82
|
+
version: 1.7.3
|
83
83
|
description: |-
|
84
84
|
WeioOAuth2 is a Ruby gem that provides a wrapper for interacting with sina weibo's v2 API,
|
85
85
|
which is currently the latest version. The output data format is Hashie::Mash
|
@@ -117,6 +117,7 @@ files:
|
|
117
117
|
- lib/weibo_2/base.rb
|
118
118
|
- lib/weibo_2/client.rb
|
119
119
|
- lib/weibo_2/config.rb
|
120
|
+
- lib/weibo_2/errors.rb
|
120
121
|
- lib/weibo_2/oauth.rb
|
121
122
|
- lib/weibo_2/strategy/auth_code.rb
|
122
123
|
- lib/weibo_2/version.rb
|
@@ -141,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
142
|
version: '0'
|
142
143
|
requirements: []
|
143
144
|
rubyforge_project:
|
144
|
-
rubygems_version: 2.
|
145
|
+
rubygems_version: 2.4.5
|
145
146
|
signing_key:
|
146
147
|
specification_version: 4
|
147
148
|
summary: A oauth2 gem for weibo
|