Skip to content

mamantoha/vk_auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vk:Auth

Crystal CI

Crystal library for getting an Access Token without manually accessing vk.com website from the browser.

access_token is needed to run most Vk API requests.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      vk_auth:
        github: mamantoha/vk_auth
  2. Run shards install

Usage

require "vk_auth"

Getting a Token

This library supports the Implicit flow way to obtain an OAuth 2.0 access token.

The Vk::Auth constructor takes one argument - the Vk application ID.

client = Vk::Auth.new(client_id)

The #get_token method takes the following arguments:

client.get_token(email, password, permissions: ["friends"])

After successful authorization, you can access #access_token:

if client.authorized?
  client.access_token # => NamedTuple(access_token: String, expires_in: String, user_id: String)
  access_token = client.token.not_nil!["access_token"]
end

API requests

Note, this shard doesn't provide the functionality to make API requests.

You can use previously obtained access_token to make API requests.

For example:

require "http/client"

access_token = "user-access-token"
method_name = "users.get"
parameters = "fields=bdate,city,country"
version = "5.131"
lang = "en"

url = "https://api.vk.com/method/#{method_name}?#{parameters}&access_token=#{access_token}&v=#{version}&lang=#{lang}"

resp = HTTP::Client.get(url)

Contributing

  1. Fork it (https://github.com/mamantoha/vk_auth/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

About

Crystal library for getting Vk Access Token without manually accessing vk.com website from the browser

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published