weibo_oauth2 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sae_client2.rb +31 -0
- data/lib/sae_oauth2.rb +64 -0
- data/lib/weibo_oauth2.rb +2 -0
- metadata +70 -0
data/lib/sae_client2.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
class SaeClient2
|
2
|
+
attr_reader :oauth
|
3
|
+
|
4
|
+
def initialize(key, secret, access_token, refresh_token)
|
5
|
+
@oauth = SaeOauth2.new(key, secret, access_token, refresh_token)
|
6
|
+
end
|
7
|
+
|
8
|
+
def upload(status, pic_path, lat = nil, long = nil)
|
9
|
+
self.oauth.post('https://api.t.sina.com.cn/statuses/upload.json', {:status => status, :pic => File.new(File.expand_path(pic_path))})
|
10
|
+
end
|
11
|
+
|
12
|
+
def followers(cursor = -1, count = 200)
|
13
|
+
self.oauth.get("https://api.t.sina.com.cn/statuses/followers.json", {:cursor => cursor, :count => count})
|
14
|
+
end
|
15
|
+
|
16
|
+
def friends(cursor = -1, count = 200)
|
17
|
+
self.oauth.get("https://api.t.sina.com.cn/statuses/friends.json", {:cursor => cursor, :count => count})
|
18
|
+
end
|
19
|
+
|
20
|
+
def update(status)
|
21
|
+
self.oauth.post("https://api.t.sina.com.cn/statuses/update.json", {:status => status})
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_friendship(id)
|
25
|
+
self.oauth.post("https://api.t.sina.com.cn/friendships/create.json", {:id => id})
|
26
|
+
end
|
27
|
+
|
28
|
+
def users_show(id)
|
29
|
+
self.oauth.get("https://api.t.sina.com.cn/users/show/#{id}.json")
|
30
|
+
end
|
31
|
+
end
|
data/lib/sae_oauth2.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require "base64"
|
2
|
+
|
3
|
+
class OpenSina
|
4
|
+
include HTTMultiParty
|
5
|
+
base_uri "https://api.t.sina.com.cn/"
|
6
|
+
headers "User-Agent" => "Sae OAuth2 v0.1"
|
7
|
+
end
|
8
|
+
|
9
|
+
class SaeOauth2
|
10
|
+
attr_accessor :client_id, :client_secret, :access_token, :refresh_token
|
11
|
+
attr_reader :access_token_url, :authorize_url
|
12
|
+
|
13
|
+
def initialize(client_id, client_secret, access_token = nil, refresh_token = nil)
|
14
|
+
@access_token_url = "https://api.t.sina.com.cn/oauth2/access_token"
|
15
|
+
@authorize_url = "https://api.t.sina.com.cn/oauth2/authorize"
|
16
|
+
|
17
|
+
@client_id = client_id
|
18
|
+
@client_secret = client_secret
|
19
|
+
@access_token = access_token
|
20
|
+
@refresh_token = refresh_token
|
21
|
+
end
|
22
|
+
|
23
|
+
######################################################
|
24
|
+
#
|
25
|
+
# @param string $signed_request 应用框架在加载iframe时会通过向Canvas URL post的参数signed_request
|
26
|
+
#
|
27
|
+
#####################################################
|
28
|
+
def parse_signed_request(signed_request)
|
29
|
+
encoded_sig, payload = signed_request.split(".")
|
30
|
+
sig = base64_url_decode(encoded_sig)
|
31
|
+
data = Yajl::Parser.parse(base64_url_decode(payload))
|
32
|
+
return nil if data["algorithm"].upcase != "HMAC-SHA256"
|
33
|
+
|
34
|
+
expected_sig = OpenSSL::HMAC.digest("sha256", self.client_secret, payload)
|
35
|
+
(sig != expected_sig) ? nil : data
|
36
|
+
end
|
37
|
+
|
38
|
+
def get(url, parameters = {})
|
39
|
+
response = self.oauth_request(url, 'GET', parameters)
|
40
|
+
Yajl::Parser.parse(response)
|
41
|
+
end
|
42
|
+
|
43
|
+
def post(url, parameters = {})
|
44
|
+
response = self.oauth_request(url, 'POST', parameters)
|
45
|
+
Yajl::Parser.parse(response)
|
46
|
+
end
|
47
|
+
|
48
|
+
def oauth_request(url, method, parameters)
|
49
|
+
url = "#{@host}#{url}.json" unless url.starts_with?("https://")
|
50
|
+
case method
|
51
|
+
when 'GET'
|
52
|
+
OpenSina.get(url, :query => parameters, :headers => {"Authorization" => "OAuth2 #{@access_token}"}).body
|
53
|
+
when 'POST'
|
54
|
+
OpenSina.post(url, :query => parameters, :headers => {"Authorization" => "OAuth2 #{@access_token}"}).body
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
def base64_url_decode str
|
60
|
+
encoded_str = str.gsub('-','+').gsub('_','/')
|
61
|
+
encoded_str += '=' while !(encoded_str.size % 4).zero?
|
62
|
+
Base64.decode64(encoded_str)
|
63
|
+
end
|
64
|
+
end
|
data/lib/weibo_oauth2.rb
ADDED
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weibo_oauth2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Li Guang
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-11-08 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: !binary |
|
22
|
+
5paw5rWq5b6u5Y2a56uZ5YaF5bqU55Sob2F1dGjlrqLmiLfnq68u
|
23
|
+
|
24
|
+
email:
|
25
|
+
- lg2046@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- lib/weibo_oauth2.rb
|
34
|
+
- lib/sae_client2.rb
|
35
|
+
- lib/sae_oauth2.rb
|
36
|
+
homepage: http://open.weibo.com/
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
hash: 3
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.11
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Sina Weibo OAuth 2
|
69
|
+
test_files: []
|
70
|
+
|