Class: Vkontakte::App::Base
- Inherits:
-
Object
- Object
- Vkontakte::App::Base
- Includes:
- HTTParty
- Defined in:
- lib/vkontakte/app/base.rb
Instance Attribute Summary collapse
-
#auth ⇒ Object
Returns the value of attribute auth.
Instance Method Summary collapse
-
#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.
-
#authorized? ⇒ Boolean
Check if app is authorized.
-
#call(method_name, params = {}) ⇒ Object
Выполнение запросов к API api.vk.com/method/METHOD_NAME?PARAMETERS&access_token=ACCESS_TOKEN METHOD_NAME – название метода из списка функций API, PARAMETERS – параметры соответствующего метода API, ACCESS_TOKEN – ключ доступа, полученный в результате успешной авторизации приложения.
-
#initialize(app_id = nil, app_secret = nil) ⇒ Base
constructor
A new instance of Base.
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
#auth ⇒ Object
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 (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
52 53 54 |
# File 'lib/vkontakte/app/base.rb', line 52 def 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 – ключ доступа, полученный в результате успешной авторизации приложения.
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 unless params[:access_token].blank? && !Vkontakte.config.without_token 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 |