vk_auth

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

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

Repository

vk_auth

Owner
Statistic
  • 0
  • 1
  • 0
  • 0
  • 2
  • 2 months ago
  • November 17, 2021
License

MIT License

Links
Synced at

Thu, 07 Nov 2024 04:10:23 GMT

Languages