Class: Vkontakte::App::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/vkontakte/app/base.rb

Direct Known Subclasses

Iframe, Secure, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id = nil, app_secret = nil) ⇒ Base

Returns a new instance of Base.



14
15
16
17
18
19
# File 'lib/vkontakte/app/base.rb', line 14

def initialize(app_id = nil, app_secret = nil)
  @config = {
    :app_id => (app_id || Vkontakte.config.app_id),
    :app_secret => (app_secret || Vkontakte.config.app_secret)
  }
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



12
13
14
# File 'lib/vkontakte/app/base.rb', line 12

def auth
  @auth
end

Instance Method Details

#authorize(code = nil, params = {}) ⇒ Object

vk.com/developers.php?oid=-1&p=%D0%90%D0%B2%D1%82%D0%BE%D1%80%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D1%8F Site auth: oauth.vk.com/access_token?

client_id=APP_ID&
client_secret=APP_SECRET&
code=7a6fa4dff77a228eeda56603b8f53806c883f011c40b72630bb50df056f6479e52a

Server auth: oauth.vk.com/access_token?client_id=‘ + APP_ID + ’&client_secret=‘ + APP_SECRET + ’&grant_type=client_credentials’

Response:

{"access_token":"533bacf01e11f55b536a565b57531ac114461ae8736d6506a3", "expires_in":43200, "user_id":6492}


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vkontakte/app/base.rb', line 34

def authorize(code = nil, params = {})
  params = {
    :client_id => @config[:app_id],
    :client_secret => @config[:app_secret],
    :code => code
  }.merge(params)

  # Server auth
  if params[:code].blank?
    params.delete(:code)
    params[:grant_type] = 'client_credentials'
  end

  @auth = get("/access_token", {:query => params, :base_uri => "https://oauth.vk.com"})
end

#authorized?Boolean

Check if app is authorized

Returns:

  • (Boolean)


52
53
54
# File 'lib/vkontakte/app/base.rb', line 52

def authorized?
  auth && auth['access_token']
end

#call(method_name, params = {}) ⇒ Object

Выполнение запросов к API api.vk.com/method/METHOD_NAME?PARAMETERS&access_token=ACCESS_TOKEN

METHOD_NAME – название метода из списка функций API,
PARAMETERS – параметры соответствующего метода API,
ACCESS_TOKEN – ключ доступа, полученный в результате успешной авторизации приложения.

Example: api.vk.com/method/getProfiles?uid=66748&access_token=533bacf01e11f55b536a565b57531ac114461ae8736d6506a3

More info: vk.com/developers.php?oid=-1&p=%D0%92%D1%8B%D0%BF%D0%BE%D0%BB%D0%BD%D0%B5%D0%BD%D0%B8%D0%B5_%D0%B7%D0%B0%D0%BF%D1%80%D0%BE%D1%81%D0%BE%D0%B2_%D0%BA_API



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vkontakte/app/base.rb', line 66

def call(method_name, params = {})
  params[:access_token] ||= @auth['access_token'] if authorized?

  unless params[:access_token].blank?
    get("/method/#{method_name}", {:query => params, :base_uri => "https://api.vk.com"})
  else
    raise VkException.new(method_name, {
      :error => 'access_token is blank',
      :error_description => 'You need first authorize app before call API methods.'
    })
  end
end