A
A
aminought2016-07-01 18:35:51
go
aminought, 2016-07-01 18:35:51

How to get an access_token for a standalone VK.API application with minimal effort?

Good evening. Is it possible to automatically get a token for a standalone application? Everything is clear with the site, you open the port and you get it, but then there will be no access to messages, but I need them. Roughly speaking, it will be a golang bot. Is there a way?
UPD. About two years ago I did it with the help of the Qt library browser, and a year ago with the help of Selenium in Java. Any other ideas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2016-07-01
@FireGM

I take a token by login / password something like this

spoiler
package client_vk

import (
  "encoding/json"
  "io/ioutil"
  "net/http"
  "net/url"
)

//Данные приложения для windows клиента
//https://new.vk.com/windows_app
//Это приложение имеет права на прямую авторизацию с логином и паролем
const (
  client_id     = "3697615"
  client_secret = "AlVXZFMUqyrnABp8ncuU"
  authHost      = "oauth.vk.com"
  authPath      = "/token"
)

func GetClientWithLoginPassword(username, password string) (*Client, bool) {
  token := GetTokenWithLoginPassword(username, password)
  if token == "" {
    return &Client{}, false
  }
  return DefaultClient(token), true
}


func GetTokenWithLoginPassword(username, password string) string {
  params := paramsMake(username, password)
  req, err := requestmake(params)
  if err != nil {
    return ""
  }
  res, err := http.DefaultClient.Do(req)
  if err != nil {
    return ""
  }
  b, err := ioutil.ReadAll(res.Body)
  if err != nil {
    return ""
  }
  var response ResponseAuthLoginPass
  json.Unmarshal(b, &response)
  return response.AccessToken
}

func paramsMake(username, password string) url.Values {
  params := url.Values{}
  params.Add("client_id", client_id)
  params.Add("client_secret", client_secret)
  params.Add("grant_type", "password")
  params.Add("v", defailtVersion)
  params.Add("username", username)
  params.Add("password", password)
  return params
}

func requestmake(params url.Values) (*http.Request, error) {
  u := url.URL{}
  u.Host = authHost
  u.Scheme = defaultScheme
  u.Path = authPath
  u.RawQuery = params.Encode()
  req, err := http.NewRequest(defaultMethod, u.String(), nil)
  return req, err
}

Part of the code has been cut out, the project will not build. Just as an example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question