Class: Oauth::Weibo

Inherits:
Provider show all
Defined in:
lib/oauth/provider/weibo.rb

Direct Known Subclasses

WeiboMobile

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Provider

#expired?, #fetch_info!, request, type

Class Method Details

.authenticate?(access_token, uid) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/oauth/provider/weibo.rb', line 42

def self.authenticate?(access_token, uid)
  result = postJSON('https://api.weibo.com/oauth2/get_token_info', {access_token: access_token})
  if result.try(:[], 'uid').to_i == uid.to_i
    uid.to_i > 0
  else
    false
  end
end

.authorize_url(params = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/oauth/provider/weibo.rb', line 51

def authorize_url(params = {})
  get_params = {
    'client_id' => Configure['weibo']['appid'],
    'redirect_uri' => Configure['weibo']['callback'],
    'response_type' => 'code',
    'display' => 'default' # for different divice, default|mobile|wap|client|apponweibo
  }.merge(params)
  "https://api.weibo.com/oauth2/authorize?#{URI.encode_www_form(get_params)}";
end

.detail_of_code(code) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/oauth/provider/weibo.rb', line 61

def detail_of_code(code)
  url = 'https://api.weibo.com/oauth2/access_token'
  post_params = {
    'client_id' => Configure['weibo']['appid'],
    'client_secret' => Configure['weibo']['secret'],
    'grant_type' => 'authorization_code',
    'code' => code,
    'redirect_uri' => Configure['weibo']['callback']
  }
  response = postJSON(url,post_params)
  if response
    response.select!{|k, v| %w{access_token uid expires_in}.include?(k)}
  end
  response
end

Instance Method Details

#api_access(api, http_params, http_method = 'get') ⇒ Object



26
27
28
29
30
31
# File 'lib/oauth/provider/weibo.rb', line 26

def api_access(api, http_params, http_method = 'get')
  return nil if expired? # expired
  url = 'https://api.weibo.com/2/' + api + '.json'
  http_params.merge!({"access_token" => access_token})
  Oauth::Weibo.request(url, http_params, http_method, 'json')
end

#basic_infoObject



16
17
18
19
20
21
22
23
24
# File 'lib/oauth/provider/weibo.rb', line 16

def basic_info
  info && {
    "name" => info.data["name"],
    "avatar" => info.data["avatar_hd"] || info.data["avatar_large"],
    "gender" => info.data["gender"],
    "location" => info.data["location"],
    "description" => info.data["description"]
  }
end

#fetch_infoObject



12
13
14
# File 'lib/oauth/provider/weibo.rb', line 12

def fetch_info
  api_access('users/show',{'uid' => uid})
end

#follow(uid) ⇒ Object



4
5
6
# File 'lib/oauth/provider/weibo.rb', line 4

def follow(uid)
  api_access('friendships/create', {'uid' => uid}, 'post')
end

#publish(content) ⇒ Object



8
9
10
# File 'lib/oauth/provider/weibo.rb', line 8

def publish(content)
  api_access('statuses/update', {'status'=>content}, 'post')
end

#refreshObject



33
34
35
36
37
38
39
# File 'lib/oauth/provider/weibo.rb', line 33

def refresh
  info = Oauth::Weibo.postJSON('https://api.weibo.com/oauth2/get_token_info', {access_token: access_token})
  if info
    self.created_at = info["create_at"]
    user.save!
  end
end