weibo_2 0.0.4 → 0.0.5
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/README.md +78 -1
- data/example/.sass-cache/faf9751221db0b5ce76d45351b9591c3bfc59441/screen.sassc +0 -0
- data/example/Gemfile +2 -1
- data/example/example.rb +34 -7
- data/example/views/index.haml +36 -1
- data/example/views/screen.sass +13 -0
- data/lib/weibo_2/access_token.rb +10 -0
- data/lib/weibo_2/api/v2/statuses.rb +4 -4
- data/lib/weibo_2/client.rb +1 -1
- data/lib/weibo_2/version.rb +1 -1
- data/lib/weibo_2.rb +15 -14
- metadata +3 -2
data/README.md
CHANGED
@@ -1,3 +1,80 @@
|
|
1
|
+
Note: I got a lot of inspiration from [this gem](https://github.com/ballantyne/weibo) and [this gem](https://github.com/acenqiu/weibo2). The first one will soon be deprecated since weibo is planning to switch to oauth2. The second one has not been fully implemented and is still lacking some features (but a nice try nevertheless). Thanks Scott and acenqiu.
|
2
|
+
|
1
3
|
# WeiboOAuth2
|
2
4
|
|
3
|
-
|
5
|
+
WeioOAuth2 is a Ruby gem that provides a wrapper for interacting with sina weibo's v2 [API](http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2), which is currently the latest version. Check out the link if you are interested in browsing the details. The output data format is Hashie::Mash, check out [here](https://github.com/intridea/hashie#mash). If you are not familiar with oauth2, I recommend that you read this [article](http://open.weibo.com/wiki/Oauth2).
|
6
|
+
|
7
|
+
## Requirements
|
8
|
+
|
9
|
+
1. weibo account
|
10
|
+
2. weibo app API key
|
11
|
+
3. weibo app API secret
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
I call it weibo_2 because someone else took the name weibo_oauth2.
|
16
|
+
|
17
|
+
```bash
|
18
|
+
$ gem install weibo_2
|
19
|
+
```
|
20
|
+
|
21
|
+
## Basic Usage
|
22
|
+
|
23
|
+
The example written with sinatra in this directory shows how to ask for oauth2 permission, get the token and send status with picture. It should cover basic usage in all ruby apps. You can visit http://weibo-oauth2-example.herokuapp.com/ to see the demo.
|
24
|
+
|
25
|
+
update: Sorry, the api key I used for this demo is still in the process of auditing by weibo side, it's not available now for connecting. But you can apply your own api key, clone the example in this directory, then type
|
26
|
+
|
27
|
+
```bash
|
28
|
+
$ KEY=change_this_to_your_key SECRET=change_this_to_your_secret REDIR_URI=change_this_to_your_redir_uri ruby example.rb
|
29
|
+
```
|
30
|
+
It should work.
|
31
|
+
|
32
|
+
|
33
|
+
1. How to get the token?
|
34
|
+
|
35
|
+
OAuth2 is simpler than its first version. In order to get the access token, you first need to get an Authorization Code through a callback request. Now use the following code to get the token.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
WeiboOAuth2::Config.api_key = YOUR_KEY
|
39
|
+
WeiboOAuth2::Config.api_secret = YOUR_SECRET
|
40
|
+
WeiboOAuth2::Config.redirect_uri = YOUR_CALLBACK_URL
|
41
|
+
```
|
42
|
+
|
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
|
+
|
45
|
+
```ruby
|
46
|
+
client = WeiboOAuth2::Client.new
|
47
|
+
```
|
48
|
+
|
49
|
+
Or you can pass the key and secret to new a client if you did not set WeiboOAuth2::Config
|
50
|
+
|
51
|
+
Redirect to this URL. If user grants you permission, then you will get the authorization code.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
client.authorize_url
|
55
|
+
```
|
56
|
+
|
57
|
+
In your callback handling method, you should add something like the following,
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
client.auth_code.get_token(params[:code])
|
61
|
+
```
|
62
|
+
|
63
|
+
which will give permission to your client.
|
64
|
+
|
65
|
+
2. How to post a status with picture? (or call other interfaces)
|
66
|
+
|
67
|
+
Simply update a status
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
client.statuses.update(params[:status])
|
71
|
+
```
|
72
|
+
|
73
|
+
Upload a picture
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
tmpfile = params[:file][:tempfile]
|
77
|
+
pic = File.open(tmpfile.path)
|
78
|
+
client.statuses.upload(params[:status], pic)
|
79
|
+
```
|
80
|
+
|
Binary file
|
data/example/Gemfile
CHANGED
data/example/example.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'weibo_2'
|
2
|
+
require 'time-ago-in-words'
|
2
3
|
|
3
4
|
%w(rubygems bundler).each { |dependency| require dependency }
|
4
5
|
Bundler.setup
|
@@ -12,11 +13,21 @@ WeiboOAuth2::Config.redirect_uri = ENV['REDIR_URI']
|
|
12
13
|
client = WeiboOAuth2::Client.new
|
13
14
|
|
14
15
|
get '/' do
|
16
|
+
puts '* ' * 80
|
17
|
+
puts session.inspect
|
18
|
+
puts !client.authorized?
|
15
19
|
if session[:access_token] && !client.authorized?
|
16
|
-
client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
|
20
|
+
token = client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
|
21
|
+
puts token.inspect
|
22
|
+
if token.expired?
|
23
|
+
reset_session
|
24
|
+
redirect 'connect'
|
25
|
+
return
|
26
|
+
end
|
17
27
|
end
|
18
28
|
if session[:uid]
|
19
29
|
@user = client.users.show_by_uid(session[:uid])
|
30
|
+
@statuses = client.statuses
|
20
31
|
end
|
21
32
|
haml :index
|
22
33
|
end
|
@@ -30,21 +41,37 @@ get '/callback' do
|
|
30
41
|
session[:uid] = access_token.params["uid"]
|
31
42
|
session[:access_token] = access_token.token
|
32
43
|
session[:expires_at] = access_token.expires_at
|
33
|
-
puts access_token.params
|
34
|
-
puts access_token.inspect
|
35
|
-
puts session[:uid]
|
36
44
|
@user = client.users.show_by_uid(session[:uid].to_i)
|
37
45
|
redirect '/'
|
38
46
|
end
|
39
47
|
|
40
48
|
get '/logout' do
|
41
|
-
|
42
|
-
session[:access_token] = nil
|
43
|
-
session[:expires_at] = nil
|
49
|
+
reset_session
|
44
50
|
redirect '/'
|
45
51
|
end
|
46
52
|
|
47
53
|
get '/screen.css' do
|
48
54
|
content_type 'text/css'
|
49
55
|
sass :screen
|
56
|
+
end
|
57
|
+
|
58
|
+
post '/update' do
|
59
|
+
statuses = client.statuses
|
60
|
+
|
61
|
+
unless params[:file] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename])
|
62
|
+
statuses.update(params[:status])
|
63
|
+
else
|
64
|
+
pic = File.open(tmpfile.path)
|
65
|
+
statuses.upload(params[:status], pic)
|
66
|
+
end
|
67
|
+
|
68
|
+
redirect '/'
|
69
|
+
end
|
70
|
+
|
71
|
+
helpers do
|
72
|
+
def reset_session
|
73
|
+
session[:uid] = nil
|
74
|
+
session[:access_token] = nil
|
75
|
+
session[:expires_at] = nil
|
76
|
+
end
|
50
77
|
end
|
data/example/views/index.haml
CHANGED
@@ -4,4 +4,39 @@
|
|
4
4
|
hello! you are
|
5
5
|
=@user.screen_name
|
6
6
|
|
7
|
-
|
7
|
+
%h3
|
8
|
+
make a weibo!
|
9
|
+
%form{:action => "/update", :method => "POST", :enctype => "multipart/form-data"}
|
10
|
+
%textarea{:id => 'status', :name => "status", :cols => "62", :rows => "5"}
|
11
|
+
%br
|
12
|
+
%input{:id => 'file', :name => 'file', :type => 'file'}
|
13
|
+
%br
|
14
|
+
%input{:type => "submit", :value => "Send"}
|
15
|
+
|
16
|
+
%br
|
17
|
+
%br
|
18
|
+
%br
|
19
|
+
%h3
|
20
|
+
home timeline
|
21
|
+
.statuses
|
22
|
+
-@statuses.friends_timeline.statuses.each do |status|
|
23
|
+
.status
|
24
|
+
%p.text
|
25
|
+
=status.text
|
26
|
+
|
27
|
+
|
28
|
+
%br
|
29
|
+
-if status.bmiddle_pic
|
30
|
+
%img{:src => status.bmiddle_pic, :alt => "", :class => 'pic'}
|
31
|
+
%br
|
32
|
+
.time_and_by
|
33
|
+
=Time.parse(status.created_at).ago_in_words
|
34
|
+
by
|
35
|
+
=status.user.screen_name
|
36
|
+
from
|
37
|
+
=status.source
|
38
|
+
%br
|
39
|
+
%br
|
40
|
+
%br
|
41
|
+
%br
|
42
|
+
|
data/example/views/screen.sass
CHANGED
@@ -28,12 +28,12 @@ module WeiboOAuth2
|
|
28
28
|
hashie get("statuses/user_timeline/ids.json", :params => opt)
|
29
29
|
end
|
30
30
|
|
31
|
-
def repost_timeline(opt={})
|
32
|
-
hashie get("statuses/repost_timeline.json", :params => opt)
|
31
|
+
def repost_timeline(id, opt={})
|
32
|
+
hashie get("statuses/repost_timeline.json", :params => {:id => id}.merge(opt))
|
33
33
|
end
|
34
34
|
|
35
|
-
def repost_timeline_ids(opt={})
|
36
|
-
hashie get("statuses/repost_timeline/ids.json", :params => opt)
|
35
|
+
def repost_timeline_ids(id, opt={})
|
36
|
+
hashie get("statuses/repost_timeline/ids.json", :params => {:id => id}.merge(opt))
|
37
37
|
end
|
38
38
|
|
39
39
|
def repost_by_me(opt={})
|
data/lib/weibo_2/client.rb
CHANGED
@@ -35,7 +35,7 @@ module WeiboOAuth2
|
|
35
35
|
:header_format => "OAuth2 %s",
|
36
36
|
:param_name => "access_token"}
|
37
37
|
|
38
|
-
@access_token =
|
38
|
+
@access_token = WeiboOAuth2::AccessToken.new(self, access_token, opts)
|
39
39
|
end
|
40
40
|
|
41
41
|
def authorized?
|
data/lib/weibo_2/version.rb
CHANGED
data/lib/weibo_2.rb
CHANGED
@@ -2,17 +2,18 @@ require "weibo_2/version"
|
|
2
2
|
require "weibo_2/config"
|
3
3
|
require "weibo_2/base"
|
4
4
|
require "weibo_2/client"
|
5
|
-
require "weibo_2/
|
6
|
-
require "weibo_2/api/v2/
|
7
|
-
require "weibo_2/api/v2/
|
8
|
-
require "weibo_2/api/v2/
|
9
|
-
require "weibo_2/api/v2/
|
10
|
-
require "weibo_2/api/v2/
|
11
|
-
require "weibo_2/api/v2/
|
12
|
-
require "weibo_2/api/v2/
|
13
|
-
require "weibo_2/api/v2/
|
14
|
-
require "weibo_2/api/v2/
|
15
|
-
require "weibo_2/api/v2/
|
16
|
-
require "weibo_2/api/v2/
|
17
|
-
require "weibo_2/api/v2/
|
18
|
-
require "weibo_2/
|
5
|
+
require "weibo_2/access_token"
|
6
|
+
require "weibo_2/api/v2/base"
|
7
|
+
require "weibo_2/api/v2/statuses"
|
8
|
+
require "weibo_2/api/v2/users"
|
9
|
+
require "weibo_2/api/v2/comments"
|
10
|
+
require "weibo_2/api/v2/friendships"
|
11
|
+
require "weibo_2/api/v2/account"
|
12
|
+
require "weibo_2/api/v2/favorites"
|
13
|
+
require "weibo_2/api/v2/trends"
|
14
|
+
require "weibo_2/api/v2/tags"
|
15
|
+
require "weibo_2/api/v2/register"
|
16
|
+
require "weibo_2/api/v2/search"
|
17
|
+
require "weibo_2/api/v2/suggestions"
|
18
|
+
require "weibo_2/api/v2/remind"
|
19
|
+
require "weibo_2/strategy/auth_code"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weibo_2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: oauth2
|
@@ -96,6 +96,7 @@ files:
|
|
96
96
|
- example/views/layout.haml
|
97
97
|
- example/views/screen.sass
|
98
98
|
- lib/weibo_2.rb
|
99
|
+
- lib/weibo_2/access_token.rb
|
99
100
|
- lib/weibo_2/api/v2/account.rb
|
100
101
|
- lib/weibo_2/api/v2/base.rb
|
101
102
|
- lib/weibo_2/api/v2/comments.rb
|